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(
64 const struct sctp_endpoint *ep,
65 const struct sctp_association *asoc,
66 struct sctp_chunk *chunk,
67 const void *payload, size_t paylen);
68 static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 struct sctp_cmd_seq *commands);
71 static struct sctp_packet *sctp_ootb_pkt_new(
73 const struct sctp_association *asoc,
74 const struct sctp_chunk *chunk);
75 static void sctp_send_stale_cookie_err(struct net *net,
76 const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 struct sctp_cmd_seq *commands,
80 struct sctp_chunk *err_chunk);
81 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
83 const struct sctp_endpoint *ep,
84 const struct sctp_association *asoc,
85 const union sctp_subtype type,
87 struct sctp_cmd_seq *commands);
88 static enum sctp_disposition sctp_sf_shut_8_4_5(
90 const struct sctp_endpoint *ep,
91 const struct sctp_association *asoc,
92 const union sctp_subtype type,
94 struct sctp_cmd_seq *commands);
95 static enum sctp_disposition sctp_sf_tabort_8_4_8(
97 const struct sctp_endpoint *ep,
98 const struct sctp_association *asoc,
99 const union sctp_subtype type,
101 struct sctp_cmd_seq *commands);
102 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
104 static enum sctp_disposition sctp_stop_t1_and_abort(
106 struct sctp_cmd_seq *commands,
107 __be16 error, int sk_err,
108 const struct sctp_association *asoc,
109 struct sctp_transport *transport);
111 static enum sctp_disposition sctp_sf_abort_violation(
113 const struct sctp_endpoint *ep,
114 const struct sctp_association *asoc,
116 struct sctp_cmd_seq *commands,
118 const size_t paylen);
120 static enum sctp_disposition sctp_sf_violation_chunklen(
122 const struct sctp_endpoint *ep,
123 const struct sctp_association *asoc,
124 const union sctp_subtype type,
126 struct sctp_cmd_seq *commands);
128 static enum sctp_disposition sctp_sf_violation_paramlen(
130 const struct sctp_endpoint *ep,
131 const struct sctp_association *asoc,
132 const union sctp_subtype type,
133 void *arg, void *ext,
134 struct sctp_cmd_seq *commands);
136 static enum sctp_disposition sctp_sf_violation_ctsn(
138 const struct sctp_endpoint *ep,
139 const struct sctp_association *asoc,
140 const union sctp_subtype type,
142 struct sctp_cmd_seq *commands);
144 static enum sctp_disposition sctp_sf_violation_chunk(
146 const struct sctp_endpoint *ep,
147 const struct sctp_association *asoc,
148 const union sctp_subtype type,
150 struct sctp_cmd_seq *commands);
152 static enum sctp_ierror sctp_sf_authenticate(
153 const struct sctp_association *asoc,
154 struct sctp_chunk *chunk);
156 static enum sctp_disposition __sctp_sf_do_9_1_abort(
158 const struct sctp_endpoint *ep,
159 const struct sctp_association *asoc,
160 const union sctp_subtype type,
162 struct sctp_cmd_seq *commands);
164 /* Small helper function that checks if the chunk length
165 * is of the appropriate length. The 'required_length' argument
166 * is set to be the size of a specific chunk we are testing.
167 * Return Values: true = Valid length
168 * false = Invalid length
171 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
172 __u16 required_length)
174 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
176 /* Previously already marked? */
177 if (unlikely(chunk->pdiscard))
179 if (unlikely(chunk_length < required_length))
185 /* Check for format error in an ABORT chunk */
186 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
188 struct sctp_errhdr *err;
190 sctp_walk_errors(err, chunk->chunk_hdr);
192 return (void *)err == (void *)chunk->chunk_end;
195 /**********************************************************
196 * These are the state functions for handling chunk events.
197 **********************************************************/
200 * Process the final SHUTDOWN COMPLETE.
202 * Section: 4 (C) (diagram), 9.2
203 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
204 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
205 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
206 * should stop the T2-shutdown timer and remove all knowledge of the
207 * association (and thus the association enters the CLOSED state).
209 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
210 * C) Rules for packet carrying SHUTDOWN COMPLETE:
212 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
213 * if the Verification Tag field of the packet matches its own tag and
214 * the T bit is not set
216 * it is set to its peer's tag and the T bit is set in the Chunk
218 * Otherwise, the receiver MUST silently discard the packet
219 * and take no further action. An endpoint MUST ignore the
220 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
223 * (endpoint, asoc, chunk)
226 * (asoc, reply_msg, msg_up, timers, counters)
228 * The return value is the disposition of the chunk.
230 enum sctp_disposition sctp_sf_do_4_C(struct net *net,
231 const struct sctp_endpoint *ep,
232 const struct sctp_association *asoc,
233 const union sctp_subtype type,
234 void *arg, struct sctp_cmd_seq *commands)
236 struct sctp_chunk *chunk = arg;
237 struct sctp_ulpevent *ev;
239 if (!sctp_vtag_verify_either(chunk, asoc))
240 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
242 /* RFC 2960 6.10 Bundling
244 * An endpoint MUST NOT bundle INIT, INIT ACK or
245 * SHUTDOWN COMPLETE with any other chunks.
247 if (!chunk->singleton)
248 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
250 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
251 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
252 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
255 /* RFC 2960 10.2 SCTP-to-ULP
257 * H) SHUTDOWN COMPLETE notification
259 * When SCTP completes the shutdown procedures (section 9.2) this
260 * notification is passed to the upper layer.
262 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
263 0, 0, 0, NULL, GFP_ATOMIC);
265 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
268 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
269 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
270 * not the chunk should be discarded. If the endpoint is in
271 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
272 * T2-shutdown timer and remove all knowledge of the
273 * association (and thus the association enters the CLOSED
276 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
277 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
279 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
280 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
282 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
283 SCTP_STATE(SCTP_STATE_CLOSED));
285 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
286 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
288 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
290 return SCTP_DISPOSITION_DELETE_TCB;
294 * Respond to a normal INIT chunk.
295 * We are the side that is being asked for an association.
297 * Section: 5.1 Normal Establishment of an Association, B
298 * B) "Z" shall respond immediately with an INIT ACK chunk. The
299 * destination IP address of the INIT ACK MUST be set to the source
300 * IP address of the INIT to which this INIT ACK is responding. In
301 * the response, besides filling in other parameters, "Z" must set the
302 * Verification Tag field to Tag_A, and also provide its own
303 * Verification Tag (Tag_Z) in the Initiate Tag field.
305 * Verification Tag: Must be 0.
308 * (endpoint, asoc, chunk)
311 * (asoc, reply_msg, msg_up, timers, counters)
313 * The return value is the disposition of the chunk.
315 enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
316 const struct sctp_endpoint *ep,
317 const struct sctp_association *asoc,
318 const union sctp_subtype type,
320 struct sctp_cmd_seq *commands)
322 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
323 struct sctp_unrecognized_param *unk_param;
324 struct sctp_association *new_asoc;
325 struct sctp_packet *packet;
329 * An endpoint MUST NOT bundle INIT, INIT ACK or
330 * SHUTDOWN COMPLETE with any other chunks.
333 * Furthermore, we require that the receiver of an INIT chunk MUST
334 * enforce these rules by silently discarding an arriving packet
335 * with an INIT chunk that is bundled with other chunks.
337 if (!chunk->singleton)
338 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
340 /* If the packet is an OOTB packet which is temporarily on the
341 * control endpoint, respond with an ABORT.
343 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
344 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
345 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
348 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
351 if (chunk->sctp_hdr->vtag != 0)
352 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
354 /* Make sure that the INIT chunk has a valid length.
355 * Normally, this would cause an ABORT with a Protocol Violation
356 * error, but since we don't have an association, we'll
357 * just discard the packet.
359 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
360 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
362 /* If the INIT is coming toward a closing socket, we'll send back
363 * and ABORT. Essentially, this catches the race of INIT being
364 * backloged to the socket at the same time as the user isses close().
365 * Since the socket and all its associations are going away, we
366 * can treat this OOTB
368 if (sctp_sstate(ep->base.sk, CLOSING))
369 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
371 /* Verify the INIT chunk before processing it. */
373 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
374 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
376 /* This chunk contains fatal error. It is to be discarded.
377 * Send an ABORT, with causes if there is any.
380 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
381 (__u8 *)(err_chunk->chunk_hdr) +
382 sizeof(struct sctp_chunkhdr),
383 ntohs(err_chunk->chunk_hdr->length) -
384 sizeof(struct sctp_chunkhdr));
386 sctp_chunk_free(err_chunk);
389 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
390 SCTP_PACKET(packet));
391 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
392 return SCTP_DISPOSITION_CONSUME;
394 return SCTP_DISPOSITION_NOMEM;
397 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
402 /* Grab the INIT header. */
403 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
405 /* Tag the variable length parameters. */
406 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
408 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
412 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
413 sctp_scope(sctp_source(chunk)),
417 /* The call, sctp_process_init(), can fail on memory allocation. */
418 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
419 (struct sctp_init_chunk *)chunk->chunk_hdr,
423 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
425 /* If there are errors need to be reported for unknown parameters,
426 * make sure to reserve enough room in the INIT ACK for them.
430 len = ntohs(err_chunk->chunk_hdr->length) -
431 sizeof(struct sctp_chunkhdr);
433 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
437 /* If there are errors need to be reported for unknown parameters,
438 * include them in the outgoing INIT ACK as "Unrecognized parameter"
442 /* Get the "Unrecognized parameter" parameter(s) out of the
443 * ERROR chunk generated by sctp_verify_init(). Since the
444 * error cause code for "unknown parameter" and the
445 * "Unrecognized parameter" type is the same, we can
446 * construct the parameters in INIT ACK by copying the
449 unk_param = (struct sctp_unrecognized_param *)
450 ((__u8 *)(err_chunk->chunk_hdr) +
451 sizeof(struct sctp_chunkhdr));
452 /* Replace the cause code with the "Unrecognized parameter"
455 sctp_addto_chunk(repl, len, unk_param);
456 sctp_chunk_free(err_chunk);
459 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
461 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
464 * Note: After sending out INIT ACK with the State Cookie parameter,
465 * "Z" MUST NOT allocate any resources, nor keep any states for the
466 * new association. Otherwise, "Z" will be vulnerable to resource
469 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
471 return SCTP_DISPOSITION_DELETE_TCB;
474 sctp_association_free(new_asoc);
477 sctp_chunk_free(err_chunk);
478 return SCTP_DISPOSITION_NOMEM;
482 * Respond to a normal INIT ACK chunk.
483 * We are the side that is initiating the association.
485 * Section: 5.1 Normal Establishment of an Association, C
486 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
487 * timer and leave COOKIE-WAIT state. "A" shall then send the State
488 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
489 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
491 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
492 * DATA chunks, but it MUST be the first chunk in the packet and
493 * until the COOKIE ACK is returned the sender MUST NOT send any
494 * other packets to the peer.
496 * Verification Tag: 3.3.3
497 * If the value of the Initiate Tag in a received INIT ACK chunk is
498 * found to be 0, the receiver MUST treat it as an error and close the
499 * association by transmitting an ABORT.
502 * (endpoint, asoc, chunk)
505 * (asoc, reply_msg, msg_up, timers, counters)
507 * The return value is the disposition of the chunk.
509 enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
510 const struct sctp_endpoint *ep,
511 const struct sctp_association *asoc,
512 const union sctp_subtype type,
514 struct sctp_cmd_seq *commands)
516 struct sctp_init_chunk *initchunk;
517 struct sctp_chunk *chunk = arg;
518 struct sctp_chunk *err_chunk;
519 struct sctp_packet *packet;
521 if (!sctp_vtag_verify(chunk, asoc))
522 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
525 * An endpoint MUST NOT bundle INIT, INIT ACK or
526 * SHUTDOWN COMPLETE with any other chunks.
528 if (!chunk->singleton)
529 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
531 /* Make sure that the INIT-ACK chunk has a valid length */
532 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
533 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
535 /* Grab the INIT header. */
536 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
538 /* Verify the INIT chunk before processing it. */
540 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
541 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
544 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
546 /* This chunk contains fatal error. It is to be discarded.
547 * Send an ABORT, with causes. If there are no causes,
548 * then there wasn't enough memory. Just terminate
552 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
553 (__u8 *)(err_chunk->chunk_hdr) +
554 sizeof(struct sctp_chunkhdr),
555 ntohs(err_chunk->chunk_hdr->length) -
556 sizeof(struct sctp_chunkhdr));
558 sctp_chunk_free(err_chunk);
561 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
562 SCTP_PACKET(packet));
563 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
564 error = SCTP_ERROR_INV_PARAM;
568 /* SCTP-AUTH, Section 6.3:
569 * It should be noted that if the receiver wants to tear
570 * down an association in an authenticated way only, the
571 * handling of malformed packets should not result in
572 * tearing down the association.
574 * This means that if we only want to abort associations
575 * in an authenticated way (i.e AUTH+ABORT), then we
576 * can't destroy this association just because the packet
579 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
580 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
582 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
583 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
584 asoc, chunk->transport);
587 /* Tag the variable length parameters. Note that we never
588 * convert the parameters in an INIT chunk.
590 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
592 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
594 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
595 SCTP_PEER_INIT(initchunk));
597 /* Reset init error count upon receipt of INIT-ACK. */
598 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
600 /* 5.1 C) "A" shall stop the T1-init timer and leave
601 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
602 * timer, and enter the COOKIE-ECHOED state.
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
606 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
607 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
608 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
609 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
611 /* SCTP-AUTH: genereate the assocition shared keys so that
612 * we can potentially signe the COOKIE-ECHO.
614 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
616 /* 5.1 C) "A" shall then send the State Cookie received in the
617 * INIT ACK chunk in a COOKIE ECHO chunk, ...
619 /* If there is any errors to report, send the ERROR chunk generated
620 * for unknown parameters as well.
622 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
623 SCTP_CHUNK(err_chunk));
625 return SCTP_DISPOSITION_CONSUME;
628 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
629 const struct sctp_association *asoc)
631 struct sctp_chunk auth;
633 if (!chunk->auth_chunk)
636 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
637 * is supposed to be authenticated and we have to do delayed
638 * authentication. We've just recreated the association using
639 * the information in the cookie and now it's much easier to
640 * do the authentication.
643 /* Make sure that we and the peer are AUTH capable */
644 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
647 /* set-up our fake chunk so that we can process it */
648 auth.skb = chunk->auth_chunk;
649 auth.asoc = chunk->asoc;
650 auth.sctp_hdr = chunk->sctp_hdr;
651 auth.chunk_hdr = (struct sctp_chunkhdr *)
652 skb_push(chunk->auth_chunk,
653 sizeof(struct sctp_chunkhdr));
654 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
655 auth.transport = chunk->transport;
657 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
661 * Respond to a normal COOKIE ECHO chunk.
662 * We are the side that is being asked for an association.
664 * Section: 5.1 Normal Establishment of an Association, D
665 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
666 * with a COOKIE ACK chunk after building a TCB and moving to
667 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
668 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
669 * chunk MUST be the first chunk in the packet.
671 * IMPLEMENTATION NOTE: An implementation may choose to send the
672 * Communication Up notification to the SCTP user upon reception
673 * of a valid COOKIE ECHO chunk.
675 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
676 * D) Rules for packet carrying a COOKIE ECHO
678 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
679 * Initial Tag received in the INIT ACK.
681 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
684 * (endpoint, asoc, chunk)
687 * (asoc, reply_msg, msg_up, timers, counters)
689 * The return value is the disposition of the chunk.
691 enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
692 const struct sctp_endpoint *ep,
693 const struct sctp_association *asoc,
694 const union sctp_subtype type,
696 struct sctp_cmd_seq *commands)
698 struct sctp_ulpevent *ev, *ai_ev = NULL;
699 struct sctp_association *new_asoc;
700 struct sctp_init_chunk *peer_init;
701 struct sctp_chunk *chunk = arg;
702 struct sctp_chunk *err_chk_p;
703 struct sctp_chunk *repl;
707 /* If the packet is an OOTB packet which is temporarily on the
708 * control endpoint, respond with an ABORT.
710 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
711 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
712 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
715 /* Make sure that the COOKIE_ECHO chunk has a valid length.
716 * In this case, we check that we have enough for at least a
717 * chunk header. More detailed verification is done
718 * in sctp_unpack_cookie().
720 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
721 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
723 /* If the endpoint is not listening or if the number of associations
724 * on the TCP-style socket exceed the max backlog, respond with an
728 if (!sctp_sstate(sk, LISTENING) ||
729 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
730 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
732 /* "Decode" the chunk. We have no optional parameters so we
735 chunk->subh.cookie_hdr =
736 (struct sctp_signed_cookie *)chunk->skb->data;
737 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
738 sizeof(struct sctp_chunkhdr)))
741 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
742 * "Z" will reply with a COOKIE ACK chunk after building a TCB
743 * and moving to the ESTABLISHED state.
745 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
749 * If the re-build failed, what is the proper error path
752 * [We should abort the association. --piggy]
755 /* FIXME: Several errors are possible. A bad cookie should
756 * be silently discarded, but think about logging it too.
759 case -SCTP_IERROR_NOMEM:
762 case -SCTP_IERROR_STALE_COOKIE:
763 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
765 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
767 case -SCTP_IERROR_BAD_SIG:
769 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
774 /* Delay state machine commands until later.
776 * Re-build the bind address for the association is done in
777 * the sctp_unpack_cookie() already.
779 /* This is a brand-new association, so these are not yet side
780 * effects--it is safe to run them here.
782 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
784 if (!sctp_process_init(new_asoc, chunk,
785 &chunk->subh.cookie_hdr->c.peer_addr,
786 peer_init, GFP_ATOMIC))
789 /* SCTP-AUTH: Now that we've populate required fields in
790 * sctp_process_init, set up the assocaition shared keys as
791 * necessary so that we can potentially authenticate the ACK
793 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
797 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
798 sctp_association_free(new_asoc);
799 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
802 repl = sctp_make_cookie_ack(new_asoc, chunk);
806 /* RFC 2960 5.1 Normal Establishment of an Association
808 * D) IMPLEMENTATION NOTE: An implementation may choose to
809 * send the Communication Up notification to the SCTP user
810 * upon reception of a valid COOKIE ECHO chunk.
812 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
813 new_asoc->c.sinit_num_ostreams,
814 new_asoc->c.sinit_max_instreams,
819 /* Sockets API Draft Section 5.3.1.6
820 * When a peer sends a Adaptation Layer Indication parameter , SCTP
821 * delivers this notification to inform the application that of the
822 * peers requested adaptation layer.
824 if (new_asoc->peer.adaptation_ind) {
825 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
831 /* Add all the state machine commands now since we've created
832 * everything. This way we don't introduce memory corruptions
833 * during side-effect processing and correclty count established
836 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
837 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
838 SCTP_STATE(SCTP_STATE_ESTABLISHED));
839 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
840 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
841 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
843 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
844 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
845 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
847 /* This will send the COOKIE ACK */
848 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
850 /* Queue the ASSOC_CHANGE event */
851 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
853 /* Send up the Adaptation Layer Indication event */
855 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
856 SCTP_ULPEVENT(ai_ev));
858 return SCTP_DISPOSITION_CONSUME;
861 sctp_ulpevent_free(ev);
863 sctp_chunk_free(repl);
865 sctp_association_free(new_asoc);
867 return SCTP_DISPOSITION_NOMEM;
871 * Respond to a normal COOKIE ACK chunk.
872 * We are the side that is asking for an association.
874 * RFC 2960 5.1 Normal Establishment of an Association
876 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
877 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
878 * timer. It may also notify its ULP about the successful
879 * establishment of the association with a Communication Up
880 * notification (see Section 10).
884 * (endpoint, asoc, chunk)
887 * (asoc, reply_msg, msg_up, timers, counters)
889 * The return value is the disposition of the chunk.
891 enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
892 const struct sctp_endpoint *ep,
893 const struct sctp_association *asoc,
894 const union sctp_subtype type,
896 struct sctp_cmd_seq *commands)
898 struct sctp_chunk *chunk = arg;
899 struct sctp_ulpevent *ev;
901 if (!sctp_vtag_verify(chunk, asoc))
902 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
904 /* Verify that the chunk length for the COOKIE-ACK is OK.
905 * If we don't do this, any bundled chunks may be junked.
907 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
908 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
911 /* Reset init error count upon receipt of COOKIE-ACK,
912 * to avoid problems with the managemement of this
913 * counter in stale cookie situations when a transition back
914 * from the COOKIE-ECHOED state to the COOKIE-WAIT
915 * state is performed.
917 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
919 /* RFC 2960 5.1 Normal Establishment of an Association
921 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
922 * from the COOKIE-ECHOED state to the ESTABLISHED state,
923 * stopping the T1-cookie timer.
925 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
926 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
927 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
928 SCTP_STATE(SCTP_STATE_ESTABLISHED));
929 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
930 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
931 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
932 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
933 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
934 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
936 /* It may also notify its ULP about the successful
937 * establishment of the association with a Communication Up
938 * notification (see Section 10).
940 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
941 0, asoc->c.sinit_num_ostreams,
942 asoc->c.sinit_max_instreams,
948 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
950 /* Sockets API Draft Section 5.3.1.6
951 * When a peer sends a Adaptation Layer Indication parameter , SCTP
952 * delivers this notification to inform the application that of the
953 * peers requested adaptation layer.
955 if (asoc->peer.adaptation_ind) {
956 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
960 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
964 return SCTP_DISPOSITION_CONSUME;
966 return SCTP_DISPOSITION_NOMEM;
969 /* Generate and sendout a heartbeat packet. */
970 static enum sctp_disposition sctp_sf_heartbeat(
971 const struct sctp_endpoint *ep,
972 const struct sctp_association *asoc,
973 const union sctp_subtype type,
975 struct sctp_cmd_seq *commands)
977 struct sctp_transport *transport = (struct sctp_transport *) arg;
978 struct sctp_chunk *reply;
980 /* Send a heartbeat to our peer. */
981 reply = sctp_make_heartbeat(asoc, transport);
983 return SCTP_DISPOSITION_NOMEM;
985 /* Set rto_pending indicating that an RTT measurement
986 * is started with this heartbeat chunk.
988 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
989 SCTP_TRANSPORT(transport));
991 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
992 return SCTP_DISPOSITION_CONSUME;
995 /* Generate a HEARTBEAT packet on the given transport. */
996 enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
997 const struct sctp_endpoint *ep,
998 const struct sctp_association *asoc,
999 const union sctp_subtype type,
1001 struct sctp_cmd_seq *commands)
1003 struct sctp_transport *transport = (struct sctp_transport *) arg;
1005 if (asoc->overall_error_count >= asoc->max_retrans) {
1006 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1007 SCTP_ERROR(ETIMEDOUT));
1008 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1009 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1010 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1011 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1012 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1013 return SCTP_DISPOSITION_DELETE_TCB;
1017 * The Sender-specific Heartbeat Info field should normally include
1018 * information about the sender's current time when this HEARTBEAT
1019 * chunk is sent and the destination transport address to which this
1020 * HEARTBEAT is sent (see Section 8.3).
1023 if (transport->param_flags & SPP_HB_ENABLE) {
1024 if (SCTP_DISPOSITION_NOMEM ==
1025 sctp_sf_heartbeat(ep, asoc, type, arg,
1027 return SCTP_DISPOSITION_NOMEM;
1029 /* Set transport error counter and association error counter
1030 * when sending heartbeat.
1032 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1033 SCTP_TRANSPORT(transport));
1035 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1036 SCTP_TRANSPORT(transport));
1037 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1038 SCTP_TRANSPORT(transport));
1040 return SCTP_DISPOSITION_CONSUME;
1043 /* resend asoc strreset_chunk. */
1044 enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1045 const struct sctp_endpoint *ep,
1046 const struct sctp_association *asoc,
1047 const union sctp_subtype type,
1049 struct sctp_cmd_seq *commands)
1051 struct sctp_transport *transport = arg;
1053 if (asoc->overall_error_count >= asoc->max_retrans) {
1054 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1055 SCTP_ERROR(ETIMEDOUT));
1056 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1057 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1058 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1059 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1060 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1061 return SCTP_DISPOSITION_DELETE_TCB;
1064 sctp_chunk_hold(asoc->strreset_chunk);
1065 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1066 SCTP_CHUNK(asoc->strreset_chunk));
1067 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1069 return SCTP_DISPOSITION_CONSUME;
1073 * Process an heartbeat request.
1075 * Section: 8.3 Path Heartbeat
1076 * The receiver of the HEARTBEAT should immediately respond with a
1077 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1078 * from the received HEARTBEAT chunk.
1080 * Verification Tag: 8.5 Verification Tag [Normal verification]
1081 * When receiving an SCTP packet, the endpoint MUST ensure that the
1082 * value in the Verification Tag field of the received SCTP packet
1083 * matches its own Tag. If the received Verification Tag value does not
1084 * match the receiver's own tag value, the receiver shall silently
1085 * discard the packet and shall not process it any further except for
1086 * those cases listed in Section 8.5.1 below.
1089 * (endpoint, asoc, chunk)
1092 * (asoc, reply_msg, msg_up, timers, counters)
1094 * The return value is the disposition of the chunk.
1096 enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1097 const struct sctp_endpoint *ep,
1098 const struct sctp_association *asoc,
1099 const union sctp_subtype type,
1100 void *arg, struct sctp_cmd_seq *commands)
1102 struct sctp_paramhdr *param_hdr;
1103 struct sctp_chunk *chunk = arg;
1104 struct sctp_chunk *reply;
1107 if (!sctp_vtag_verify(chunk, asoc))
1108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1110 /* Make sure that the HEARTBEAT chunk has a valid length. */
1111 if (!sctp_chunk_length_valid(chunk,
1112 sizeof(struct sctp_heartbeat_chunk)))
1113 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1116 /* 8.3 The receiver of the HEARTBEAT should immediately
1117 * respond with a HEARTBEAT ACK that contains the Heartbeat
1118 * Information field copied from the received HEARTBEAT chunk.
1120 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1121 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1122 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1124 if (ntohs(param_hdr->length) > paylen)
1125 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1126 param_hdr, commands);
1128 if (!pskb_pull(chunk->skb, paylen))
1131 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1135 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1136 return SCTP_DISPOSITION_CONSUME;
1139 return SCTP_DISPOSITION_NOMEM;
1143 * Process the returning HEARTBEAT ACK.
1145 * Section: 8.3 Path Heartbeat
1146 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1147 * should clear the error counter of the destination transport
1148 * address to which the HEARTBEAT was sent, and mark the destination
1149 * transport address as active if it is not so marked. The endpoint may
1150 * optionally report to the upper layer when an inactive destination
1151 * address is marked as active due to the reception of the latest
1152 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1153 * clear the association overall error count as well (as defined
1156 * The receiver of the HEARTBEAT ACK should also perform an RTT
1157 * measurement for that destination transport address using the time
1158 * value carried in the HEARTBEAT ACK chunk.
1160 * Verification Tag: 8.5 Verification Tag [Normal verification]
1163 * (endpoint, asoc, chunk)
1166 * (asoc, reply_msg, msg_up, timers, counters)
1168 * The return value is the disposition of the chunk.
1170 enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1171 const struct sctp_endpoint *ep,
1172 const struct sctp_association *asoc,
1173 const union sctp_subtype type,
1175 struct sctp_cmd_seq *commands)
1177 struct sctp_sender_hb_info *hbinfo;
1178 struct sctp_chunk *chunk = arg;
1179 struct sctp_transport *link;
1180 unsigned long max_interval;
1181 union sctp_addr from_addr;
1183 if (!sctp_vtag_verify(chunk, asoc))
1184 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1186 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1187 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1189 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1192 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1193 /* Make sure that the length of the parameter is what we expect */
1194 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1195 return SCTP_DISPOSITION_DISCARD;
1197 from_addr = hbinfo->daddr;
1198 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1200 /* This should never happen, but lets log it if so. */
1201 if (unlikely(!link)) {
1202 if (from_addr.sa.sa_family == AF_INET6) {
1203 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1206 &from_addr.v6.sin6_addr);
1208 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1211 &from_addr.v4.sin_addr.s_addr);
1213 return SCTP_DISPOSITION_DISCARD;
1216 /* Validate the 64-bit random nonce. */
1217 if (hbinfo->hb_nonce != link->hb_nonce)
1218 return SCTP_DISPOSITION_DISCARD;
1220 max_interval = link->hbinterval + link->rto;
1222 /* Check if the timestamp looks valid. */
1223 if (time_after(hbinfo->sent_at, jiffies) ||
1224 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1225 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1226 "for transport:%p\n", __func__, link);
1228 return SCTP_DISPOSITION_DISCARD;
1231 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1232 * the HEARTBEAT should clear the error counter of the
1233 * destination transport address to which the HEARTBEAT was
1234 * sent and mark the destination transport address as active if
1235 * it is not so marked.
1237 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1239 return SCTP_DISPOSITION_CONSUME;
1242 /* Helper function to send out an abort for the restart
1245 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1246 struct sctp_chunk *init,
1247 struct sctp_cmd_seq *commands)
1249 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1250 union sctp_addr_param *addrparm;
1251 struct sctp_errhdr *errhdr;
1252 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1253 struct sctp_endpoint *ep;
1254 struct sctp_packet *pkt;
1257 /* Build the error on the stack. We are way to malloc crazy
1258 * throughout the code today.
1260 errhdr = (struct sctp_errhdr *)buffer;
1261 addrparm = (union sctp_addr_param *)errhdr->variable;
1263 /* Copy into a parm format. */
1264 len = af->to_addr_param(ssa, addrparm);
1265 len += sizeof(*errhdr);
1267 errhdr->cause = SCTP_ERROR_RESTART;
1268 errhdr->length = htons(len);
1270 /* Assign to the control socket. */
1271 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1273 /* Association is NULL since this may be a restart attack and we
1274 * want to send back the attacker's vtag.
1276 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1280 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1282 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1284 /* Discard the rest of the inbound packet. */
1285 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1288 /* Even if there is no memory, treat as a failure so
1289 * the packet will get dropped.
1294 static bool list_has_sctp_addr(const struct list_head *list,
1295 union sctp_addr *ipaddr)
1297 struct sctp_transport *addr;
1299 list_for_each_entry(addr, list, transports) {
1300 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1306 /* A restart is occurring, check to make sure no new addresses
1307 * are being added as we may be under a takeover attack.
1309 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1310 const struct sctp_association *asoc,
1311 struct sctp_chunk *init,
1312 struct sctp_cmd_seq *commands)
1314 struct net *net = sock_net(new_asoc->base.sk);
1315 struct sctp_transport *new_addr;
1318 /* Implementor's Guide - Section 5.2.2
1320 * Before responding the endpoint MUST check to see if the
1321 * unexpected INIT adds new addresses to the association. If new
1322 * addresses are added to the association, the endpoint MUST respond
1326 /* Search through all current addresses and make sure
1327 * we aren't adding any new ones.
1329 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1331 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1332 &new_addr->ipaddr)) {
1333 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1340 /* Return success if all addresses were found. */
1344 /* Populate the verification/tie tags based on overlapping INIT
1347 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1349 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1350 const struct sctp_association *asoc)
1352 switch (asoc->state) {
1354 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1356 case SCTP_STATE_COOKIE_WAIT:
1357 new_asoc->c.my_vtag = asoc->c.my_vtag;
1358 new_asoc->c.my_ttag = asoc->c.my_vtag;
1359 new_asoc->c.peer_ttag = 0;
1362 case SCTP_STATE_COOKIE_ECHOED:
1363 new_asoc->c.my_vtag = asoc->c.my_vtag;
1364 new_asoc->c.my_ttag = asoc->c.my_vtag;
1365 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1368 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1369 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1372 new_asoc->c.my_ttag = asoc->c.my_vtag;
1373 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1377 /* Other parameters for the endpoint SHOULD be copied from the
1378 * existing parameters of the association (e.g. number of
1379 * outbound streams) into the INIT ACK and cookie.
1381 new_asoc->rwnd = asoc->rwnd;
1382 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1383 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1384 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1388 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1391 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1393 * Returns value representing action to be taken. These action values
1394 * correspond to Action/Description values in RFC 2960, Table 2.
1396 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1397 const struct sctp_association *asoc)
1399 /* In this case, the peer may have restarted. */
1400 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1401 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1402 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1403 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1406 /* Collision case B. */
1407 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1408 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1409 (0 == asoc->c.peer_vtag))) {
1413 /* Collision case D. */
1414 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1415 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1418 /* Collision case C. */
1419 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1420 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1421 (0 == new_asoc->c.my_ttag) &&
1422 (0 == new_asoc->c.peer_ttag))
1425 /* No match to any of the special cases; discard this packet. */
1429 /* Common helper routine for both duplicate and simulataneous INIT
1432 static enum sctp_disposition sctp_sf_do_unexpected_init(
1434 const struct sctp_endpoint *ep,
1435 const struct sctp_association *asoc,
1436 const union sctp_subtype type,
1438 struct sctp_cmd_seq *commands)
1440 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1441 struct sctp_unrecognized_param *unk_param;
1442 struct sctp_association *new_asoc;
1443 enum sctp_disposition retval;
1444 struct sctp_packet *packet;
1448 * An endpoint MUST NOT bundle INIT, INIT ACK or
1449 * SHUTDOWN COMPLETE with any other chunks.
1452 * Furthermore, we require that the receiver of an INIT chunk MUST
1453 * enforce these rules by silently discarding an arriving packet
1454 * with an INIT chunk that is bundled with other chunks.
1456 if (!chunk->singleton)
1457 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1459 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1462 if (chunk->sctp_hdr->vtag != 0)
1463 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1465 /* Make sure that the INIT chunk has a valid length.
1466 * In this case, we generate a protocol violation since we have
1467 * an association established.
1469 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1470 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1472 /* Grab the INIT header. */
1473 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1475 /* Tag the variable length parameters. */
1476 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1478 /* Verify the INIT chunk before processing it. */
1480 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1481 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1483 /* This chunk contains fatal error. It is to be discarded.
1484 * Send an ABORT, with causes if there is any.
1487 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1488 (__u8 *)(err_chunk->chunk_hdr) +
1489 sizeof(struct sctp_chunkhdr),
1490 ntohs(err_chunk->chunk_hdr->length) -
1491 sizeof(struct sctp_chunkhdr));
1494 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1495 SCTP_PACKET(packet));
1496 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1497 retval = SCTP_DISPOSITION_CONSUME;
1499 retval = SCTP_DISPOSITION_NOMEM;
1503 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1509 * Other parameters for the endpoint SHOULD be copied from the
1510 * existing parameters of the association (e.g. number of
1511 * outbound streams) into the INIT ACK and cookie.
1512 * FIXME: We are copying parameters from the endpoint not the
1515 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1519 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1520 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1523 /* In the outbound INIT ACK the endpoint MUST copy its current
1524 * Verification Tag and Peers Verification tag into a reserved
1525 * place (local tie-tag and per tie-tag) within the state cookie.
1527 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1528 (struct sctp_init_chunk *)chunk->chunk_hdr,
1532 /* Make sure no new addresses are being added during the
1533 * restart. Do not do this check for COOKIE-WAIT state,
1534 * since there are no peer addresses to check against.
1535 * Upon return an ABORT will have been sent if needed.
1537 if (!sctp_state(asoc, COOKIE_WAIT)) {
1538 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1540 retval = SCTP_DISPOSITION_CONSUME;
1545 sctp_tietags_populate(new_asoc, asoc);
1547 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1549 /* If there are errors need to be reported for unknown parameters,
1550 * make sure to reserve enough room in the INIT ACK for them.
1554 len = ntohs(err_chunk->chunk_hdr->length) -
1555 sizeof(struct sctp_chunkhdr);
1558 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1562 /* If there are errors need to be reported for unknown parameters,
1563 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1567 /* Get the "Unrecognized parameter" parameter(s) out of the
1568 * ERROR chunk generated by sctp_verify_init(). Since the
1569 * error cause code for "unknown parameter" and the
1570 * "Unrecognized parameter" type is the same, we can
1571 * construct the parameters in INIT ACK by copying the
1572 * ERROR causes over.
1574 unk_param = (struct sctp_unrecognized_param *)
1575 ((__u8 *)(err_chunk->chunk_hdr) +
1576 sizeof(struct sctp_chunkhdr));
1577 /* Replace the cause code with the "Unrecognized parameter"
1580 sctp_addto_chunk(repl, len, unk_param);
1583 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1584 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1587 * Note: After sending out INIT ACK with the State Cookie parameter,
1588 * "Z" MUST NOT allocate any resources for this new association.
1589 * Otherwise, "Z" will be vulnerable to resource attacks.
1591 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1592 retval = SCTP_DISPOSITION_CONSUME;
1597 retval = SCTP_DISPOSITION_NOMEM;
1600 sctp_association_free(new_asoc);
1603 sctp_chunk_free(err_chunk);
1608 * Handle simultaneous INIT.
1609 * This means we started an INIT and then we got an INIT request from
1612 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1613 * This usually indicates an initialization collision, i.e., each
1614 * endpoint is attempting, at about the same time, to establish an
1615 * association with the other endpoint.
1617 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1618 * endpoint MUST respond with an INIT ACK using the same parameters it
1619 * sent in its original INIT chunk (including its Verification Tag,
1620 * unchanged). These original parameters are combined with those from the
1621 * newly received INIT chunk. The endpoint shall also generate a State
1622 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1623 * INIT to calculate the State Cookie.
1625 * After that, the endpoint MUST NOT change its state, the T1-init
1626 * timer shall be left running and the corresponding TCB MUST NOT be
1627 * destroyed. The normal procedures for handling State Cookies when
1628 * a TCB exists will resolve the duplicate INITs to a single association.
1630 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1631 * its Tie-Tags with the Tag information of itself and its peer (see
1632 * section 5.2.2 for a description of the Tie-Tags).
1634 * Verification Tag: Not explicit, but an INIT can not have a valid
1635 * verification tag, so we skip the check.
1638 * (endpoint, asoc, chunk)
1641 * (asoc, reply_msg, msg_up, timers, counters)
1643 * The return value is the disposition of the chunk.
1645 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1647 const struct sctp_endpoint *ep,
1648 const struct sctp_association *asoc,
1649 const union sctp_subtype type,
1651 struct sctp_cmd_seq *commands)
1653 /* Call helper to do the real work for both simulataneous and
1654 * duplicate INIT chunk handling.
1656 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1660 * Handle duplicated INIT messages. These are usually delayed
1663 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1664 * COOKIE-ECHOED and COOKIE-WAIT
1666 * Unless otherwise stated, upon reception of an unexpected INIT for
1667 * this association, the endpoint shall generate an INIT ACK with a
1668 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1669 * current Verification Tag and peer's Verification Tag into a reserved
1670 * place within the state cookie. We shall refer to these locations as
1671 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1672 * containing this INIT ACK MUST carry a Verification Tag value equal to
1673 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1674 * MUST contain a new Initiation Tag (randomly generated see Section
1675 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1676 * existing parameters of the association (e.g. number of outbound
1677 * streams) into the INIT ACK and cookie.
1679 * After sending out the INIT ACK, the endpoint shall take no further
1680 * actions, i.e., the existing association, including its current state,
1681 * and the corresponding TCB MUST NOT be changed.
1683 * Note: Only when a TCB exists and the association is not in a COOKIE-
1684 * WAIT state are the Tie-Tags populated. For a normal association INIT
1685 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1686 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1687 * State Cookie are populated as specified in section 5.2.1.
1689 * Verification Tag: Not specified, but an INIT has no way of knowing
1690 * what the verification tag could be, so we ignore it.
1693 * (endpoint, asoc, chunk)
1696 * (asoc, reply_msg, msg_up, timers, counters)
1698 * The return value is the disposition of the chunk.
1700 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1702 const struct sctp_endpoint *ep,
1703 const struct sctp_association *asoc,
1704 const union sctp_subtype type,
1706 struct sctp_cmd_seq *commands)
1708 /* Call helper to do the real work for both simulataneous and
1709 * duplicate INIT chunk handling.
1711 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1716 * Unexpected INIT-ACK handler.
1719 * If an INIT ACK received by an endpoint in any state other than the
1720 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1721 * An unexpected INIT ACK usually indicates the processing of an old or
1722 * duplicated INIT chunk.
1724 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1726 const struct sctp_endpoint *ep,
1727 const struct sctp_association *asoc,
1728 const union sctp_subtype type,
1730 struct sctp_cmd_seq *commands)
1732 /* Per the above section, we'll discard the chunk if we have an
1733 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1735 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1736 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1738 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1741 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1744 * A) In this case, the peer may have restarted.
1746 static enum sctp_disposition sctp_sf_do_dupcook_a(
1748 const struct sctp_endpoint *ep,
1749 const struct sctp_association *asoc,
1750 struct sctp_chunk *chunk,
1751 struct sctp_cmd_seq *commands,
1752 struct sctp_association *new_asoc)
1754 struct sctp_init_chunk *peer_init;
1755 enum sctp_disposition disposition;
1756 struct sctp_ulpevent *ev;
1757 struct sctp_chunk *repl;
1758 struct sctp_chunk *err;
1760 /* new_asoc is a brand-new association, so these are not yet
1761 * side effects--it is safe to run them here.
1763 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1765 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1769 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1770 return SCTP_DISPOSITION_DISCARD;
1772 /* Make sure no new addresses are being added during the
1773 * restart. Though this is a pretty complicated attack
1774 * since you'd have to get inside the cookie.
1776 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1777 return SCTP_DISPOSITION_CONSUME;
1779 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1780 * the peer has restarted (Action A), it MUST NOT setup a new
1781 * association but instead resend the SHUTDOWN ACK and send an ERROR
1782 * chunk with a "Cookie Received while Shutting Down" error cause to
1785 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1786 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1787 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1789 if (SCTP_DISPOSITION_NOMEM == disposition)
1792 err = sctp_make_op_error(asoc, chunk,
1793 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1796 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1799 return SCTP_DISPOSITION_CONSUME;
1802 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1803 * data. Consider the optional choice of resending of this data.
1805 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1806 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1807 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1808 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1810 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1811 * and ASCONF-ACK cache.
1813 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1814 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1815 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1817 repl = sctp_make_cookie_ack(new_asoc, chunk);
1821 /* Report association restart to upper layer. */
1822 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1823 new_asoc->c.sinit_num_ostreams,
1824 new_asoc->c.sinit_max_instreams,
1829 /* Update the content of current association. */
1830 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1831 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1832 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1833 sctp_state(asoc, SHUTDOWN_SENT)) &&
1834 (sctp_sstate(asoc->base.sk, CLOSING) ||
1835 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1836 /* If the socket has been closed by user, don't
1837 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1838 * bundled with COOKIE_ACK.
1840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1841 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1842 SCTP_ST_CHUNK(0), NULL,
1845 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1846 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1847 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1849 return SCTP_DISPOSITION_CONSUME;
1852 sctp_chunk_free(repl);
1854 return SCTP_DISPOSITION_NOMEM;
1857 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1860 * B) In this case, both sides may be attempting to start an association
1861 * at about the same time but the peer endpoint started its INIT
1862 * after responding to the local endpoint's INIT
1864 /* This case represents an initialization collision. */
1865 static enum sctp_disposition sctp_sf_do_dupcook_b(
1867 const struct sctp_endpoint *ep,
1868 const struct sctp_association *asoc,
1869 struct sctp_chunk *chunk,
1870 struct sctp_cmd_seq *commands,
1871 struct sctp_association *new_asoc)
1873 struct sctp_init_chunk *peer_init;
1874 struct sctp_chunk *repl;
1876 /* new_asoc is a brand-new association, so these are not yet
1877 * side effects--it is safe to run them here.
1879 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1880 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1884 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1885 return SCTP_DISPOSITION_DISCARD;
1887 /* Update the content of current association. */
1888 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1889 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1890 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1891 if (asoc->state < SCTP_STATE_ESTABLISHED)
1892 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1893 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1895 repl = sctp_make_cookie_ack(new_asoc, chunk);
1899 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1901 /* RFC 2960 5.1 Normal Establishment of an Association
1903 * D) IMPLEMENTATION NOTE: An implementation may choose to
1904 * send the Communication Up notification to the SCTP user
1905 * upon reception of a valid COOKIE ECHO chunk.
1907 * Sadly, this needs to be implemented as a side-effect, because
1908 * we are not guaranteed to have set the association id of the real
1909 * association and so these notifications need to be delayed until
1910 * the association id is allocated.
1913 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1915 /* Sockets API Draft Section 5.3.1.6
1916 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1917 * delivers this notification to inform the application that of the
1918 * peers requested adaptation layer.
1920 * This also needs to be done as a side effect for the same reason as
1923 if (asoc->peer.adaptation_ind)
1924 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1926 return SCTP_DISPOSITION_CONSUME;
1929 return SCTP_DISPOSITION_NOMEM;
1932 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1935 * C) In this case, the local endpoint's cookie has arrived late.
1936 * Before it arrived, the local endpoint sent an INIT and received an
1937 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1938 * but a new tag of its own.
1940 /* This case represents an initialization collision. */
1941 static enum sctp_disposition sctp_sf_do_dupcook_c(
1943 const struct sctp_endpoint *ep,
1944 const struct sctp_association *asoc,
1945 struct sctp_chunk *chunk,
1946 struct sctp_cmd_seq *commands,
1947 struct sctp_association *new_asoc)
1949 /* The cookie should be silently discarded.
1950 * The endpoint SHOULD NOT change states and should leave
1951 * any timers running.
1953 return SCTP_DISPOSITION_DISCARD;
1956 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1960 * D) When both local and remote tags match the endpoint should always
1961 * enter the ESTABLISHED state, if it has not already done so.
1963 /* This case represents an initialization collision. */
1964 static enum sctp_disposition sctp_sf_do_dupcook_d(
1966 const struct sctp_endpoint *ep,
1967 const struct sctp_association *asoc,
1968 struct sctp_chunk *chunk,
1969 struct sctp_cmd_seq *commands,
1970 struct sctp_association *new_asoc)
1972 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1973 struct sctp_chunk *repl;
1975 /* Clarification from Implementor's Guide:
1976 * D) When both local and remote tags match the endpoint should
1977 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1978 * It should stop any cookie timer that may be running and send
1982 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1983 return SCTP_DISPOSITION_DISCARD;
1985 /* Don't accidentally move back into established state. */
1986 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1987 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1988 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1989 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1990 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1991 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1992 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1995 /* RFC 2960 5.1 Normal Establishment of an Association
1997 * D) IMPLEMENTATION NOTE: An implementation may choose
1998 * to send the Communication Up notification to the
1999 * SCTP user upon reception of a valid COOKIE
2002 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2004 asoc->c.sinit_num_ostreams,
2005 asoc->c.sinit_max_instreams,
2010 /* Sockets API Draft Section 5.3.1.6
2011 * When a peer sends a Adaptation Layer Indication parameter,
2012 * SCTP delivers this notification to inform the application
2013 * that of the peers requested adaptation layer.
2015 if (asoc->peer.adaptation_ind) {
2016 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2024 repl = sctp_make_cookie_ack(asoc, chunk);
2028 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2031 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2034 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2035 SCTP_ULPEVENT(ai_ev));
2037 return SCTP_DISPOSITION_CONSUME;
2041 sctp_ulpevent_free(ai_ev);
2043 sctp_ulpevent_free(ev);
2044 return SCTP_DISPOSITION_NOMEM;
2048 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2049 * chunk was retransmitted and then delayed in the network.
2051 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2053 * Verification Tag: None. Do cookie validation.
2056 * (endpoint, asoc, chunk)
2059 * (asoc, reply_msg, msg_up, timers, counters)
2061 * The return value is the disposition of the chunk.
2063 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2065 const struct sctp_endpoint *ep,
2066 const struct sctp_association *asoc,
2067 const union sctp_subtype type,
2069 struct sctp_cmd_seq *commands)
2071 struct sctp_association *new_asoc;
2072 struct sctp_chunk *chunk = arg;
2073 enum sctp_disposition retval;
2074 struct sctp_chunk *err_chk_p;
2078 /* Make sure that the chunk has a valid length from the protocol
2079 * perspective. In this case check to make sure we have at least
2080 * enough for the chunk header. Cookie length verification is
2083 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2084 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2087 /* "Decode" the chunk. We have no optional parameters so we
2088 * are in good shape.
2090 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2091 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2092 sizeof(struct sctp_chunkhdr)))
2095 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2096 * of a duplicate COOKIE ECHO match the Verification Tags of the
2097 * current association, consider the State Cookie valid even if
2098 * the lifespan is exceeded.
2100 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2104 * If the re-build failed, what is the proper error path
2107 * [We should abort the association. --piggy]
2110 /* FIXME: Several errors are possible. A bad cookie should
2111 * be silently discarded, but think about logging it too.
2114 case -SCTP_IERROR_NOMEM:
2117 case -SCTP_IERROR_STALE_COOKIE:
2118 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2120 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2121 case -SCTP_IERROR_BAD_SIG:
2123 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2127 /* Set temp so that it won't be added into hashtable */
2130 /* Compare the tie_tag in cookie with the verification tag of
2131 * current association.
2133 action = sctp_tietags_compare(new_asoc, asoc);
2136 case 'A': /* Association restart. */
2137 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2141 case 'B': /* Collision case B. */
2142 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2146 case 'C': /* Collision case C. */
2147 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2151 case 'D': /* Collision case D. */
2152 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2156 default: /* Discard packet for all others. */
2157 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2161 /* Delete the tempory new association. */
2162 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2163 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2165 /* Restore association pointer to provide SCTP command interpeter
2166 * with a valid context in case it needs to manipulate
2168 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2169 SCTP_ASOC((struct sctp_association *)asoc));
2174 return SCTP_DISPOSITION_NOMEM;
2178 * Process an ABORT. (SHUTDOWN-PENDING state)
2180 * See sctp_sf_do_9_1_abort().
2182 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2184 const struct sctp_endpoint *ep,
2185 const struct sctp_association *asoc,
2186 const union sctp_subtype type,
2188 struct sctp_cmd_seq *commands)
2190 struct sctp_chunk *chunk = arg;
2192 if (!sctp_vtag_verify_either(chunk, asoc))
2193 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2195 /* Make sure that the ABORT chunk has a valid length.
2196 * Since this is an ABORT chunk, we have to discard it
2197 * because of the following text:
2198 * RFC 2960, Section 3.3.7
2199 * If an endpoint receives an ABORT with a format error or for an
2200 * association that doesn't exist, it MUST silently discard it.
2201 * Because the length is "invalid", we can't really discard just
2202 * as we do not know its true length. So, to be safe, discard the
2205 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2206 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2208 /* ADD-IP: Special case for ABORT chunks
2209 * F4) One special consideration is that ABORT Chunks arriving
2210 * destined to the IP address being deleted MUST be
2211 * ignored (see Section 5.3.1 for further details).
2213 if (SCTP_ADDR_DEL ==
2214 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2215 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2217 if (!sctp_err_chunk_valid(chunk))
2218 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2220 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2224 * Process an ABORT. (SHUTDOWN-SENT state)
2226 * See sctp_sf_do_9_1_abort().
2228 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2230 const struct sctp_endpoint *ep,
2231 const struct sctp_association *asoc,
2232 const union sctp_subtype type,
2234 struct sctp_cmd_seq *commands)
2236 struct sctp_chunk *chunk = arg;
2238 if (!sctp_vtag_verify_either(chunk, asoc))
2239 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2241 /* Make sure that the ABORT chunk has a valid length.
2242 * Since this is an ABORT chunk, we have to discard it
2243 * because of the following text:
2244 * RFC 2960, Section 3.3.7
2245 * If an endpoint receives an ABORT with a format error or for an
2246 * association that doesn't exist, it MUST silently discard it.
2247 * Because the length is "invalid", we can't really discard just
2248 * as we do not know its true length. So, to be safe, discard the
2251 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2252 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2254 /* ADD-IP: Special case for ABORT chunks
2255 * F4) One special consideration is that ABORT Chunks arriving
2256 * destined to the IP address being deleted MUST be
2257 * ignored (see Section 5.3.1 for further details).
2259 if (SCTP_ADDR_DEL ==
2260 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2261 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2263 if (!sctp_err_chunk_valid(chunk))
2264 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2266 /* Stop the T2-shutdown timer. */
2267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2268 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2270 /* Stop the T5-shutdown guard timer. */
2271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2272 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2274 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2278 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2280 * See sctp_sf_do_9_1_abort().
2282 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2284 const struct sctp_endpoint *ep,
2285 const struct sctp_association *asoc,
2286 const union sctp_subtype type,
2288 struct sctp_cmd_seq *commands)
2290 /* The same T2 timer, so we should be able to use
2291 * common function with the SHUTDOWN-SENT state.
2293 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2297 * Handle an Error received in COOKIE_ECHOED state.
2299 * Only handle the error type of stale COOKIE Error, the other errors will
2303 * (endpoint, asoc, chunk)
2306 * (asoc, reply_msg, msg_up, timers, counters)
2308 * The return value is the disposition of the chunk.
2310 enum sctp_disposition sctp_sf_cookie_echoed_err(
2312 const struct sctp_endpoint *ep,
2313 const struct sctp_association *asoc,
2314 const union sctp_subtype type,
2316 struct sctp_cmd_seq *commands)
2318 struct sctp_chunk *chunk = arg;
2319 struct sctp_errhdr *err;
2321 if (!sctp_vtag_verify(chunk, asoc))
2322 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2324 /* Make sure that the ERROR chunk has a valid length.
2325 * The parameter walking depends on this as well.
2327 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2328 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2331 /* Process the error here */
2332 /* FUTURE FIXME: When PR-SCTP related and other optional
2333 * parms are emitted, this will have to change to handle multiple
2336 sctp_walk_errors(err, chunk->chunk_hdr) {
2337 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2338 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2342 /* It is possible to have malformed error causes, and that
2343 * will cause us to end the walk early. However, since
2344 * we are discarding the packet, there should be no adverse
2347 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2351 * Handle a Stale COOKIE Error
2353 * Section: 5.2.6 Handle Stale COOKIE Error
2354 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2355 * one of the following three alternatives.
2357 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2358 * Preservative parameter requesting an extension to the lifetime of
2359 * the State Cookie. When calculating the time extension, an
2360 * implementation SHOULD use the RTT information measured based on the
2361 * previous COOKIE ECHO / ERROR exchange, and should add no more
2362 * than 1 second beyond the measured RTT, due to long State Cookie
2363 * lifetimes making the endpoint more subject to a replay attack.
2365 * Verification Tag: Not explicit, but safe to ignore.
2368 * (endpoint, asoc, chunk)
2371 * (asoc, reply_msg, msg_up, timers, counters)
2373 * The return value is the disposition of the chunk.
2375 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2377 const struct sctp_endpoint *ep,
2378 const struct sctp_association *asoc,
2379 const union sctp_subtype type,
2381 struct sctp_cmd_seq *commands)
2383 int attempts = asoc->init_err_counter + 1;
2384 struct sctp_chunk *chunk = arg, *reply;
2385 struct sctp_cookie_preserve_param bht;
2386 struct sctp_bind_addr *bp;
2387 struct sctp_errhdr *err;
2390 if (attempts > asoc->max_init_attempts) {
2391 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2392 SCTP_ERROR(ETIMEDOUT));
2393 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2394 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2395 return SCTP_DISPOSITION_DELETE_TCB;
2398 err = (struct sctp_errhdr *)(chunk->skb->data);
2400 /* When calculating the time extension, an implementation
2401 * SHOULD use the RTT information measured based on the
2402 * previous COOKIE ECHO / ERROR exchange, and should add no
2403 * more than 1 second beyond the measured RTT, due to long
2404 * State Cookie lifetimes making the endpoint more subject to
2406 * Measure of Staleness's unit is usec. (1/1000000 sec)
2407 * Suggested Cookie Life-span Increment's unit is msec.
2409 * In general, if you use the suggested cookie life, the value
2410 * found in the field of measure of staleness should be doubled
2411 * to give ample time to retransmit the new cookie and thus
2412 * yield a higher probability of success on the reattempt.
2414 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2415 stale = (stale * 2) / 1000;
2417 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2418 bht.param_hdr.length = htons(sizeof(bht));
2419 bht.lifespan_increment = htonl(stale);
2421 /* Build that new INIT chunk. */
2422 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2423 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2427 sctp_addto_chunk(reply, sizeof(bht), &bht);
2429 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2430 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2432 /* Stop pending T3-rtx and heartbeat timers */
2433 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2434 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2436 /* Delete non-primary peer ip addresses since we are transitioning
2437 * back to the COOKIE-WAIT state
2439 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2441 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2444 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2445 SCTP_TRANSPORT(asoc->peer.primary_path));
2447 /* Cast away the const modifier, as we want to just
2448 * rerun it through as a sideffect.
2450 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2452 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2453 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2454 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2455 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2456 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2457 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2461 return SCTP_DISPOSITION_CONSUME;
2464 return SCTP_DISPOSITION_NOMEM;
2471 * After checking the Verification Tag, the receiving endpoint shall
2472 * remove the association from its record, and shall report the
2473 * termination to its upper layer.
2475 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2476 * B) Rules for packet carrying ABORT:
2478 * - The endpoint shall always fill in the Verification Tag field of the
2479 * outbound packet with the destination endpoint's tag value if it
2482 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2483 * MUST follow the procedure described in Section 8.4.
2485 * - The receiver MUST accept the packet if the Verification Tag
2486 * matches either its own tag, OR the tag of its peer. Otherwise, the
2487 * receiver MUST silently discard the packet and take no further
2491 * (endpoint, asoc, chunk)
2494 * (asoc, reply_msg, msg_up, timers, counters)
2496 * The return value is the disposition of the chunk.
2498 enum sctp_disposition sctp_sf_do_9_1_abort(
2500 const struct sctp_endpoint *ep,
2501 const struct sctp_association *asoc,
2502 const union sctp_subtype type,
2504 struct sctp_cmd_seq *commands)
2506 struct sctp_chunk *chunk = arg;
2508 if (!sctp_vtag_verify_either(chunk, asoc))
2509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2511 /* Make sure that the ABORT chunk has a valid length.
2512 * Since this is an ABORT chunk, we have to discard it
2513 * because of the following text:
2514 * RFC 2960, Section 3.3.7
2515 * If an endpoint receives an ABORT with a format error or for an
2516 * association that doesn't exist, it MUST silently discard it.
2517 * Because the length is "invalid", we can't really discard just
2518 * as we do not know its true length. So, to be safe, discard the
2521 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2522 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2524 /* ADD-IP: Special case for ABORT chunks
2525 * F4) One special consideration is that ABORT Chunks arriving
2526 * destined to the IP address being deleted MUST be
2527 * ignored (see Section 5.3.1 for further details).
2529 if (SCTP_ADDR_DEL ==
2530 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2531 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2533 if (!sctp_err_chunk_valid(chunk))
2534 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2536 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2539 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2541 const struct sctp_endpoint *ep,
2542 const struct sctp_association *asoc,
2543 const union sctp_subtype type,
2545 struct sctp_cmd_seq *commands)
2547 __be16 error = SCTP_ERROR_NO_ERROR;
2548 struct sctp_chunk *chunk = arg;
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 = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2556 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2557 /* ASSOC_FAILED will DELETE_TCB. */
2558 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2559 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2560 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2562 return SCTP_DISPOSITION_ABORT;
2566 * Process an ABORT. (COOKIE-WAIT state)
2568 * See sctp_sf_do_9_1_abort() above.
2570 enum sctp_disposition sctp_sf_cookie_wait_abort(
2572 const struct sctp_endpoint *ep,
2573 const struct sctp_association *asoc,
2574 const union sctp_subtype type,
2576 struct sctp_cmd_seq *commands)
2578 __be16 error = SCTP_ERROR_NO_ERROR;
2579 struct sctp_chunk *chunk = arg;
2582 if (!sctp_vtag_verify_either(chunk, asoc))
2583 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2585 /* Make sure that the ABORT chunk has a valid length.
2586 * Since this is an ABORT chunk, we have to discard it
2587 * because of the following text:
2588 * RFC 2960, Section 3.3.7
2589 * If an endpoint receives an ABORT with a format error or for an
2590 * association that doesn't exist, it MUST silently discard it.
2591 * Because the length is "invalid", we can't really discard just
2592 * as we do not know its true length. So, to be safe, discard the
2595 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2596 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2598 /* See if we have an error cause code in the chunk. */
2599 len = ntohs(chunk->chunk_hdr->length);
2600 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2601 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2603 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2608 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2610 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2612 const struct sctp_endpoint *ep,
2613 const struct sctp_association *asoc,
2614 const union sctp_subtype type,
2616 struct sctp_cmd_seq *commands)
2618 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2620 (struct sctp_transport *)arg);
2624 * Process an ABORT. (COOKIE-ECHOED state)
2626 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2628 const struct sctp_endpoint *ep,
2629 const struct sctp_association *asoc,
2630 const union sctp_subtype type,
2632 struct sctp_cmd_seq *commands)
2634 /* There is a single T1 timer, so we should be able to use
2635 * common function with the COOKIE-WAIT state.
2637 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2641 * Stop T1 timer and abort association with "INIT failed".
2643 * This is common code called by several sctp_sf_*_abort() functions above.
2645 static enum sctp_disposition sctp_stop_t1_and_abort(
2647 struct sctp_cmd_seq *commands,
2648 __be16 error, int sk_err,
2649 const struct sctp_association *asoc,
2650 struct sctp_transport *transport)
2652 pr_debug("%s: ABORT received (INIT)\n", __func__);
2654 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2655 SCTP_STATE(SCTP_STATE_CLOSED));
2656 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2657 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2658 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2659 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2660 /* CMD_INIT_FAILED will DELETE_TCB. */
2661 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2664 return SCTP_DISPOSITION_ABORT;
2668 * sctp_sf_do_9_2_shut
2671 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2672 * - enter the SHUTDOWN-RECEIVED state,
2674 * - stop accepting new data from its SCTP user
2676 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2677 * that all its outstanding DATA chunks have been received by the
2680 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2681 * send a SHUTDOWN in response to a ULP request. And should discard
2682 * subsequent SHUTDOWN chunks.
2684 * If there are still outstanding DATA chunks left, the SHUTDOWN
2685 * receiver shall continue to follow normal data transmission
2686 * procedures defined in Section 6 until all outstanding DATA chunks
2687 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2688 * new data from its SCTP user.
2690 * Verification Tag: 8.5 Verification Tag [Normal verification]
2693 * (endpoint, asoc, chunk)
2696 * (asoc, reply_msg, msg_up, timers, counters)
2698 * The return value is the disposition of the chunk.
2700 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2702 const struct sctp_endpoint *ep,
2703 const struct sctp_association *asoc,
2704 const union sctp_subtype type,
2706 struct sctp_cmd_seq *commands)
2708 enum sctp_disposition disposition;
2709 struct sctp_chunk *chunk = arg;
2710 struct sctp_shutdownhdr *sdh;
2711 struct sctp_ulpevent *ev;
2714 if (!sctp_vtag_verify(chunk, asoc))
2715 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2717 /* Make sure that the SHUTDOWN chunk has a valid length. */
2718 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2719 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2722 /* Convert the elaborate header. */
2723 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2724 skb_pull(chunk->skb, sizeof(*sdh));
2725 chunk->subh.shutdown_hdr = sdh;
2726 ctsn = ntohl(sdh->cum_tsn_ack);
2728 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2729 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2730 asoc->ctsn_ack_point);
2732 return SCTP_DISPOSITION_DISCARD;
2735 /* If Cumulative TSN Ack beyond the max tsn currently
2736 * send, terminating the association and respond to the
2737 * sender with an ABORT.
2739 if (!TSN_lt(ctsn, asoc->next_tsn))
2740 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2742 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2743 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2744 * inform the application that it should cease sending data.
2746 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2748 disposition = SCTP_DISPOSITION_NOMEM;
2751 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2753 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2754 * - enter the SHUTDOWN-RECEIVED state,
2755 * - stop accepting new data from its SCTP user
2757 * [This is implicit in the new state.]
2759 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2760 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2761 disposition = SCTP_DISPOSITION_CONSUME;
2763 if (sctp_outq_is_empty(&asoc->outqueue)) {
2764 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2768 if (SCTP_DISPOSITION_NOMEM == disposition)
2771 /* - verify, by checking the Cumulative TSN Ack field of the
2772 * chunk, that all its outstanding DATA chunks have been
2773 * received by the SHUTDOWN sender.
2775 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2776 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2783 * sctp_sf_do_9_2_shut_ctsn
2785 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2786 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2787 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2788 * MUST be processed.
2790 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2792 const struct sctp_endpoint *ep,
2793 const struct sctp_association *asoc,
2794 const union sctp_subtype type,
2796 struct sctp_cmd_seq *commands)
2798 struct sctp_chunk *chunk = arg;
2799 struct sctp_shutdownhdr *sdh;
2802 if (!sctp_vtag_verify(chunk, asoc))
2803 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2805 /* Make sure that the SHUTDOWN chunk has a valid length. */
2806 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2807 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2810 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2811 ctsn = ntohl(sdh->cum_tsn_ack);
2813 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2814 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2815 asoc->ctsn_ack_point);
2817 return SCTP_DISPOSITION_DISCARD;
2820 /* If Cumulative TSN Ack beyond the max tsn currently
2821 * send, terminating the association and respond to the
2822 * sender with an ABORT.
2824 if (!TSN_lt(ctsn, asoc->next_tsn))
2825 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2827 /* verify, by checking the Cumulative TSN Ack field of the
2828 * chunk, that all its outstanding DATA chunks have been
2829 * received by the SHUTDOWN sender.
2831 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2832 SCTP_BE32(sdh->cum_tsn_ack));
2834 return SCTP_DISPOSITION_CONSUME;
2838 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2839 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2840 * transport addresses (either in the IP addresses or in the INIT chunk)
2841 * that belong to this association, it should discard the INIT chunk and
2842 * retransmit the SHUTDOWN ACK chunk.
2844 enum sctp_disposition sctp_sf_do_9_2_reshutack(
2846 const struct sctp_endpoint *ep,
2847 const struct sctp_association *asoc,
2848 const union sctp_subtype type,
2850 struct sctp_cmd_seq *commands)
2852 struct sctp_chunk *chunk = arg;
2853 struct sctp_chunk *reply;
2855 /* Make sure that the chunk has a valid length */
2856 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2857 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2860 /* Since we are not going to really process this INIT, there
2861 * is no point in verifying chunk boundries. Just generate
2864 reply = sctp_make_shutdown_ack(asoc, chunk);
2868 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2869 * the T2-SHUTDOWN timer.
2871 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2873 /* and restart the T2-shutdown timer. */
2874 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2875 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2877 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2879 return SCTP_DISPOSITION_CONSUME;
2881 return SCTP_DISPOSITION_NOMEM;
2885 * sctp_sf_do_ecn_cwr
2887 * Section: Appendix A: Explicit Congestion Notification
2891 * RFC 2481 details a specific bit for a sender to send in the header of
2892 * its next outbound TCP segment to indicate to its peer that it has
2893 * reduced its congestion window. This is termed the CWR bit. For
2894 * SCTP the same indication is made by including the CWR chunk.
2895 * This chunk contains one data element, i.e. the TSN number that
2896 * was sent in the ECNE chunk. This element represents the lowest
2897 * TSN number in the datagram that was originally marked with the
2900 * Verification Tag: 8.5 Verification Tag [Normal verification]
2902 * (endpoint, asoc, chunk)
2905 * (asoc, reply_msg, msg_up, timers, counters)
2907 * The return value is the disposition of the chunk.
2909 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2910 const struct sctp_endpoint *ep,
2911 const struct sctp_association *asoc,
2912 const union sctp_subtype type,
2914 struct sctp_cmd_seq *commands)
2916 struct sctp_chunk *chunk = arg;
2917 struct sctp_cwrhdr *cwr;
2920 if (!sctp_vtag_verify(chunk, asoc))
2921 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2923 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
2924 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2927 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2928 skb_pull(chunk->skb, sizeof(*cwr));
2930 lowest_tsn = ntohl(cwr->lowest_tsn);
2932 /* Does this CWR ack the last sent congestion notification? */
2933 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2934 /* Stop sending ECNE. */
2935 sctp_add_cmd_sf(commands,
2937 SCTP_U32(lowest_tsn));
2939 return SCTP_DISPOSITION_CONSUME;
2945 * Section: Appendix A: Explicit Congestion Notification
2949 * RFC 2481 details a specific bit for a receiver to send back in its
2950 * TCP acknowledgements to notify the sender of the Congestion
2951 * Experienced (CE) bit having arrived from the network. For SCTP this
2952 * same indication is made by including the ECNE chunk. This chunk
2953 * contains one data element, i.e. the lowest TSN associated with the IP
2954 * datagram marked with the CE bit.....
2956 * Verification Tag: 8.5 Verification Tag [Normal verification]
2958 * (endpoint, asoc, chunk)
2961 * (asoc, reply_msg, msg_up, timers, counters)
2963 * The return value is the disposition of the chunk.
2965 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
2966 const struct sctp_endpoint *ep,
2967 const struct sctp_association *asoc,
2968 const union sctp_subtype type,
2969 void *arg, struct sctp_cmd_seq *commands)
2971 struct sctp_chunk *chunk = arg;
2972 struct sctp_ecnehdr *ecne;
2974 if (!sctp_vtag_verify(chunk, asoc))
2975 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2977 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
2978 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2981 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
2982 skb_pull(chunk->skb, sizeof(*ecne));
2984 /* If this is a newer ECNE than the last CWR packet we sent out */
2985 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2986 SCTP_U32(ntohl(ecne->lowest_tsn)));
2988 return SCTP_DISPOSITION_CONSUME;
2992 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2994 * The SCTP endpoint MUST always acknowledge the reception of each valid
2997 * The guidelines on delayed acknowledgement algorithm specified in
2998 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2999 * acknowledgement SHOULD be generated for at least every second packet
3000 * (not every second DATA chunk) received, and SHOULD be generated within
3001 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3002 * situations it may be beneficial for an SCTP transmitter to be more
3003 * conservative than the algorithms detailed in this document allow.
3004 * However, an SCTP transmitter MUST NOT be more aggressive than the
3005 * following algorithms allow.
3007 * A SCTP receiver MUST NOT generate more than one SACK for every
3008 * incoming packet, other than to update the offered window as the
3009 * receiving application consumes new data.
3011 * Verification Tag: 8.5 Verification Tag [Normal verification]
3014 * (endpoint, asoc, chunk)
3017 * (asoc, reply_msg, msg_up, timers, counters)
3019 * The return value is the disposition of the chunk.
3021 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3022 const struct sctp_endpoint *ep,
3023 const struct sctp_association *asoc,
3024 const union sctp_subtype type,
3026 struct sctp_cmd_seq *commands)
3028 union sctp_arg force = SCTP_NOFORCE();
3029 struct sctp_chunk *chunk = arg;
3032 if (!sctp_vtag_verify(chunk, asoc)) {
3033 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3035 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3038 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
3039 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3042 error = sctp_eat_data(asoc, chunk, commands);
3044 case SCTP_IERROR_NO_ERROR:
3046 case SCTP_IERROR_HIGH_TSN:
3047 case SCTP_IERROR_BAD_STREAM:
3048 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3049 goto discard_noforce;
3050 case SCTP_IERROR_DUP_TSN:
3051 case SCTP_IERROR_IGNORE_TSN:
3052 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3054 case SCTP_IERROR_NO_DATA:
3055 return SCTP_DISPOSITION_ABORT;
3056 case SCTP_IERROR_PROTO_VIOLATION:
3057 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3058 (u8 *)chunk->subh.data_hdr,
3059 sizeof(struct sctp_datahdr));
3064 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3065 force = SCTP_FORCE();
3067 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3068 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3069 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3072 /* If this is the last chunk in a packet, we need to count it
3073 * toward sack generation. Note that we need to SACK every
3074 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3075 * THEM. We elect to NOT generate SACK's if the chunk fails
3076 * the verification tag test.
3078 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3080 * The SCTP endpoint MUST always acknowledge the reception of
3081 * each valid DATA chunk.
3083 * The guidelines on delayed acknowledgement algorithm
3084 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3085 * Specifically, an acknowledgement SHOULD be generated for at
3086 * least every second packet (not every second DATA chunk)
3087 * received, and SHOULD be generated within 200 ms of the
3088 * arrival of any unacknowledged DATA chunk. In some
3089 * situations it may be beneficial for an SCTP transmitter to
3090 * be more conservative than the algorithms detailed in this
3091 * document allow. However, an SCTP transmitter MUST NOT be
3092 * more aggressive than the following algorithms allow.
3094 if (chunk->end_of_packet)
3095 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3097 return SCTP_DISPOSITION_CONSUME;
3100 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3102 * When a packet arrives with duplicate DATA chunk(s) and with
3103 * no new DATA chunk(s), the endpoint MUST immediately send a
3104 * SACK with no delay. If a packet arrives with duplicate
3105 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3106 * MAY immediately send a SACK. Normally receipt of duplicate
3107 * DATA chunks will occur when the original SACK chunk was lost
3108 * and the peer's RTO has expired. The duplicate TSN number(s)
3109 * SHOULD be reported in the SACK as duplicate.
3111 /* In our case, we split the MAY SACK advice up whether or not
3112 * the last chunk is a duplicate.'
3114 if (chunk->end_of_packet)
3115 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3116 return SCTP_DISPOSITION_DISCARD;
3119 if (chunk->end_of_packet)
3120 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3122 return SCTP_DISPOSITION_DISCARD;
3126 * sctp_sf_eat_data_fast_4_4
3129 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3130 * DATA chunks without delay.
3132 * Verification Tag: 8.5 Verification Tag [Normal verification]
3134 * (endpoint, asoc, chunk)
3137 * (asoc, reply_msg, msg_up, timers, counters)
3139 * The return value is the disposition of the chunk.
3141 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3143 const struct sctp_endpoint *ep,
3144 const struct sctp_association *asoc,
3145 const union sctp_subtype type,
3147 struct sctp_cmd_seq *commands)
3149 struct sctp_chunk *chunk = arg;
3152 if (!sctp_vtag_verify(chunk, asoc)) {
3153 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3155 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3158 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
3159 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3162 error = sctp_eat_data(asoc, chunk, commands);
3164 case SCTP_IERROR_NO_ERROR:
3165 case SCTP_IERROR_HIGH_TSN:
3166 case SCTP_IERROR_DUP_TSN:
3167 case SCTP_IERROR_IGNORE_TSN:
3168 case SCTP_IERROR_BAD_STREAM:
3170 case SCTP_IERROR_NO_DATA:
3171 return SCTP_DISPOSITION_ABORT;
3172 case SCTP_IERROR_PROTO_VIOLATION:
3173 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3174 (u8 *)chunk->subh.data_hdr,
3175 sizeof(struct sctp_datahdr));
3180 /* Go a head and force a SACK, since we are shutting down. */
3182 /* Implementor's Guide.
3184 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3185 * respond to each received packet containing one or more DATA chunk(s)
3186 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3188 if (chunk->end_of_packet) {
3189 /* We must delay the chunk creation since the cumulative
3190 * TSN has not been updated yet.
3192 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3193 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3194 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3195 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3198 return SCTP_DISPOSITION_CONSUME;
3202 * Section: 6.2 Processing a Received SACK
3203 * D) Any time a SACK arrives, the endpoint performs the following:
3205 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3206 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3207 * increasing, a SACK whose Cumulative TSN Ack is less than the
3208 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3210 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3211 * of bytes still outstanding after processing the Cumulative TSN Ack
3212 * and the Gap Ack Blocks.
3214 * iii) If the SACK is missing a TSN that was previously
3215 * acknowledged via a Gap Ack Block (e.g., the data receiver
3216 * reneged on the data), then mark the corresponding DATA chunk
3217 * as available for retransmit: Mark it as missing for fast
3218 * retransmit as described in Section 7.2.4 and if no retransmit
3219 * timer is running for the destination address to which the DATA
3220 * chunk was originally transmitted, then T3-rtx is started for
3221 * that destination address.
3223 * Verification Tag: 8.5 Verification Tag [Normal verification]
3226 * (endpoint, asoc, chunk)
3229 * (asoc, reply_msg, msg_up, timers, counters)
3231 * The return value is the disposition of the chunk.
3233 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3234 const struct sctp_endpoint *ep,
3235 const struct sctp_association *asoc,
3236 const union sctp_subtype type,
3238 struct sctp_cmd_seq *commands)
3240 struct sctp_chunk *chunk = arg;
3241 struct sctp_sackhdr *sackh;
3244 if (!sctp_vtag_verify(chunk, asoc))
3245 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3247 /* Make sure that the SACK chunk has a valid length. */
3248 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3249 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3252 /* Pull the SACK chunk from the data buffer */
3253 sackh = sctp_sm_pull_sack(chunk);
3254 /* Was this a bogus SACK? */
3256 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3257 chunk->subh.sack_hdr = sackh;
3258 ctsn = ntohl(sackh->cum_tsn_ack);
3260 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3261 * Ack Point, then drop the SACK. Since Cumulative TSN
3262 * Ack is monotonically increasing, a SACK whose
3263 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3264 * Point indicates an out-of-order SACK.
3266 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3267 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3268 asoc->ctsn_ack_point);
3270 return SCTP_DISPOSITION_DISCARD;
3273 /* If Cumulative TSN Ack beyond the max tsn currently
3274 * send, terminating the association and respond to the
3275 * sender with an ABORT.
3277 if (!TSN_lt(ctsn, asoc->next_tsn))
3278 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3280 /* Return this SACK for further processing. */
3281 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3283 /* Note: We do the rest of the work on the PROCESS_SACK
3286 return SCTP_DISPOSITION_CONSUME;
3290 * Generate an ABORT in response to a packet.
3292 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3294 * 8) The receiver should respond to the sender of the OOTB packet with
3295 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3296 * MUST fill in the Verification Tag field of the outbound packet
3297 * with the value found in the Verification Tag field of the OOTB
3298 * packet and set the T-bit in the Chunk Flags to indicate that the
3299 * Verification Tag is reflected. After sending this ABORT, the
3300 * receiver of the OOTB packet shall discard the OOTB packet and take
3301 * no further action.
3305 * The return value is the disposition of the chunk.
3307 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3309 const struct sctp_endpoint *ep,
3310 const struct sctp_association *asoc,
3311 const union sctp_subtype type,
3313 struct sctp_cmd_seq *commands)
3315 struct sctp_packet *packet = NULL;
3316 struct sctp_chunk *chunk = arg;
3317 struct sctp_chunk *abort;
3319 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3321 return SCTP_DISPOSITION_NOMEM;
3323 /* Make an ABORT. The T bit will be set if the asoc
3326 abort = sctp_make_abort(asoc, chunk, 0);
3328 sctp_ootb_pkt_free(packet);
3329 return SCTP_DISPOSITION_NOMEM;
3332 /* Reflect vtag if T-Bit is set */
3333 if (sctp_test_T_bit(abort))
3334 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3336 /* Set the skb to the belonging sock for accounting. */
3337 abort->skb->sk = ep->base.sk;
3339 sctp_packet_append_chunk(packet, abort);
3341 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3342 SCTP_PACKET(packet));
3344 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3346 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3347 return SCTP_DISPOSITION_CONSUME;
3351 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3352 * event as ULP notification for each cause included in the chunk.
3354 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3356 * The return value is the disposition of the chunk.
3358 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3359 const struct sctp_endpoint *ep,
3360 const struct sctp_association *asoc,
3361 const union sctp_subtype type,
3363 struct sctp_cmd_seq *commands)
3365 struct sctp_chunk *chunk = arg;
3366 struct sctp_errhdr *err;
3368 if (!sctp_vtag_verify(chunk, asoc))
3369 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3371 /* Make sure that the ERROR chunk has a valid length. */
3372 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3373 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3375 sctp_walk_errors(err, chunk->chunk_hdr);
3376 if ((void *)err != (void *)chunk->chunk_end)
3377 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3378 (void *)err, commands);
3380 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3383 return SCTP_DISPOSITION_CONSUME;
3387 * Process an inbound SHUTDOWN ACK.
3390 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3391 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3392 * peer, and remove all record of the association.
3394 * The return value is the disposition.
3396 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3397 const struct sctp_endpoint *ep,
3398 const struct sctp_association *asoc,
3399 const union sctp_subtype type,
3401 struct sctp_cmd_seq *commands)
3403 struct sctp_chunk *chunk = arg;
3404 struct sctp_chunk *reply;
3405 struct sctp_ulpevent *ev;
3407 if (!sctp_vtag_verify(chunk, asoc))
3408 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3410 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3411 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3412 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3414 /* 10.2 H) SHUTDOWN COMPLETE notification
3416 * When SCTP completes the shutdown procedures (section 9.2) this
3417 * notification is passed to the upper layer.
3419 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3420 0, 0, 0, NULL, GFP_ATOMIC);
3424 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3425 reply = sctp_make_shutdown_complete(asoc, chunk);
3429 /* Do all the commands now (after allocation), so that we
3430 * have consistent state if memory allocation failes
3432 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3434 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3435 * stop the T2-shutdown timer,
3437 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3438 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3440 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3441 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3443 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3444 SCTP_STATE(SCTP_STATE_CLOSED));
3445 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3446 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3447 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3449 /* ...and remove all record of the association. */
3450 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3451 return SCTP_DISPOSITION_DELETE_TCB;
3454 sctp_ulpevent_free(ev);
3456 return SCTP_DISPOSITION_NOMEM;
3460 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3462 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3463 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3464 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3465 * packet must fill in the Verification Tag field of the outbound
3466 * packet with the Verification Tag received in the SHUTDOWN ACK and
3467 * set the T-bit in the Chunk Flags to indicate that the Verification
3470 * 8) The receiver should respond to the sender of the OOTB packet with
3471 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3472 * MUST fill in the Verification Tag field of the outbound packet
3473 * with the value found in the Verification Tag field of the OOTB
3474 * packet and set the T-bit in the Chunk Flags to indicate that the
3475 * Verification Tag is reflected. After sending this ABORT, the
3476 * receiver of the OOTB packet shall discard the OOTB packet and take
3477 * no further action.
3479 enum sctp_disposition sctp_sf_ootb(struct net *net,
3480 const struct sctp_endpoint *ep,
3481 const struct sctp_association *asoc,
3482 const union sctp_subtype type,
3483 void *arg, struct sctp_cmd_seq *commands)
3485 struct sctp_chunk *chunk = arg;
3486 struct sk_buff *skb = chunk->skb;
3487 struct sctp_chunkhdr *ch;
3488 struct sctp_errhdr *err;
3489 int ootb_cookie_ack = 0;
3490 int ootb_shut_ack = 0;
3493 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3495 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3497 /* Report violation if the chunk is less then minimal */
3498 if (ntohs(ch->length) < sizeof(*ch))
3499 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3502 /* Report violation if chunk len overflows */
3503 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3504 if (ch_end > skb_tail_pointer(skb))
3505 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3508 /* Now that we know we at least have a chunk header,
3509 * do things that are type appropriate.
3511 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3514 /* RFC 2960, Section 3.3.7
3515 * Moreover, under any circumstances, an endpoint that
3516 * receives an ABORT MUST NOT respond to that ABORT by
3517 * sending an ABORT of its own.
3519 if (SCTP_CID_ABORT == ch->type)
3520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3522 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3523 * or a COOKIE ACK the SCTP Packet should be silently
3527 if (SCTP_CID_COOKIE_ACK == ch->type)
3528 ootb_cookie_ack = 1;
3530 if (SCTP_CID_ERROR == ch->type) {
3531 sctp_walk_errors(err, ch) {
3532 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3533 ootb_cookie_ack = 1;
3539 ch = (struct sctp_chunkhdr *)ch_end;
3540 } while (ch_end < skb_tail_pointer(skb));
3543 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3544 else if (ootb_cookie_ack)
3545 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3547 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3551 * Handle an "Out of the blue" SHUTDOWN ACK.
3553 * Section: 8.4 5, sctpimpguide 2.41.
3555 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3556 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3557 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3558 * packet must fill in the Verification Tag field of the outbound
3559 * packet with the Verification Tag received in the SHUTDOWN ACK and
3560 * set the T-bit in the Chunk Flags to indicate that the Verification
3564 * (endpoint, asoc, type, arg, commands)
3567 * (enum sctp_disposition)
3569 * The return value is the disposition of the chunk.
3571 static enum sctp_disposition sctp_sf_shut_8_4_5(
3573 const struct sctp_endpoint *ep,
3574 const struct sctp_association *asoc,
3575 const union sctp_subtype type,
3577 struct sctp_cmd_seq *commands)
3579 struct sctp_packet *packet = NULL;
3580 struct sctp_chunk *chunk = arg;
3581 struct sctp_chunk *shut;
3583 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3585 return SCTP_DISPOSITION_NOMEM;
3587 /* Make an SHUTDOWN_COMPLETE.
3588 * The T bit will be set if the asoc is NULL.
3590 shut = sctp_make_shutdown_complete(asoc, chunk);
3592 sctp_ootb_pkt_free(packet);
3593 return SCTP_DISPOSITION_NOMEM;
3596 /* Reflect vtag if T-Bit is set */
3597 if (sctp_test_T_bit(shut))
3598 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3600 /* Set the skb to the belonging sock for accounting. */
3601 shut->skb->sk = ep->base.sk;
3603 sctp_packet_append_chunk(packet, shut);
3605 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3606 SCTP_PACKET(packet));
3608 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3610 /* If the chunk length is invalid, we don't want to process
3611 * the reset of the packet.
3613 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3614 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3616 /* We need to discard the rest of the packet to prevent
3617 * potential bomming attacks from additional bundled chunks.
3618 * This is documented in SCTP Threats ID.
3620 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3624 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3626 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3627 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3628 * procedures in section 8.4 SHOULD be followed, in other words it
3629 * should be treated as an Out Of The Blue packet.
3630 * [This means that we do NOT check the Verification Tag on these
3634 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3635 const struct sctp_endpoint *ep,
3636 const struct sctp_association *asoc,
3637 const union sctp_subtype type,
3639 struct sctp_cmd_seq *commands)
3641 struct sctp_chunk *chunk = arg;
3643 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3644 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3645 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3648 /* Although we do have an association in this case, it corresponds
3649 * to a restarted association. So the packet is treated as an OOTB
3650 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3651 * called with a NULL association.
3653 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3655 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3658 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3659 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3660 const struct sctp_endpoint *ep,
3661 const struct sctp_association *asoc,
3662 const union sctp_subtype type,
3664 struct sctp_cmd_seq *commands)
3666 struct sctp_paramhdr *err_param = NULL;
3667 struct sctp_chunk *asconf_ack = NULL;
3668 struct sctp_chunk *chunk = arg;
3669 struct sctp_addiphdr *hdr;
3672 if (!sctp_vtag_verify(chunk, asoc)) {
3673 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3678 /* ADD-IP: Section 4.1.1
3679 * This chunk MUST be sent in an authenticated way by using
3680 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3681 * is received unauthenticated it MUST be silently discarded as
3682 * described in [I-D.ietf-tsvwg-sctp-auth].
3684 if (!net->sctp.addip_noauth && !chunk->auth)
3685 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3688 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3689 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3690 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3693 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3694 serial = ntohl(hdr->serial);
3696 /* Verify the ASCONF chunk before processing it. */
3697 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3698 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3699 (void *)err_param, commands);
3701 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3702 * the endpoint stored in a new association variable
3703 * 'Peer-Serial-Number'.
3705 if (serial == asoc->peer.addip_serial + 1) {
3706 /* If this is the first instance of ASCONF in the packet,
3707 * we can clean our old ASCONF-ACKs.
3709 if (!chunk->has_asconf)
3710 sctp_assoc_clean_asconf_ack_cache(asoc);
3712 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3713 * expected, process the ASCONF as described below and after
3714 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3715 * the response packet and cache a copy of it (in the event it
3716 * later needs to be retransmitted).
3718 * Essentially, do V1-V5.
3720 asconf_ack = sctp_process_asconf((struct sctp_association *)
3723 return SCTP_DISPOSITION_NOMEM;
3724 } else if (serial < asoc->peer.addip_serial + 1) {
3726 * If the value found in the Sequence Number is less than the
3727 * ('Peer- Sequence-Number' + 1), simply skip to the next
3728 * ASCONF, and include in the outbound response packet
3729 * any previously cached ASCONF-ACK response that was
3730 * sent and saved that matches the Sequence Number of the
3731 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3732 * Chunk exists. This will occur when an older ASCONF
3733 * arrives out of order. In such a case, the receiver
3734 * should skip the ASCONF Chunk and not include ASCONF-ACK
3735 * Chunk for that chunk.
3737 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3739 return SCTP_DISPOSITION_DISCARD;
3741 /* Reset the transport so that we select the correct one
3742 * this time around. This is to make sure that we don't
3743 * accidentally use a stale transport that's been removed.
3745 asconf_ack->transport = NULL;
3747 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3748 * it must be either a stale packet or from an attacker.
3750 return SCTP_DISPOSITION_DISCARD;
3753 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3754 * containing the ASCONF-ACK Chunks MUST be the source address of
3755 * the SCTP packet that held the ASCONF Chunks.
3757 * To do this properly, we'll set the destination address of the chunk
3758 * and at the transmit time, will try look up the transport to use.
3759 * Since ASCONFs may be bundled, the correct transport may not be
3760 * created until we process the entire packet, thus this workaround.
3762 asconf_ack->dest = chunk->source;
3763 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3764 if (asoc->new_transport) {
3765 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3766 ((struct sctp_association *)asoc)->new_transport = NULL;
3769 return SCTP_DISPOSITION_CONSUME;
3772 static enum sctp_disposition sctp_send_next_asconf(
3774 const struct sctp_endpoint *ep,
3775 struct sctp_association *asoc,
3776 const union sctp_subtype type,
3777 struct sctp_cmd_seq *commands)
3779 struct sctp_chunk *asconf;
3780 struct list_head *entry;
3782 if (list_empty(&asoc->addip_chunk_list))
3783 return SCTP_DISPOSITION_CONSUME;
3785 entry = asoc->addip_chunk_list.next;
3786 asconf = list_entry(entry, struct sctp_chunk, list);
3788 list_del_init(entry);
3789 sctp_chunk_hold(asconf);
3790 asoc->addip_last_asconf = asconf;
3792 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3796 * ADDIP Section 4.3 General rules for address manipulation
3797 * When building TLV parameters for the ASCONF Chunk that will add or
3798 * delete IP addresses the D0 to D13 rules should be applied:
3800 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3801 const struct sctp_endpoint *ep,
3802 const struct sctp_association *asoc,
3803 const union sctp_subtype type,
3805 struct sctp_cmd_seq *commands)
3807 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3808 struct sctp_paramhdr *err_param = NULL;
3809 struct sctp_chunk *asconf_ack = arg;
3810 struct sctp_addiphdr *addip_hdr;
3811 __u32 sent_serial, rcvd_serial;
3812 struct sctp_chunk *abort;
3814 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3815 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3817 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3820 /* ADD-IP, Section 4.1.2:
3821 * This chunk MUST be sent in an authenticated way by using
3822 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3823 * is received unauthenticated it MUST be silently discarded as
3824 * described in [I-D.ietf-tsvwg-sctp-auth].
3826 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3827 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3830 /* Make sure that the ADDIP chunk has a valid length. */
3831 if (!sctp_chunk_length_valid(asconf_ack,
3832 sizeof(struct sctp_addip_chunk)))
3833 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3836 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3837 rcvd_serial = ntohl(addip_hdr->serial);
3839 /* Verify the ASCONF-ACK chunk before processing it. */
3840 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3841 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3842 (void *)err_param, commands);
3845 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3846 sent_serial = ntohl(addip_hdr->serial);
3848 sent_serial = asoc->addip_serial - 1;
3851 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3852 * equal to the next serial number to be used but no ASCONF chunk is
3853 * outstanding the endpoint MUST ABORT the association. Note that a
3854 * sequence number is greater than if it is no more than 2^^31-1
3855 * larger than the current sequence number (using serial arithmetic).
3857 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3858 !(asoc->addip_last_asconf)) {
3859 abort = sctp_make_abort(asoc, asconf_ack,
3860 sizeof(struct sctp_errhdr));
3862 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3863 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3866 /* We are going to ABORT, so we might as well stop
3867 * processing the rest of the chunks in the packet.
3869 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3870 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3871 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3872 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3873 SCTP_ERROR(ECONNABORTED));
3874 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3875 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3876 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3877 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3878 return SCTP_DISPOSITION_ABORT;
3881 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3882 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3883 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3885 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3887 return sctp_send_next_asconf(net, ep,
3888 (struct sctp_association *)asoc,
3891 abort = sctp_make_abort(asoc, asconf_ack,
3892 sizeof(struct sctp_errhdr));
3894 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3895 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3898 /* We are going to ABORT, so we might as well stop
3899 * processing the rest of the chunks in the packet.
3901 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3902 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3903 SCTP_ERROR(ECONNABORTED));
3904 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3905 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3906 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3907 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3908 return SCTP_DISPOSITION_ABORT;
3911 return SCTP_DISPOSITION_DISCARD;
3914 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3915 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
3916 const struct sctp_endpoint *ep,
3917 const struct sctp_association *asoc,
3918 const union sctp_subtype type,
3920 struct sctp_cmd_seq *commands)
3922 struct sctp_paramhdr *err_param = NULL;
3923 struct sctp_chunk *chunk = arg;
3924 struct sctp_reconf_chunk *hdr;
3925 union sctp_params param;
3927 if (!sctp_vtag_verify(chunk, asoc)) {
3928 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3930 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3933 /* Make sure that the RECONF chunk has a valid length. */
3934 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3935 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3938 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3939 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3940 (void *)err_param, commands);
3942 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3943 sctp_walk_params(param, hdr, params) {
3944 struct sctp_chunk *reply = NULL;
3945 struct sctp_ulpevent *ev = NULL;
3947 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3948 reply = sctp_process_strreset_outreq(
3949 (struct sctp_association *)asoc, param, &ev);
3950 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3951 reply = sctp_process_strreset_inreq(
3952 (struct sctp_association *)asoc, param, &ev);
3953 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3954 reply = sctp_process_strreset_tsnreq(
3955 (struct sctp_association *)asoc, param, &ev);
3956 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3957 reply = sctp_process_strreset_addstrm_out(
3958 (struct sctp_association *)asoc, param, &ev);
3959 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3960 reply = sctp_process_strreset_addstrm_in(
3961 (struct sctp_association *)asoc, param, &ev);
3962 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3963 reply = sctp_process_strreset_resp(
3964 (struct sctp_association *)asoc, param, &ev);
3967 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3971 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3975 return SCTP_DISPOSITION_CONSUME;
3979 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3981 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3982 * its cumulative TSN point to the value carried in the FORWARD TSN
3983 * chunk, and then MUST further advance its cumulative TSN point locally
3985 * After the above processing, the data receiver MUST stop reporting any
3986 * missing TSNs earlier than or equal to the new cumulative TSN point.
3988 * Verification Tag: 8.5 Verification Tag [Normal verification]
3990 * The return value is the disposition of the chunk.
3992 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
3993 const struct sctp_endpoint *ep,
3994 const struct sctp_association *asoc,
3995 const union sctp_subtype type,
3997 struct sctp_cmd_seq *commands)
3999 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4000 struct sctp_chunk *chunk = arg;
4001 struct sctp_fwdtsn_skip *skip;
4005 if (!sctp_vtag_verify(chunk, asoc)) {
4006 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4008 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4011 if (!asoc->peer.prsctp_capable)
4012 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4014 /* Make sure that the FORWARD_TSN chunk has valid length. */
4015 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
4016 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4019 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4020 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4021 len = ntohs(chunk->chunk_hdr->length);
4022 len -= sizeof(struct sctp_chunkhdr);
4023 skb_pull(chunk->skb, len);
4025 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4026 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4028 /* The TSN is too high--silently discard the chunk and count on it
4029 * getting retransmitted later.
4031 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4032 goto discard_noforce;
4034 /* Silently discard the chunk if stream-id is not valid */
4035 sctp_walk_fwdtsn(skip, chunk) {
4036 if (ntohs(skip->stream) >= asoc->stream.incnt)
4037 goto discard_noforce;
4040 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4041 if (len > sizeof(struct sctp_fwdtsn_hdr))
4042 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4045 /* Count this as receiving DATA. */
4046 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4047 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4048 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4051 /* FIXME: For now send a SACK, but DATA processing may
4054 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4056 return SCTP_DISPOSITION_CONSUME;
4059 return SCTP_DISPOSITION_DISCARD;
4062 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4064 const struct sctp_endpoint *ep,
4065 const struct sctp_association *asoc,
4066 const union sctp_subtype type,
4068 struct sctp_cmd_seq *commands)
4070 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4071 struct sctp_chunk *chunk = arg;
4072 struct sctp_fwdtsn_skip *skip;
4076 if (!sctp_vtag_verify(chunk, asoc)) {
4077 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4079 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4082 if (!asoc->peer.prsctp_capable)
4083 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4085 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4086 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
4087 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4090 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4091 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4092 len = ntohs(chunk->chunk_hdr->length);
4093 len -= sizeof(struct sctp_chunkhdr);
4094 skb_pull(chunk->skb, len);
4096 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4097 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4099 /* The TSN is too high--silently discard the chunk and count on it
4100 * getting retransmitted later.
4102 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4105 /* Silently discard the chunk if stream-id is not valid */
4106 sctp_walk_fwdtsn(skip, chunk) {
4107 if (ntohs(skip->stream) >= asoc->stream.incnt)
4111 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4112 if (len > sizeof(struct sctp_fwdtsn_hdr))
4113 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4116 /* Go a head and force a SACK, since we are shutting down. */
4118 /* Implementor's Guide.
4120 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4121 * respond to each received packet containing one or more DATA chunk(s)
4122 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4124 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4125 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4126 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4127 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4129 return SCTP_DISPOSITION_CONSUME;
4133 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4135 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4136 * Identifier field. If this algorithm was not specified by the
4137 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4138 * during association setup, the AUTH chunk and all chunks after it MUST
4139 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4140 * defined in Section 4.1.
4142 * If an endpoint with no shared key receives a Shared Key Identifier
4143 * other than 0, it MUST silently discard all authenticated chunks. If
4144 * the endpoint has at least one endpoint pair shared key for the peer,
4145 * it MUST use the key specified by the Shared Key Identifier if a
4146 * key has been configured for that Shared Key Identifier. If no
4147 * endpoint pair shared key has been configured for that Shared Key
4148 * Identifier, all authenticated chunks MUST be silently discarded.
4150 * Verification Tag: 8.5 Verification Tag [Normal verification]
4152 * The return value is the disposition of the chunk.
4154 static enum sctp_ierror sctp_sf_authenticate(
4155 const struct sctp_association *asoc,
4156 struct sctp_chunk *chunk)
4158 struct sctp_authhdr *auth_hdr;
4159 __u8 *save_digest, *digest;
4160 struct sctp_hmac *hmac;
4161 unsigned int sig_len;
4164 /* Pull in the auth header, so we can do some more verification */
4165 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4166 chunk->subh.auth_hdr = auth_hdr;
4167 skb_pull(chunk->skb, sizeof(*auth_hdr));
4169 /* Make sure that we support the HMAC algorithm from the auth
4172 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4173 return SCTP_IERROR_AUTH_BAD_HMAC;
4175 /* Make sure that the provided shared key identifier has been
4178 key_id = ntohs(auth_hdr->shkey_id);
4179 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4180 return SCTP_IERROR_AUTH_BAD_KEYID;
4183 /* Make sure that the length of the signature matches what
4186 sig_len = ntohs(chunk->chunk_hdr->length) -
4187 sizeof(struct sctp_auth_chunk);
4188 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4189 if (sig_len != hmac->hmac_len)
4190 return SCTP_IERROR_PROTO_VIOLATION;
4192 /* Now that we've done validation checks, we can compute and
4193 * verify the hmac. The steps involved are:
4194 * 1. Save the digest from the chunk.
4195 * 2. Zero out the digest in the chunk.
4196 * 3. Compute the new digest
4197 * 4. Compare saved and new digests.
4199 digest = auth_hdr->hmac;
4200 skb_pull(chunk->skb, sig_len);
4202 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4206 memset(digest, 0, sig_len);
4208 sctp_auth_calculate_hmac(asoc, chunk->skb,
4209 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4212 /* Discard the packet if the digests do not match */
4213 if (memcmp(save_digest, digest, sig_len)) {
4215 return SCTP_IERROR_BAD_SIG;
4221 return SCTP_IERROR_NO_ERROR;
4223 return SCTP_IERROR_NOMEM;
4226 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4227 const struct sctp_endpoint *ep,
4228 const struct sctp_association *asoc,
4229 const union sctp_subtype type,
4230 void *arg, struct sctp_cmd_seq *commands)
4232 struct sctp_chunk *chunk = arg;
4233 struct sctp_authhdr *auth_hdr;
4234 struct sctp_chunk *err_chunk;
4235 enum sctp_ierror error;
4237 /* Make sure that the peer has AUTH capable */
4238 if (!asoc->peer.auth_capable)
4239 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4241 if (!sctp_vtag_verify(chunk, asoc)) {
4242 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4244 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4247 /* Make sure that the AUTH chunk has valid length. */
4248 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4249 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4252 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4253 error = sctp_sf_authenticate(asoc, chunk);
4255 case SCTP_IERROR_AUTH_BAD_HMAC:
4256 /* Generate the ERROR chunk and discard the rest
4259 err_chunk = sctp_make_op_error(asoc, chunk,
4260 SCTP_ERROR_UNSUP_HMAC,
4264 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4265 SCTP_CHUNK(err_chunk));
4268 case SCTP_IERROR_AUTH_BAD_KEYID:
4269 case SCTP_IERROR_BAD_SIG:
4270 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4272 case SCTP_IERROR_PROTO_VIOLATION:
4273 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4276 case SCTP_IERROR_NOMEM:
4277 return SCTP_DISPOSITION_NOMEM;
4279 default: /* Prevent gcc warnings */
4283 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4284 struct sctp_ulpevent *ev;
4286 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4287 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4292 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4296 return SCTP_DISPOSITION_CONSUME;
4300 * Process an unknown chunk.
4302 * Section: 3.2. Also, 2.1 in the implementor's guide.
4304 * Chunk Types are encoded such that the highest-order two bits specify
4305 * the action that must be taken if the processing endpoint does not
4306 * recognize the Chunk Type.
4308 * 00 - Stop processing this SCTP packet and discard it, do not process
4309 * any further chunks within it.
4311 * 01 - Stop processing this SCTP packet and discard it, do not process
4312 * any further chunks within it, and report the unrecognized
4313 * chunk in an 'Unrecognized Chunk Type'.
4315 * 10 - Skip this chunk and continue processing.
4317 * 11 - Skip this chunk and continue processing, but report in an ERROR
4318 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4320 * The return value is the disposition of the chunk.
4322 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4323 const struct sctp_endpoint *ep,
4324 const struct sctp_association *asoc,
4325 const union sctp_subtype type,
4327 struct sctp_cmd_seq *commands)
4329 struct sctp_chunk *unk_chunk = arg;
4330 struct sctp_chunk *err_chunk;
4331 struct sctp_chunkhdr *hdr;
4333 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4335 if (!sctp_vtag_verify(unk_chunk, asoc))
4336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4338 /* Make sure that the chunk has a valid length.
4339 * Since we don't know the chunk type, we use a general
4340 * chunkhdr structure to make a comparison.
4342 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4343 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4346 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4347 case SCTP_CID_ACTION_DISCARD:
4348 /* Discard the packet. */
4349 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4350 case SCTP_CID_ACTION_DISCARD_ERR:
4351 /* Generate an ERROR chunk as response. */
4352 hdr = unk_chunk->chunk_hdr;
4353 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4354 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4355 SCTP_PAD4(ntohs(hdr->length)),
4358 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4359 SCTP_CHUNK(err_chunk));
4362 /* Discard the packet. */
4363 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4364 return SCTP_DISPOSITION_CONSUME;
4365 case SCTP_CID_ACTION_SKIP:
4366 /* Skip the chunk. */
4367 return SCTP_DISPOSITION_DISCARD;
4368 case SCTP_CID_ACTION_SKIP_ERR:
4369 /* Generate an ERROR chunk as response. */
4370 hdr = unk_chunk->chunk_hdr;
4371 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4372 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4373 SCTP_PAD4(ntohs(hdr->length)),
4376 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4377 SCTP_CHUNK(err_chunk));
4379 /* Skip the chunk. */
4380 return SCTP_DISPOSITION_CONSUME;
4385 return SCTP_DISPOSITION_DISCARD;
4389 * Discard the chunk.
4391 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4392 * [Too numerous to mention...]
4393 * Verification Tag: No verification needed.
4395 * (endpoint, asoc, chunk)
4398 * (asoc, reply_msg, msg_up, timers, counters)
4400 * The return value is the disposition of the chunk.
4402 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4403 const struct sctp_endpoint *ep,
4404 const struct sctp_association *asoc,
4405 const union sctp_subtype type,
4407 struct sctp_cmd_seq *commands)
4409 struct sctp_chunk *chunk = arg;
4411 /* Make sure that the chunk has a valid length.
4412 * Since we don't know the chunk type, we use a general
4413 * chunkhdr structure to make a comparison.
4415 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4416 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4419 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4421 return SCTP_DISPOSITION_DISCARD;
4425 * Discard the whole packet.
4429 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4430 * silently discard the OOTB packet and take no further action.
4432 * Verification Tag: No verification necessary
4435 * (endpoint, asoc, chunk)
4438 * (asoc, reply_msg, msg_up, timers, counters)
4440 * The return value is the disposition of the chunk.
4442 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4443 const struct sctp_endpoint *ep,
4444 const struct sctp_association *asoc,
4445 const union sctp_subtype type,
4446 void *arg, struct sctp_cmd_seq *commands)
4448 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4449 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4451 return SCTP_DISPOSITION_CONSUME;
4456 * The other end is violating protocol.
4458 * Section: Not specified
4459 * Verification Tag: Not specified
4461 * (endpoint, asoc, chunk)
4464 * (asoc, reply_msg, msg_up, timers, counters)
4466 * We simply tag the chunk as a violation. The state machine will log
4467 * the violation and continue.
4469 enum sctp_disposition sctp_sf_violation(struct net *net,
4470 const struct sctp_endpoint *ep,
4471 const struct sctp_association *asoc,
4472 const union sctp_subtype type,
4474 struct sctp_cmd_seq *commands)
4476 struct sctp_chunk *chunk = arg;
4478 /* Make sure that the chunk has a valid length. */
4479 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4480 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4483 return SCTP_DISPOSITION_VIOLATION;
4487 * Common function to handle a protocol violation.
4489 static enum sctp_disposition sctp_sf_abort_violation(
4491 const struct sctp_endpoint *ep,
4492 const struct sctp_association *asoc,
4494 struct sctp_cmd_seq *commands,
4495 const __u8 *payload,
4496 const size_t paylen)
4498 struct sctp_packet *packet = NULL;
4499 struct sctp_chunk *chunk = arg;
4500 struct sctp_chunk *abort = NULL;
4502 /* SCTP-AUTH, Section 6.3:
4503 * It should be noted that if the receiver wants to tear
4504 * down an association in an authenticated way only, the
4505 * handling of malformed packets should not result in
4506 * tearing down the association.
4508 * This means that if we only want to abort associations
4509 * in an authenticated way (i.e AUTH+ABORT), then we
4510 * can't destroy this association just because the packet
4513 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4516 /* Make the abort chunk. */
4517 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4522 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4523 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4524 !asoc->peer.i.init_tag) {
4525 struct sctp_initack_chunk *initack;
4527 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4528 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4529 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4531 unsigned int inittag;
4533 inittag = ntohl(initack->init_hdr.init_tag);
4534 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4539 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4540 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4542 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4543 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4544 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4545 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4546 SCTP_ERROR(ECONNREFUSED));
4547 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4548 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4550 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4551 SCTP_ERROR(ECONNABORTED));
4552 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4553 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4554 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4557 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4562 if (sctp_test_T_bit(abort))
4563 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4565 abort->skb->sk = ep->base.sk;
4567 sctp_packet_append_chunk(packet, abort);
4569 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4570 SCTP_PACKET(packet));
4572 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4575 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4578 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4579 return SCTP_DISPOSITION_ABORT;
4582 sctp_chunk_free(abort);
4584 return SCTP_DISPOSITION_NOMEM;
4588 * Handle a protocol violation when the chunk length is invalid.
4589 * "Invalid" length is identified as smaller than the minimal length a
4590 * given chunk can be. For example, a SACK chunk has invalid length
4591 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4593 * We inform the other end by sending an ABORT with a Protocol Violation
4596 * Section: Not specified
4597 * Verification Tag: Nothing to do
4599 * (endpoint, asoc, chunk)
4602 * (reply_msg, msg_up, counters)
4604 * Generate an ABORT chunk and terminate the association.
4606 static enum sctp_disposition sctp_sf_violation_chunklen(
4608 const struct sctp_endpoint *ep,
4609 const struct sctp_association *asoc,
4610 const union sctp_subtype type,
4612 struct sctp_cmd_seq *commands)
4614 static const char err_str[] = "The following chunk had invalid length:";
4616 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4621 * Handle a protocol violation when the parameter length is invalid.
4622 * If the length is smaller than the minimum length of a given parameter,
4623 * or accumulated length in multi parameters exceeds the end of the chunk,
4624 * the length is considered as invalid.
4626 static enum sctp_disposition sctp_sf_violation_paramlen(
4628 const struct sctp_endpoint *ep,
4629 const struct sctp_association *asoc,
4630 const union sctp_subtype type,
4631 void *arg, void *ext,
4632 struct sctp_cmd_seq *commands)
4634 struct sctp_paramhdr *param = ext;
4635 struct sctp_chunk *abort = NULL;
4636 struct sctp_chunk *chunk = arg;
4638 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4641 /* Make the abort chunk. */
4642 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4646 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4647 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4649 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4650 SCTP_ERROR(ECONNABORTED));
4651 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4652 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4653 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4654 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4657 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4658 return SCTP_DISPOSITION_ABORT;
4660 return SCTP_DISPOSITION_NOMEM;
4663 /* Handle a protocol violation when the peer trying to advance the
4664 * cumulative tsn ack to a point beyond the max tsn currently sent.
4666 * We inform the other end by sending an ABORT with a Protocol Violation
4669 static enum sctp_disposition sctp_sf_violation_ctsn(
4671 const struct sctp_endpoint *ep,
4672 const struct sctp_association *asoc,
4673 const union sctp_subtype type,
4675 struct sctp_cmd_seq *commands)
4677 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4679 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4683 /* Handle protocol violation of an invalid chunk bundling. For example,
4684 * when we have an association and we receive bundled INIT-ACK, or
4685 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4686 * statement from the specs. Additionally, there might be an attacker
4687 * on the path and we may not want to continue this communication.
4689 static enum sctp_disposition sctp_sf_violation_chunk(
4691 const struct sctp_endpoint *ep,
4692 const struct sctp_association *asoc,
4693 const union sctp_subtype type,
4695 struct sctp_cmd_seq *commands)
4697 static const char err_str[] = "The following chunk violates protocol:";
4700 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4702 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4705 /***************************************************************************
4706 * These are the state functions for handling primitive (Section 10) events.
4707 ***************************************************************************/
4709 * sctp_sf_do_prm_asoc
4711 * Section: 10.1 ULP-to-SCTP
4714 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4715 * outbound stream count)
4716 * -> association id [,destination transport addr list] [,outbound stream
4719 * This primitive allows the upper layer to initiate an association to a
4720 * specific peer endpoint.
4722 * The peer endpoint shall be specified by one of the transport addresses
4723 * which defines the endpoint (see Section 1.4). If the local SCTP
4724 * instance has not been initialized, the ASSOCIATE is considered an
4726 * [This is not relevant for the kernel implementation since we do all
4727 * initialization at boot time. It we hadn't initialized we wouldn't
4728 * get anywhere near this code.]
4730 * An association id, which is a local handle to the SCTP association,
4731 * will be returned on successful establishment of the association. If
4732 * SCTP is not able to open an SCTP association with the peer endpoint,
4733 * an error is returned.
4734 * [In the kernel implementation, the struct sctp_association needs to
4735 * be created BEFORE causing this primitive to run.]
4737 * Other association parameters may be returned, including the
4738 * complete destination transport addresses of the peer as well as the
4739 * outbound stream count of the local endpoint. One of the transport
4740 * address from the returned destination addresses will be selected by
4741 * the local endpoint as default primary path for sending SCTP packets
4742 * to this peer. The returned "destination transport addr list" can
4743 * be used by the ULP to change the default primary path or to force
4744 * sending a packet to a specific transport address. [All of this
4745 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4748 * Mandatory attributes:
4750 * o local SCTP instance name - obtained from the INITIALIZE operation.
4751 * [This is the argument asoc.]
4752 * o destination transport addr - specified as one of the transport
4753 * addresses of the peer endpoint with which the association is to be
4755 * [This is asoc->peer.active_path.]
4756 * o outbound stream count - the number of outbound streams the ULP
4757 * would like to open towards this peer endpoint.
4758 * [BUG: This is not currently implemented.]
4759 * Optional attributes:
4763 * The return value is a disposition.
4765 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4766 const struct sctp_endpoint *ep,
4767 const struct sctp_association *asoc,
4768 const union sctp_subtype type,
4770 struct sctp_cmd_seq *commands)
4772 struct sctp_association *my_asoc;
4773 struct sctp_chunk *repl;
4775 /* The comment below says that we enter COOKIE-WAIT AFTER
4776 * sending the INIT, but that doesn't actually work in our
4779 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4780 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4782 /* RFC 2960 5.1 Normal Establishment of an Association
4784 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4785 * must provide its Verification Tag (Tag_A) in the Initiate
4786 * Tag field. Tag_A SHOULD be a random number in the range of
4787 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4790 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4794 /* Choose transport for INIT. */
4795 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4798 /* Cast away the const modifier, as we want to just
4799 * rerun it through as a sideffect.
4801 my_asoc = (struct sctp_association *)asoc;
4802 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4804 /* After sending the INIT, "A" starts the T1-init timer and
4805 * enters the COOKIE-WAIT state.
4807 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4808 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4809 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4810 return SCTP_DISPOSITION_CONSUME;
4813 return SCTP_DISPOSITION_NOMEM;
4817 * Process the SEND primitive.
4819 * Section: 10.1 ULP-to-SCTP
4822 * Format: SEND(association id, buffer address, byte count [,context]
4823 * [,stream id] [,life time] [,destination transport address]
4824 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4827 * This is the main method to send user data via SCTP.
4829 * Mandatory attributes:
4831 * o association id - local handle to the SCTP association
4833 * o buffer address - the location where the user message to be
4834 * transmitted is stored;
4836 * o byte count - The size of the user data in number of bytes;
4838 * Optional attributes:
4840 * o context - an optional 32 bit integer that will be carried in the
4841 * sending failure notification to the ULP if the transportation of
4842 * this User Message fails.
4844 * o stream id - to indicate which stream to send the data on. If not
4845 * specified, stream 0 will be used.
4847 * o life time - specifies the life time of the user data. The user data
4848 * will not be sent by SCTP after the life time expires. This
4849 * parameter can be used to avoid efforts to transmit stale
4850 * user messages. SCTP notifies the ULP if the data cannot be
4851 * initiated to transport (i.e. sent to the destination via SCTP's
4852 * send primitive) within the life time variable. However, the
4853 * user data will be transmitted if SCTP has attempted to transmit a
4854 * chunk before the life time expired.
4856 * o destination transport address - specified as one of the destination
4857 * transport addresses of the peer endpoint to which this packet
4858 * should be sent. Whenever possible, SCTP should use this destination
4859 * transport address for sending the packets, instead of the current
4862 * o unorder flag - this flag, if present, indicates that the user
4863 * would like the data delivered in an unordered fashion to the peer
4864 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4867 * o no-bundle flag - instructs SCTP not to bundle this user data with
4868 * other outbound DATA chunks. SCTP MAY still bundle even when
4869 * this flag is present, when faced with network congestion.
4871 * o payload protocol-id - A 32 bit unsigned integer that is to be
4872 * passed to the peer indicating the type of payload protocol data
4873 * being transmitted. This value is passed as opaque data by SCTP.
4875 * The return value is the disposition.
4877 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4878 const struct sctp_endpoint *ep,
4879 const struct sctp_association *asoc,
4880 const union sctp_subtype type,
4882 struct sctp_cmd_seq *commands)
4884 struct sctp_datamsg *msg = arg;
4886 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4887 return SCTP_DISPOSITION_CONSUME;
4891 * Process the SHUTDOWN primitive.
4896 * Format: SHUTDOWN(association id)
4899 * Gracefully closes an association. Any locally queued user data
4900 * will be delivered to the peer. The association will be terminated only
4901 * after the peer acknowledges all the SCTP packets sent. A success code
4902 * will be returned on successful termination of the association. If
4903 * attempting to terminate the association results in a failure, an error
4904 * code shall be returned.
4906 * Mandatory attributes:
4908 * o association id - local handle to the SCTP association
4910 * Optional attributes:
4914 * The return value is the disposition.
4916 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
4918 const struct sctp_endpoint *ep,
4919 const struct sctp_association *asoc,
4920 const union sctp_subtype type,
4922 struct sctp_cmd_seq *commands)
4924 enum sctp_disposition disposition;
4926 /* From 9.2 Shutdown of an Association
4927 * Upon receipt of the SHUTDOWN primitive from its upper
4928 * layer, the endpoint enters SHUTDOWN-PENDING state and
4929 * remains there until all outstanding data has been
4930 * acknowledged by its peer. The endpoint accepts no new data
4931 * from its upper layer, but retransmits data to the far end
4932 * if necessary to fill gaps.
4934 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4935 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4937 disposition = SCTP_DISPOSITION_CONSUME;
4938 if (sctp_outq_is_empty(&asoc->outqueue)) {
4939 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4947 * Process the ABORT primitive.
4952 * Format: Abort(association id [, cause code])
4955 * Ungracefully closes an association. Any locally queued user data
4956 * will be discarded and an ABORT chunk is sent to the peer. A success code
4957 * will be returned on successful abortion of the association. If
4958 * attempting to abort the association results in a failure, an error
4959 * code shall be returned.
4961 * Mandatory attributes:
4963 * o association id - local handle to the SCTP association
4965 * Optional attributes:
4967 * o cause code - reason of the abort to be passed to the peer
4971 * The return value is the disposition.
4973 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
4975 const struct sctp_endpoint *ep,
4976 const struct sctp_association *asoc,
4977 const union sctp_subtype type,
4979 struct sctp_cmd_seq *commands)
4981 /* From 9.1 Abort of an Association
4982 * Upon receipt of the ABORT primitive from its upper
4983 * layer, the endpoint enters CLOSED state and
4984 * discard all outstanding data has been
4985 * acknowledged by its peer. The endpoint accepts no new data
4986 * from its upper layer, but retransmits data to the far end
4987 * if necessary to fill gaps.
4989 struct sctp_chunk *abort = arg;
4992 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4994 /* Even if we can't send the ABORT due to low memory delete the
4995 * TCB. This is a departure from our typical NOMEM handling.
4998 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4999 SCTP_ERROR(ECONNABORTED));
5000 /* Delete the established association. */
5001 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5002 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5004 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5005 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5007 return SCTP_DISPOSITION_ABORT;
5010 /* We tried an illegal operation on an association which is closed. */
5011 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5012 const struct sctp_endpoint *ep,
5013 const struct sctp_association *asoc,
5014 const union sctp_subtype type,
5016 struct sctp_cmd_seq *commands)
5018 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5019 return SCTP_DISPOSITION_CONSUME;
5022 /* We tried an illegal operation on an association which is shutting
5025 enum sctp_disposition sctp_sf_error_shutdown(
5027 const struct sctp_endpoint *ep,
5028 const struct sctp_association *asoc,
5029 const union sctp_subtype type,
5031 struct sctp_cmd_seq *commands)
5033 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5034 SCTP_ERROR(-ESHUTDOWN));
5035 return SCTP_DISPOSITION_CONSUME;
5039 * sctp_cookie_wait_prm_shutdown
5041 * Section: 4 Note: 2
5046 * The RFC does not explicitly address this issue, but is the route through the
5047 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5052 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5054 const struct sctp_endpoint *ep,
5055 const struct sctp_association *asoc,
5056 const union sctp_subtype type,
5058 struct sctp_cmd_seq *commands)
5060 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5061 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5063 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5064 SCTP_STATE(SCTP_STATE_CLOSED));
5066 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5068 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5070 return SCTP_DISPOSITION_DELETE_TCB;
5074 * sctp_cookie_echoed_prm_shutdown
5076 * Section: 4 Note: 2
5081 * The RFC does not explcitly address this issue, but is the route through the
5082 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5087 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5089 const struct sctp_endpoint *ep,
5090 const struct sctp_association *asoc,
5091 const union sctp_subtype type,
5093 struct sctp_cmd_seq *commands)
5095 /* There is a single T1 timer, so we should be able to use
5096 * common function with the COOKIE-WAIT state.
5098 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5102 * sctp_sf_cookie_wait_prm_abort
5104 * Section: 4 Note: 2
5109 * The RFC does not explicitly address this issue, but is the route through the
5110 * state table when someone issues an abort while in COOKIE_WAIT state.
5115 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5117 const struct sctp_endpoint *ep,
5118 const struct sctp_association *asoc,
5119 const union sctp_subtype type,
5121 struct sctp_cmd_seq *commands)
5123 struct sctp_chunk *abort = arg;
5125 /* Stop T1-init timer */
5126 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5127 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5130 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5132 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5133 SCTP_STATE(SCTP_STATE_CLOSED));
5135 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5137 /* Even if we can't send the ABORT due to low memory delete the
5138 * TCB. This is a departure from our typical NOMEM handling.
5141 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5142 SCTP_ERROR(ECONNREFUSED));
5143 /* Delete the established association. */
5144 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5145 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5147 return SCTP_DISPOSITION_ABORT;
5151 * sctp_sf_cookie_echoed_prm_abort
5153 * Section: 4 Note: 3
5158 * The RFC does not explcitly address this issue, but is the route through the
5159 * state table when someone issues an abort while in COOKIE_ECHOED state.
5164 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5166 const struct sctp_endpoint *ep,
5167 const struct sctp_association *asoc,
5168 const union sctp_subtype type,
5170 struct sctp_cmd_seq *commands)
5172 /* There is a single T1 timer, so we should be able to use
5173 * common function with the COOKIE-WAIT state.
5175 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5179 * sctp_sf_shutdown_pending_prm_abort
5184 * The RFC does not explicitly address this issue, but is the route through the
5185 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5190 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5192 const struct sctp_endpoint *ep,
5193 const struct sctp_association *asoc,
5194 const union sctp_subtype type,
5196 struct sctp_cmd_seq *commands)
5198 /* Stop the T5-shutdown guard timer. */
5199 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5200 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5202 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5206 * sctp_sf_shutdown_sent_prm_abort
5211 * The RFC does not explicitly address this issue, but is the route through the
5212 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5217 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5219 const struct sctp_endpoint *ep,
5220 const struct sctp_association *asoc,
5221 const union sctp_subtype type,
5223 struct sctp_cmd_seq *commands)
5225 /* Stop the T2-shutdown timer. */
5226 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5227 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5229 /* Stop the T5-shutdown guard timer. */
5230 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5231 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5233 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5237 * sctp_sf_cookie_echoed_prm_abort
5242 * The RFC does not explcitly address this issue, but is the route through the
5243 * state table when someone issues an abort while in COOKIE_ECHOED state.
5248 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5250 const struct sctp_endpoint *ep,
5251 const struct sctp_association *asoc,
5252 const union sctp_subtype type,
5254 struct sctp_cmd_seq *commands)
5256 /* The same T2 timer, so we should be able to use
5257 * common function with the SHUTDOWN-SENT state.
5259 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5263 * Process the REQUESTHEARTBEAT primitive
5266 * J) Request Heartbeat
5268 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5272 * Instructs the local endpoint to perform a HeartBeat on the specified
5273 * destination transport address of the given association. The returned
5274 * result should indicate whether the transmission of the HEARTBEAT
5275 * chunk to the destination address is successful.
5277 * Mandatory attributes:
5279 * o association id - local handle to the SCTP association
5281 * o destination transport address - the transport address of the
5282 * association on which a heartbeat should be issued.
5284 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5286 const struct sctp_endpoint *ep,
5287 const struct sctp_association *asoc,
5288 const union sctp_subtype type,
5290 struct sctp_cmd_seq *commands)
5292 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5293 (struct sctp_transport *)arg, commands))
5294 return SCTP_DISPOSITION_NOMEM;
5297 * RFC 2960 (bis), section 8.3
5299 * D) Request an on-demand HEARTBEAT on a specific destination
5300 * transport address of a given association.
5302 * The endpoint should increment the respective error counter of
5303 * the destination transport address each time a HEARTBEAT is sent
5304 * to that address and not acknowledged within one RTO.
5307 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5308 SCTP_TRANSPORT(arg));
5309 return SCTP_DISPOSITION_CONSUME;
5313 * ADDIP Section 4.1 ASCONF Chunk Procedures
5314 * When an endpoint has an ASCONF signaled change to be sent to the
5315 * remote endpoint it should do A1 to A9
5317 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5318 const struct sctp_endpoint *ep,
5319 const struct sctp_association *asoc,
5320 const union sctp_subtype type,
5322 struct sctp_cmd_seq *commands)
5324 struct sctp_chunk *chunk = arg;
5326 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5327 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5328 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5329 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5330 return SCTP_DISPOSITION_CONSUME;
5333 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5334 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5335 const struct sctp_endpoint *ep,
5336 const struct sctp_association *asoc,
5337 const union sctp_subtype type,
5339 struct sctp_cmd_seq *commands)
5341 struct sctp_chunk *chunk = arg;
5343 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5344 return SCTP_DISPOSITION_CONSUME;
5348 * Ignore the primitive event
5350 * The return value is the disposition of the primitive.
5352 enum sctp_disposition sctp_sf_ignore_primitive(
5354 const struct sctp_endpoint *ep,
5355 const struct sctp_association *asoc,
5356 const union sctp_subtype type,
5358 struct sctp_cmd_seq *commands)
5360 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5363 return SCTP_DISPOSITION_DISCARD;
5366 /***************************************************************************
5367 * These are the state functions for the OTHER events.
5368 ***************************************************************************/
5371 * When the SCTP stack has no more user data to send or retransmit, this
5372 * notification is given to the user. Also, at the time when a user app
5373 * subscribes to this event, if there is no data to be sent or
5374 * retransmit, the stack will immediately send up this notification.
5376 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5378 const struct sctp_endpoint *ep,
5379 const struct sctp_association *asoc,
5380 const union sctp_subtype type,
5382 struct sctp_cmd_seq *commands)
5384 struct sctp_ulpevent *event;
5386 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5388 return SCTP_DISPOSITION_NOMEM;
5390 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5392 return SCTP_DISPOSITION_CONSUME;
5396 * Start the shutdown negotiation.
5399 * Once all its outstanding data has been acknowledged, the endpoint
5400 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5401 * TSN Ack field the last sequential TSN it has received from the peer.
5402 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5403 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5404 * with the updated last sequential TSN received from its peer.
5406 * The return value is the disposition.
5408 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5410 const struct sctp_endpoint *ep,
5411 const struct sctp_association *asoc,
5412 const union sctp_subtype type,
5414 struct sctp_cmd_seq *commands)
5416 struct sctp_chunk *reply;
5418 /* Once all its outstanding data has been acknowledged, the
5419 * endpoint shall send a SHUTDOWN chunk to its peer including
5420 * in the Cumulative TSN Ack field the last sequential TSN it
5421 * has received from the peer.
5423 reply = sctp_make_shutdown(asoc, NULL);
5427 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5428 * T2-shutdown timer.
5430 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5432 /* It shall then start the T2-shutdown timer */
5433 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5434 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5436 /* RFC 4960 Section 9.2
5437 * The sender of the SHUTDOWN MAY also start an overall guard timer
5438 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5440 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5441 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5443 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5444 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5445 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5447 /* and enter the SHUTDOWN-SENT state. */
5448 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5449 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5451 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5453 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5456 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5458 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5460 return SCTP_DISPOSITION_CONSUME;
5463 return SCTP_DISPOSITION_NOMEM;
5467 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5471 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5472 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5473 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5474 * endpoint must re-send the SHUTDOWN ACK.
5476 * The return value is the disposition.
5478 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5480 const struct sctp_endpoint *ep,
5481 const struct sctp_association *asoc,
5482 const union sctp_subtype type,
5484 struct sctp_cmd_seq *commands)
5486 struct sctp_chunk *chunk = arg;
5487 struct sctp_chunk *reply;
5489 /* There are 2 ways of getting here:
5490 * 1) called in response to a SHUTDOWN chunk
5491 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5493 * For the case (2), the arg parameter is set to NULL. We need
5494 * to check that we have a chunk before accessing it's fields.
5497 if (!sctp_vtag_verify(chunk, asoc))
5498 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5501 /* Make sure that the SHUTDOWN chunk has a valid length. */
5502 if (!sctp_chunk_length_valid(
5503 chunk, sizeof(struct sctp_shutdown_chunk)))
5504 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5508 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5509 * shall send a SHUTDOWN ACK ...
5511 reply = sctp_make_shutdown_ack(asoc, chunk);
5515 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5516 * the T2-shutdown timer.
5518 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5520 /* and start/restart a T2-shutdown timer of its own, */
5521 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5522 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5524 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5525 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5526 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5528 /* Enter the SHUTDOWN-ACK-SENT state. */
5529 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5530 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5532 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5534 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5537 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5539 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5541 return SCTP_DISPOSITION_CONSUME;
5544 return SCTP_DISPOSITION_NOMEM;
5548 * Ignore the event defined as other
5550 * The return value is the disposition of the event.
5552 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5553 const struct sctp_endpoint *ep,
5554 const struct sctp_association *asoc,
5555 const union sctp_subtype type,
5557 struct sctp_cmd_seq *commands)
5559 pr_debug("%s: the event other type:%d is ignored\n",
5560 __func__, type.other);
5562 return SCTP_DISPOSITION_DISCARD;
5565 /************************************************************
5566 * These are the state functions for handling timeout events.
5567 ************************************************************/
5572 * Section: 6.3.3 Handle T3-rtx Expiration
5574 * Whenever the retransmission timer T3-rtx expires for a destination
5575 * address, do the following:
5578 * The return value is the disposition of the chunk.
5580 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5581 const struct sctp_endpoint *ep,
5582 const struct sctp_association *asoc,
5583 const union sctp_subtype type,
5585 struct sctp_cmd_seq *commands)
5587 struct sctp_transport *transport = arg;
5589 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5591 if (asoc->overall_error_count >= asoc->max_retrans) {
5592 if (asoc->peer.zero_window_announced &&
5593 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5595 * We are here likely because the receiver had its rwnd
5596 * closed for a while and we have not been able to
5597 * transmit the locally queued data within the maximum
5598 * retransmission attempts limit. Start the T5
5599 * shutdown guard timer to give the receiver one last
5600 * chance and some additional time to recover before
5603 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5604 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5606 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5607 SCTP_ERROR(ETIMEDOUT));
5608 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5609 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5610 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5611 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5612 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5613 return SCTP_DISPOSITION_DELETE_TCB;
5617 /* E1) For the destination address for which the timer
5618 * expires, adjust its ssthresh with rules defined in Section
5619 * 7.2.3 and set the cwnd <- MTU.
5622 /* E2) For the destination address for which the timer
5623 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5624 * maximum value discussed in rule C7 above (RTO.max) may be
5625 * used to provide an upper bound to this doubling operation.
5628 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5629 * outstanding DATA chunks for the address for which the
5630 * T3-rtx has expired will fit into a single packet, subject
5631 * to the MTU constraint for the path corresponding to the
5632 * destination transport address to which the retransmission
5633 * is being sent (this may be different from the address for
5634 * which the timer expires [see Section 6.4]). Call this
5635 * value K. Bundle and retransmit those K DATA chunks in a
5636 * single packet to the destination endpoint.
5638 * Note: Any DATA chunks that were sent to the address for
5639 * which the T3-rtx timer expired but did not fit in one MTU
5640 * (rule E3 above), should be marked for retransmission and
5641 * sent as soon as cwnd allows (normally when a SACK arrives).
5644 /* Do some failure management (Section 8.2). */
5645 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5647 /* NB: Rules E4 and F1 are implicit in R1. */
5648 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5650 return SCTP_DISPOSITION_CONSUME;
5654 * Generate delayed SACK on timeout
5656 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5658 * The guidelines on delayed acknowledgement algorithm specified in
5659 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5660 * acknowledgement SHOULD be generated for at least every second packet
5661 * (not every second DATA chunk) received, and SHOULD be generated
5662 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5663 * some situations it may be beneficial for an SCTP transmitter to be
5664 * more conservative than the algorithms detailed in this document
5665 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5666 * the following algorithms allow.
5668 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5669 const struct sctp_endpoint *ep,
5670 const struct sctp_association *asoc,
5671 const union sctp_subtype type,
5673 struct sctp_cmd_seq *commands)
5675 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5676 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5677 return SCTP_DISPOSITION_CONSUME;
5681 * sctp_sf_t1_init_timer_expire
5683 * Section: 4 Note: 2
5688 * RFC 2960 Section 4 Notes
5689 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5690 * and re-start the T1-init timer without changing state. This MUST
5691 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5692 * endpoint MUST abort the initialization process and report the
5693 * error to SCTP user.
5699 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5701 const struct sctp_endpoint *ep,
5702 const struct sctp_association *asoc,
5703 const union sctp_subtype type,
5705 struct sctp_cmd_seq *commands)
5707 int attempts = asoc->init_err_counter + 1;
5708 struct sctp_chunk *repl = NULL;
5709 struct sctp_bind_addr *bp;
5711 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5713 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5715 if (attempts <= asoc->max_init_attempts) {
5716 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5717 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5719 return SCTP_DISPOSITION_NOMEM;
5721 /* Choose transport for INIT. */
5722 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5725 /* Issue a sideeffect to do the needed accounting. */
5726 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5727 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5729 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5731 pr_debug("%s: giving up on INIT, attempts:%d "
5732 "max_init_attempts:%d\n", __func__, attempts,
5733 asoc->max_init_attempts);
5735 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5736 SCTP_ERROR(ETIMEDOUT));
5737 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5738 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5739 return SCTP_DISPOSITION_DELETE_TCB;
5742 return SCTP_DISPOSITION_CONSUME;
5746 * sctp_sf_t1_cookie_timer_expire
5748 * Section: 4 Note: 2
5753 * RFC 2960 Section 4 Notes
5754 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5755 * COOKIE ECHO and re-start the T1-cookie timer without changing
5756 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5757 * After that, the endpoint MUST abort the initialization process and
5758 * report the error to SCTP user.
5764 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5766 const struct sctp_endpoint *ep,
5767 const struct sctp_association *asoc,
5768 const union sctp_subtype type,
5770 struct sctp_cmd_seq *commands)
5772 int attempts = asoc->init_err_counter + 1;
5773 struct sctp_chunk *repl = NULL;
5775 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5777 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5779 if (attempts <= asoc->max_init_attempts) {
5780 repl = sctp_make_cookie_echo(asoc, NULL);
5782 return SCTP_DISPOSITION_NOMEM;
5784 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5786 /* Issue a sideeffect to do the needed accounting. */
5787 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5788 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5790 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5792 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5793 SCTP_ERROR(ETIMEDOUT));
5794 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5795 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5796 return SCTP_DISPOSITION_DELETE_TCB;
5799 return SCTP_DISPOSITION_CONSUME;
5802 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5803 * with the updated last sequential TSN received from its peer.
5805 * An endpoint should limit the number of retransmissions of the
5806 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5807 * If this threshold is exceeded the endpoint should destroy the TCB and
5808 * MUST report the peer endpoint unreachable to the upper layer (and
5809 * thus the association enters the CLOSED state). The reception of any
5810 * packet from its peer (i.e. as the peer sends all of its queued DATA
5811 * chunks) should clear the endpoint's retransmission count and restart
5812 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5813 * all of its queued DATA chunks that have not yet been sent.
5815 enum sctp_disposition sctp_sf_t2_timer_expire(
5817 const struct sctp_endpoint *ep,
5818 const struct sctp_association *asoc,
5819 const union sctp_subtype type,
5821 struct sctp_cmd_seq *commands)
5823 struct sctp_chunk *reply = NULL;
5825 pr_debug("%s: timer T2 expired\n", __func__);
5827 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5829 ((struct sctp_association *)asoc)->shutdown_retries++;
5831 if (asoc->overall_error_count >= asoc->max_retrans) {
5832 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5833 SCTP_ERROR(ETIMEDOUT));
5834 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5835 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5836 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5837 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5838 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5839 return SCTP_DISPOSITION_DELETE_TCB;
5842 switch (asoc->state) {
5843 case SCTP_STATE_SHUTDOWN_SENT:
5844 reply = sctp_make_shutdown(asoc, NULL);
5847 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5848 reply = sctp_make_shutdown_ack(asoc, NULL);
5859 /* Do some failure management (Section 8.2).
5860 * If we remove the transport an SHUTDOWN was last sent to, don't
5861 * do failure management.
5863 if (asoc->shutdown_last_sent_to)
5864 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5865 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5867 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5868 * the T2-shutdown timer.
5870 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5872 /* Restart the T2-shutdown timer. */
5873 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5874 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5875 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5876 return SCTP_DISPOSITION_CONSUME;
5879 return SCTP_DISPOSITION_NOMEM;
5883 * ADDIP Section 4.1 ASCONF CHunk Procedures
5884 * If the T4 RTO timer expires the endpoint should do B1 to B5
5886 enum sctp_disposition sctp_sf_t4_timer_expire(
5888 const struct sctp_endpoint *ep,
5889 const struct sctp_association *asoc,
5890 const union sctp_subtype type,
5892 struct sctp_cmd_seq *commands)
5894 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5895 struct sctp_transport *transport = chunk->transport;
5897 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5899 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5900 * detection on the appropriate destination address as defined in
5901 * RFC2960 [5] section 8.1 and 8.2.
5904 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5905 SCTP_TRANSPORT(transport));
5907 /* Reconfig T4 timer and transport. */
5908 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5910 /* ADDIP 4.1 B2) Increment the association error counters and perform
5911 * endpoint failure detection on the association as defined in
5912 * RFC2960 [5] section 8.1 and 8.2.
5913 * association error counter is incremented in SCTP_CMD_STRIKE.
5915 if (asoc->overall_error_count >= asoc->max_retrans) {
5916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5917 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5918 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5919 SCTP_ERROR(ETIMEDOUT));
5920 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5921 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5922 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5923 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5924 return SCTP_DISPOSITION_ABORT;
5927 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5928 * the ASCONF chunk was sent by doubling the RTO timer value.
5929 * This is done in SCTP_CMD_STRIKE.
5932 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5933 * choose an alternate destination address (please refer to RFC2960
5934 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5935 * chunk, it MUST be the same (including its serial number) as the last
5938 sctp_chunk_hold(asoc->addip_last_asconf);
5939 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5940 SCTP_CHUNK(asoc->addip_last_asconf));
5942 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5943 * destination is selected, then the RTO used will be that of the new
5944 * destination address.
5946 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5947 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5949 return SCTP_DISPOSITION_CONSUME;
5952 /* sctpimpguide-05 Section 2.12.2
5953 * The sender of the SHUTDOWN MAY also start an overall guard timer
5954 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5955 * At the expiration of this timer the sender SHOULD abort the association
5956 * by sending an ABORT chunk.
5958 enum sctp_disposition sctp_sf_t5_timer_expire(
5960 const struct sctp_endpoint *ep,
5961 const struct sctp_association *asoc,
5962 const union sctp_subtype type,
5964 struct sctp_cmd_seq *commands)
5966 struct sctp_chunk *reply = NULL;
5968 pr_debug("%s: timer T5 expired\n", __func__);
5970 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5972 reply = sctp_make_abort(asoc, NULL, 0);
5976 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5977 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5978 SCTP_ERROR(ETIMEDOUT));
5979 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5980 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5982 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5983 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5985 return SCTP_DISPOSITION_DELETE_TCB;
5987 return SCTP_DISPOSITION_NOMEM;
5990 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5991 * the association is automatically closed by starting the shutdown process.
5992 * The work that needs to be done is same as when SHUTDOWN is initiated by
5993 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5995 enum sctp_disposition sctp_sf_autoclose_timer_expire(
5997 const struct sctp_endpoint *ep,
5998 const struct sctp_association *asoc,
5999 const union sctp_subtype type,
6001 struct sctp_cmd_seq *commands)
6003 enum sctp_disposition disposition;
6005 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6007 /* From 9.2 Shutdown of an Association
6008 * Upon receipt of the SHUTDOWN primitive from its upper
6009 * layer, the endpoint enters SHUTDOWN-PENDING state and
6010 * remains there until all outstanding data has been
6011 * acknowledged by its peer. The endpoint accepts no new data
6012 * from its upper layer, but retransmits data to the far end
6013 * if necessary to fill gaps.
6015 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6016 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6018 disposition = SCTP_DISPOSITION_CONSUME;
6019 if (sctp_outq_is_empty(&asoc->outqueue)) {
6020 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6027 /*****************************************************************************
6028 * These are sa state functions which could apply to all types of events.
6029 ****************************************************************************/
6032 * This table entry is not implemented.
6035 * (endpoint, asoc, chunk)
6037 * The return value is the disposition of the chunk.
6039 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6040 const struct sctp_endpoint *ep,
6041 const struct sctp_association *asoc,
6042 const union sctp_subtype type,
6043 void *arg, struct sctp_cmd_seq *commands)
6045 return SCTP_DISPOSITION_NOT_IMPL;
6049 * This table entry represents a bug.
6052 * (endpoint, asoc, chunk)
6054 * The return value is the disposition of the chunk.
6056 enum sctp_disposition sctp_sf_bug(struct net *net,
6057 const struct sctp_endpoint *ep,
6058 const struct sctp_association *asoc,
6059 const union sctp_subtype type,
6060 void *arg, struct sctp_cmd_seq *commands)
6062 return SCTP_DISPOSITION_BUG;
6066 * This table entry represents the firing of a timer in the wrong state.
6067 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6068 * when the association is in the wrong state. This event should
6069 * be ignored, so as to prevent any rearming of the timer.
6072 * (endpoint, asoc, chunk)
6074 * The return value is the disposition of the chunk.
6076 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6077 const struct sctp_endpoint *ep,
6078 const struct sctp_association *asoc,
6079 const union sctp_subtype type,
6081 struct sctp_cmd_seq *commands)
6083 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6085 return SCTP_DISPOSITION_CONSUME;
6088 /********************************************************************
6089 * 2nd Level Abstractions
6090 ********************************************************************/
6092 /* Pull the SACK chunk based on the SACK header. */
6093 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6095 struct sctp_sackhdr *sack;
6100 /* Protect ourselves from reading too far into
6101 * the skb from a bogus sender.
6103 sack = (struct sctp_sackhdr *) chunk->skb->data;
6105 num_blocks = ntohs(sack->num_gap_ack_blocks);
6106 num_dup_tsns = ntohs(sack->num_dup_tsns);
6107 len = sizeof(struct sctp_sackhdr);
6108 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6109 if (len > chunk->skb->len)
6112 skb_pull(chunk->skb, len);
6117 /* Create an ABORT packet to be sent as a response, with the specified
6120 static struct sctp_packet *sctp_abort_pkt_new(
6122 const struct sctp_endpoint *ep,
6123 const struct sctp_association *asoc,
6124 struct sctp_chunk *chunk,
6125 const void *payload, size_t paylen)
6127 struct sctp_packet *packet;
6128 struct sctp_chunk *abort;
6130 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6134 * The T bit will be set if the asoc is NULL.
6136 abort = sctp_make_abort(asoc, chunk, paylen);
6138 sctp_ootb_pkt_free(packet);
6142 /* Reflect vtag if T-Bit is set */
6143 if (sctp_test_T_bit(abort))
6144 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6146 /* Add specified error causes, i.e., payload, to the
6149 sctp_addto_chunk(abort, paylen, payload);
6151 /* Set the skb to the belonging sock for accounting. */
6152 abort->skb->sk = ep->base.sk;
6154 sctp_packet_append_chunk(packet, abort);
6161 /* Allocate a packet for responding in the OOTB conditions. */
6162 static struct sctp_packet *sctp_ootb_pkt_new(
6164 const struct sctp_association *asoc,
6165 const struct sctp_chunk *chunk)
6167 struct sctp_transport *transport;
6168 struct sctp_packet *packet;
6172 /* Get the source and destination port from the inbound packet. */
6173 sport = ntohs(chunk->sctp_hdr->dest);
6174 dport = ntohs(chunk->sctp_hdr->source);
6176 /* The V-tag is going to be the same as the inbound packet if no
6177 * association exists, otherwise, use the peer's vtag.
6180 /* Special case the INIT-ACK as there is no peer's vtag
6183 switch (chunk->chunk_hdr->type) {
6184 case SCTP_CID_INIT_ACK:
6186 struct sctp_initack_chunk *initack;
6188 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6189 vtag = ntohl(initack->init_hdr.init_tag);
6193 vtag = asoc->peer.i.init_tag;
6197 /* Special case the INIT and stale COOKIE_ECHO as there is no
6200 switch (chunk->chunk_hdr->type) {
6203 struct sctp_init_chunk *init;
6205 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6206 vtag = ntohl(init->init_hdr.init_tag);
6210 vtag = ntohl(chunk->sctp_hdr->vtag);
6215 /* Make a transport for the bucket, Eliza... */
6216 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6220 /* Cache a route for the transport with the chunk's destination as
6221 * the source address.
6223 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6224 sctp_sk(net->sctp.ctl_sock));
6226 packet = &transport->packet;
6227 sctp_packet_init(packet, transport, sport, dport);
6228 sctp_packet_config(packet, vtag, 0);
6236 /* Free the packet allocated earlier for responding in the OOTB condition. */
6237 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6239 sctp_transport_free(packet->transport);
6242 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6243 static void sctp_send_stale_cookie_err(struct net *net,
6244 const struct sctp_endpoint *ep,
6245 const struct sctp_association *asoc,
6246 const struct sctp_chunk *chunk,
6247 struct sctp_cmd_seq *commands,
6248 struct sctp_chunk *err_chunk)
6250 struct sctp_packet *packet;
6253 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6255 struct sctp_signed_cookie *cookie;
6257 /* Override the OOTB vtag from the cookie. */
6258 cookie = chunk->subh.cookie_hdr;
6259 packet->vtag = cookie->c.peer_vtag;
6261 /* Set the skb to the belonging sock for accounting. */
6262 err_chunk->skb->sk = ep->base.sk;
6263 sctp_packet_append_chunk(packet, err_chunk);
6264 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6265 SCTP_PACKET(packet));
6266 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6268 sctp_chunk_free (err_chunk);
6273 /* Process a data chunk */
6274 static int sctp_eat_data(const struct sctp_association *asoc,
6275 struct sctp_chunk *chunk,
6276 struct sctp_cmd_seq *commands)
6278 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6279 struct sock *sk = asoc->base.sk;
6280 struct net *net = sock_net(sk);
6281 struct sctp_datahdr *data_hdr;
6282 struct sctp_chunk *err;
6283 enum sctp_verb deliver;
6290 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6291 chunk->subh.data_hdr = data_hdr;
6292 skb_pull(chunk->skb, sizeof(*data_hdr));
6294 tsn = ntohl(data_hdr->tsn);
6295 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6297 /* ASSERT: Now skb->data is really the user data. */
6299 /* Process ECN based congestion.
6301 * Since the chunk structure is reused for all chunks within
6302 * a packet, we use ecn_ce_done to track if we've already
6303 * done CE processing for this packet.
6305 * We need to do ECN processing even if we plan to discard the
6309 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6310 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6311 chunk->ecn_ce_done = 1;
6313 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6314 /* Do real work as sideffect. */
6315 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6320 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6322 /* The TSN is too high--silently discard the chunk and
6323 * count on it getting retransmitted later.
6326 chunk->asoc->stats.outofseqtsns++;
6327 return SCTP_IERROR_HIGH_TSN;
6328 } else if (tmp > 0) {
6329 /* This is a duplicate. Record it. */
6330 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6331 return SCTP_IERROR_DUP_TSN;
6334 /* This is a new TSN. */
6336 /* Discard if there is no room in the receive window.
6337 * Actually, allow a little bit of overflow (up to a MTU).
6339 datalen = ntohs(chunk->chunk_hdr->length);
6340 datalen -= sizeof(struct sctp_data_chunk);
6342 deliver = SCTP_CMD_CHUNK_ULP;
6344 /* Think about partial delivery. */
6345 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6347 /* Even if we don't accept this chunk there is
6350 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6353 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6354 * seems a bit troublesome in that frag_point varies based on
6355 * PMTU. In cases, such as loopback, this might be a rather
6358 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6359 (datalen > asoc->rwnd + asoc->frag_point))) {
6361 /* If this is the next TSN, consider reneging to make
6362 * room. Note: Playing nice with a confused sender. A
6363 * malicious sender can still eat up all our buffer
6364 * space and in the future we may want to detect and
6365 * do more drastic reneging.
6367 if (sctp_tsnmap_has_gap(map) &&
6368 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6369 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6370 deliver = SCTP_CMD_RENEGE;
6372 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6373 __func__, tsn, datalen, asoc->rwnd);
6375 return SCTP_IERROR_IGNORE_TSN;
6380 * Also try to renege to limit our memory usage in the event that
6381 * we are under memory pressure
6382 * If we can't renege, don't worry about it, the sk_rmem_schedule
6383 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6384 * memory usage too much
6386 if (*sk->sk_prot_creator->memory_pressure) {
6387 if (sctp_tsnmap_has_gap(map) &&
6388 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6389 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6391 deliver = SCTP_CMD_RENEGE;
6396 * Section 3.3.10.9 No User Data (9)
6400 * No User Data: This error cause is returned to the originator of a
6401 * DATA chunk if a received DATA chunk has no user data.
6403 if (unlikely(0 == datalen)) {
6404 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6406 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6409 /* We are going to ABORT, so we might as well stop
6410 * processing the rest of the chunks in the packet.
6412 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6413 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6414 SCTP_ERROR(ECONNABORTED));
6415 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6416 SCTP_PERR(SCTP_ERROR_NO_DATA));
6417 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6418 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6419 return SCTP_IERROR_NO_DATA;
6422 chunk->data_accepted = 1;
6424 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6425 * if we renege and the chunk arrives again.
6427 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6428 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6430 chunk->asoc->stats.iuodchunks++;
6432 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6434 chunk->asoc->stats.iodchunks++;
6438 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6440 * If an endpoint receive a DATA chunk with an invalid stream
6441 * identifier, it shall acknowledge the reception of the DATA chunk
6442 * following the normal procedure, immediately send an ERROR chunk
6443 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6444 * and discard the DATA chunk.
6446 sid = ntohs(data_hdr->stream);
6447 if (sid >= asoc->stream.incnt) {
6448 /* Mark tsn as received even though we drop it */
6449 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6451 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6453 sizeof(data_hdr->stream),
6456 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6458 return SCTP_IERROR_BAD_STREAM;
6461 /* Check to see if the SSN is possible for this TSN.
6462 * The biggest gap we can record is 4K wide. Since SSNs wrap
6463 * at an unsigned short, there is no way that an SSN can
6464 * wrap and for a valid TSN. We can simply check if the current
6465 * SSN is smaller then the next expected one. If it is, it wrapped
6468 ssn = ntohs(data_hdr->ssn);
6469 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
6470 return SCTP_IERROR_PROTO_VIOLATION;
6472 /* Send the data up to the user. Note: Schedule the
6473 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6474 * chunk needs the updated rwnd.
6476 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6478 return SCTP_IERROR_NO_ERROR;