1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2002 Intel Corp.
7 * Copyright (c) 2002 Nokia Corp.
9 * This is part of the SCTP Linux Kernel Implementation.
11 * These are the state functions for the state machine.
13 * Please send any bug reports or fixes you make to the
15 * lksctp developers <linux-sctp@vger.kernel.org>
17 * Written or modified by:
18 * La Monte H.P. Yarroll <piggy@acm.org>
19 * Karl Knutson <karl@athena.chicago.il.us>
20 * Mathew Kotowsky <kotowsky@sctp.org>
21 * Sridhar Samudrala <samudrala@us.ibm.com>
22 * Jon Grimm <jgrimm@us.ibm.com>
23 * Hui Huang <hui.huang@nokia.com>
24 * Dajiang Zhang <dajiang.zhang@nokia.com>
25 * Daisy Chang <daisyc@us.ibm.com>
26 * Ardelle Fan <ardelle.fan@intel.com>
27 * Ryan Layer <rmlayer@us.ibm.com>
28 * Kevin Gao <kevin.gao@intel.com>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/types.h>
34 #include <linux/kernel.h>
36 #include <linux/ipv6.h>
37 #include <linux/net.h>
38 #include <linux/inet.h>
39 #include <linux/slab.h>
41 #include <net/inet_ecn.h>
42 #include <linux/skbuff.h>
43 #include <net/sctp/sctp.h>
44 #include <net/sctp/sm.h>
45 #include <net/sctp/structs.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/sctp.h>
50 static struct sctp_packet *sctp_abort_pkt_new(
52 const struct sctp_endpoint *ep,
53 const struct sctp_association *asoc,
54 struct sctp_chunk *chunk,
55 const void *payload, size_t paylen);
56 static int sctp_eat_data(const struct sctp_association *asoc,
57 struct sctp_chunk *chunk,
58 struct sctp_cmd_seq *commands);
59 static struct sctp_packet *sctp_ootb_pkt_new(
61 const struct sctp_association *asoc,
62 const struct sctp_chunk *chunk);
63 static void sctp_send_stale_cookie_err(struct net *net,
64 const struct sctp_endpoint *ep,
65 const struct sctp_association *asoc,
66 const struct sctp_chunk *chunk,
67 struct sctp_cmd_seq *commands,
68 struct sctp_chunk *err_chunk);
69 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
71 const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const union sctp_subtype type,
75 struct sctp_cmd_seq *commands);
76 static enum sctp_disposition sctp_sf_shut_8_4_5(
78 const struct sctp_endpoint *ep,
79 const struct sctp_association *asoc,
80 const union sctp_subtype type,
82 struct sctp_cmd_seq *commands);
83 static enum sctp_disposition sctp_sf_tabort_8_4_8(
85 const struct sctp_endpoint *ep,
86 const struct sctp_association *asoc,
87 const union sctp_subtype type,
89 struct sctp_cmd_seq *commands);
90 static enum sctp_disposition sctp_sf_new_encap_port(
92 const struct sctp_endpoint *ep,
93 const struct sctp_association *asoc,
94 const union sctp_subtype type,
96 struct sctp_cmd_seq *commands);
97 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99 static enum sctp_disposition sctp_stop_t1_and_abort(
101 struct sctp_cmd_seq *commands,
102 __be16 error, int sk_err,
103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
106 static enum sctp_disposition sctp_sf_abort_violation(
108 const struct sctp_endpoint *ep,
109 const struct sctp_association *asoc,
111 struct sctp_cmd_seq *commands,
113 const size_t paylen);
115 static enum sctp_disposition sctp_sf_violation_chunklen(
117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const union sctp_subtype type,
121 struct sctp_cmd_seq *commands);
123 static enum sctp_disposition sctp_sf_violation_paramlen(
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const union sctp_subtype type,
128 void *arg, void *ext,
129 struct sctp_cmd_seq *commands);
131 static enum sctp_disposition sctp_sf_violation_ctsn(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const union sctp_subtype type,
137 struct sctp_cmd_seq *commands);
139 static enum sctp_disposition sctp_sf_violation_chunk(
141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const union sctp_subtype type,
145 struct sctp_cmd_seq *commands);
147 static enum sctp_ierror sctp_sf_authenticate(
148 const struct sctp_association *asoc,
149 struct sctp_chunk *chunk);
151 static enum sctp_disposition __sctp_sf_do_9_1_abort(
153 const struct sctp_endpoint *ep,
154 const struct sctp_association *asoc,
155 const union sctp_subtype type,
157 struct sctp_cmd_seq *commands);
159 static enum sctp_disposition
160 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
161 const struct sctp_association *asoc,
162 const union sctp_subtype type, void *arg,
163 struct sctp_cmd_seq *commands);
165 /* Small helper function that checks if the chunk length
166 * is of the appropriate length. The 'required_length' argument
167 * is set to be the size of a specific chunk we are testing.
168 * Return Values: true = Valid length
169 * false = Invalid length
172 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
173 __u16 required_length)
175 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
177 /* Previously already marked? */
178 if (unlikely(chunk->pdiscard))
180 if (unlikely(chunk_length < required_length))
186 /* Check for format error in an ABORT chunk */
187 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
189 struct sctp_errhdr *err;
191 sctp_walk_errors(err, chunk->chunk_hdr);
193 return (void *)err == (void *)chunk->chunk_end;
196 /**********************************************************
197 * These are the state functions for handling chunk events.
198 **********************************************************/
201 * Process the final SHUTDOWN COMPLETE.
203 * Section: 4 (C) (diagram), 9.2
204 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
205 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
206 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
207 * should stop the T2-shutdown timer and remove all knowledge of the
208 * association (and thus the association enters the CLOSED state).
210 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
211 * C) Rules for packet carrying SHUTDOWN COMPLETE:
213 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
214 * if the Verification Tag field of the packet matches its own tag and
215 * the T bit is not set
217 * it is set to its peer's tag and the T bit is set in the Chunk
219 * Otherwise, the receiver MUST silently discard the packet
220 * and take no further action. An endpoint MUST ignore the
221 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
224 * (endpoint, asoc, chunk)
227 * (asoc, reply_msg, msg_up, timers, counters)
229 * The return value is the disposition of the chunk.
231 enum sctp_disposition sctp_sf_do_4_C(struct net *net,
232 const struct sctp_endpoint *ep,
233 const struct sctp_association *asoc,
234 const union sctp_subtype type,
235 void *arg, struct sctp_cmd_seq *commands)
237 struct sctp_chunk *chunk = arg;
238 struct sctp_ulpevent *ev;
240 if (!sctp_vtag_verify_either(chunk, asoc))
241 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
243 /* RFC 2960 6.10 Bundling
245 * An endpoint MUST NOT bundle INIT, INIT ACK or
246 * SHUTDOWN COMPLETE with any other chunks.
248 if (!chunk->singleton)
249 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
251 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
252 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
253 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
256 /* RFC 2960 10.2 SCTP-to-ULP
258 * H) SHUTDOWN COMPLETE notification
260 * When SCTP completes the shutdown procedures (section 9.2) this
261 * notification is passed to the upper layer.
263 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
264 0, 0, 0, NULL, GFP_ATOMIC);
266 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
269 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
270 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
271 * not the chunk should be discarded. If the endpoint is in
272 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
273 * T2-shutdown timer and remove all knowledge of the
274 * association (and thus the association enters the CLOSED
277 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
278 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
280 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
281 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
283 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
284 SCTP_STATE(SCTP_STATE_CLOSED));
286 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
287 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
289 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
291 return SCTP_DISPOSITION_DELETE_TCB;
295 * Respond to a normal INIT chunk.
296 * We are the side that is being asked for an association.
298 * Section: 5.1 Normal Establishment of an Association, B
299 * B) "Z" shall respond immediately with an INIT ACK chunk. The
300 * destination IP address of the INIT ACK MUST be set to the source
301 * IP address of the INIT to which this INIT ACK is responding. In
302 * the response, besides filling in other parameters, "Z" must set the
303 * Verification Tag field to Tag_A, and also provide its own
304 * Verification Tag (Tag_Z) in the Initiate Tag field.
306 * Verification Tag: Must be 0.
309 * (endpoint, asoc, chunk)
312 * (asoc, reply_msg, msg_up, timers, counters)
314 * The return value is the disposition of the chunk.
316 enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
317 const struct sctp_endpoint *ep,
318 const struct sctp_association *asoc,
319 const union sctp_subtype type,
321 struct sctp_cmd_seq *commands)
323 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
324 struct sctp_unrecognized_param *unk_param;
325 struct sctp_association *new_asoc;
326 struct sctp_packet *packet;
330 * An endpoint MUST NOT bundle INIT, INIT ACK or
331 * SHUTDOWN COMPLETE with any other chunks.
334 * Furthermore, we require that the receiver of an INIT chunk MUST
335 * enforce these rules by silently discarding an arriving packet
336 * with an INIT chunk that is bundled with other chunks.
338 if (!chunk->singleton)
339 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
341 /* Make sure that the INIT chunk has a valid length.
342 * Normally, this would cause an ABORT with a Protocol Violation
343 * error, but since we don't have an association, we'll
344 * just discard the packet.
346 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
347 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
349 /* If the packet is an OOTB packet which is temporarily on the
350 * control endpoint, respond with an ABORT.
352 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
353 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
354 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
357 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
360 if (chunk->sctp_hdr->vtag != 0)
361 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
363 /* If the INIT is coming toward a closing socket, we'll send back
364 * and ABORT. Essentially, this catches the race of INIT being
365 * backloged to the socket at the same time as the user issues close().
366 * Since the socket and all its associations are going away, we
367 * can treat this OOTB
369 if (sctp_sstate(ep->base.sk, CLOSING))
370 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
372 /* Verify the INIT chunk before processing it. */
374 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
375 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
377 /* This chunk contains fatal error. It is to be discarded.
378 * Send an ABORT, with causes if there is any.
381 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
382 (__u8 *)(err_chunk->chunk_hdr) +
383 sizeof(struct sctp_chunkhdr),
384 ntohs(err_chunk->chunk_hdr->length) -
385 sizeof(struct sctp_chunkhdr));
387 sctp_chunk_free(err_chunk);
390 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
391 SCTP_PACKET(packet));
392 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
393 return SCTP_DISPOSITION_CONSUME;
395 return SCTP_DISPOSITION_NOMEM;
398 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
403 /* Grab the INIT header. */
404 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
406 /* Tag the variable length parameters. */
407 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
409 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
413 /* Update socket peer label if first association. */
414 if (security_sctp_assoc_request(new_asoc, chunk->skb)) {
415 sctp_association_free(new_asoc);
416 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
419 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
420 sctp_scope(sctp_source(chunk)),
424 /* The call, sctp_process_init(), can fail on memory allocation. */
425 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
426 (struct sctp_init_chunk *)chunk->chunk_hdr,
430 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
432 /* If there are errors need to be reported for unknown parameters,
433 * make sure to reserve enough room in the INIT ACK for them.
437 len = ntohs(err_chunk->chunk_hdr->length) -
438 sizeof(struct sctp_chunkhdr);
440 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
444 /* If there are errors need to be reported for unknown parameters,
445 * include them in the outgoing INIT ACK as "Unrecognized parameter"
449 /* Get the "Unrecognized parameter" parameter(s) out of the
450 * ERROR chunk generated by sctp_verify_init(). Since the
451 * error cause code for "unknown parameter" and the
452 * "Unrecognized parameter" type is the same, we can
453 * construct the parameters in INIT ACK by copying the
456 unk_param = (struct sctp_unrecognized_param *)
457 ((__u8 *)(err_chunk->chunk_hdr) +
458 sizeof(struct sctp_chunkhdr));
459 /* Replace the cause code with the "Unrecognized parameter"
462 sctp_addto_chunk(repl, len, unk_param);
463 sctp_chunk_free(err_chunk);
466 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
468 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
471 * Note: After sending out INIT ACK with the State Cookie parameter,
472 * "Z" MUST NOT allocate any resources, nor keep any states for the
473 * new association. Otherwise, "Z" will be vulnerable to resource
476 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
478 return SCTP_DISPOSITION_DELETE_TCB;
481 sctp_association_free(new_asoc);
484 sctp_chunk_free(err_chunk);
485 return SCTP_DISPOSITION_NOMEM;
489 * Respond to a normal INIT ACK chunk.
490 * We are the side that is initiating the association.
492 * Section: 5.1 Normal Establishment of an Association, C
493 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
494 * timer and leave COOKIE-WAIT state. "A" shall then send the State
495 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
496 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
498 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
499 * DATA chunks, but it MUST be the first chunk in the packet and
500 * until the COOKIE ACK is returned the sender MUST NOT send any
501 * other packets to the peer.
503 * Verification Tag: 3.3.3
504 * If the value of the Initiate Tag in a received INIT ACK chunk is
505 * found to be 0, the receiver MUST treat it as an error and close the
506 * association by transmitting an ABORT.
509 * (endpoint, asoc, chunk)
512 * (asoc, reply_msg, msg_up, timers, counters)
514 * The return value is the disposition of the chunk.
516 enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
517 const struct sctp_endpoint *ep,
518 const struct sctp_association *asoc,
519 const union sctp_subtype type,
521 struct sctp_cmd_seq *commands)
523 struct sctp_init_chunk *initchunk;
524 struct sctp_chunk *chunk = arg;
525 struct sctp_chunk *err_chunk;
526 struct sctp_packet *packet;
528 if (!sctp_vtag_verify(chunk, asoc))
529 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
532 * An endpoint MUST NOT bundle INIT, INIT ACK or
533 * SHUTDOWN COMPLETE with any other chunks.
535 if (!chunk->singleton)
536 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
538 /* Make sure that the INIT-ACK chunk has a valid length */
539 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
540 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
542 /* Grab the INIT header. */
543 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
545 /* Verify the INIT chunk before processing it. */
547 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
548 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
551 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
553 /* This chunk contains fatal error. It is to be discarded.
554 * Send an ABORT, with causes. If there are no causes,
555 * then there wasn't enough memory. Just terminate
559 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
560 (__u8 *)(err_chunk->chunk_hdr) +
561 sizeof(struct sctp_chunkhdr),
562 ntohs(err_chunk->chunk_hdr->length) -
563 sizeof(struct sctp_chunkhdr));
565 sctp_chunk_free(err_chunk);
568 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
569 SCTP_PACKET(packet));
570 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
571 error = SCTP_ERROR_INV_PARAM;
575 /* SCTP-AUTH, Section 6.3:
576 * It should be noted that if the receiver wants to tear
577 * down an association in an authenticated way only, the
578 * handling of malformed packets should not result in
579 * tearing down the association.
581 * This means that if we only want to abort associations
582 * in an authenticated way (i.e AUTH+ABORT), then we
583 * can't destroy this association just because the packet
586 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
587 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
589 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
590 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
591 asoc, chunk->transport);
594 /* Tag the variable length parameters. Note that we never
595 * convert the parameters in an INIT chunk.
597 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
599 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
601 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
602 SCTP_PEER_INIT(initchunk));
604 /* Reset init error count upon receipt of INIT-ACK. */
605 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
607 /* 5.1 C) "A" shall stop the T1-init timer and leave
608 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
609 * timer, and enter the COOKIE-ECHOED state.
611 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
612 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
613 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
614 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
615 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
616 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
618 /* SCTP-AUTH: generate the association shared keys so that
619 * we can potentially sign the COOKIE-ECHO.
621 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
623 /* 5.1 C) "A" shall then send the State Cookie received in the
624 * INIT ACK chunk in a COOKIE ECHO chunk, ...
626 /* If there is any errors to report, send the ERROR chunk generated
627 * for unknown parameters as well.
629 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
630 SCTP_CHUNK(err_chunk));
632 return SCTP_DISPOSITION_CONSUME;
635 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
636 const struct sctp_association *asoc)
638 struct sctp_chunk auth;
640 if (!chunk->auth_chunk)
643 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
644 * is supposed to be authenticated and we have to do delayed
645 * authentication. We've just recreated the association using
646 * the information in the cookie and now it's much easier to
647 * do the authentication.
650 /* Make sure that we and the peer are AUTH capable */
651 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
654 /* set-up our fake chunk so that we can process it */
655 auth.skb = chunk->auth_chunk;
656 auth.asoc = chunk->asoc;
657 auth.sctp_hdr = chunk->sctp_hdr;
658 auth.chunk_hdr = (struct sctp_chunkhdr *)
659 skb_push(chunk->auth_chunk,
660 sizeof(struct sctp_chunkhdr));
661 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
662 auth.transport = chunk->transport;
664 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
668 * Respond to a normal COOKIE ECHO chunk.
669 * We are the side that is being asked for an association.
671 * Section: 5.1 Normal Establishment of an Association, D
672 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
673 * with a COOKIE ACK chunk after building a TCB and moving to
674 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
675 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
676 * chunk MUST be the first chunk in the packet.
678 * IMPLEMENTATION NOTE: An implementation may choose to send the
679 * Communication Up notification to the SCTP user upon reception
680 * of a valid COOKIE ECHO chunk.
682 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
683 * D) Rules for packet carrying a COOKIE ECHO
685 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
686 * Initial Tag received in the INIT ACK.
688 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
691 * (endpoint, asoc, chunk)
694 * (asoc, reply_msg, msg_up, timers, counters)
696 * The return value is the disposition of the chunk.
698 enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
699 const struct sctp_endpoint *ep,
700 const struct sctp_association *asoc,
701 const union sctp_subtype type,
703 struct sctp_cmd_seq *commands)
705 struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
706 struct sctp_association *new_asoc;
707 struct sctp_init_chunk *peer_init;
708 struct sctp_chunk *chunk = arg;
709 struct sctp_chunk *err_chk_p;
710 struct sctp_chunk *repl;
714 if (asoc && !sctp_vtag_verify(chunk, asoc))
715 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
717 /* If the packet is an OOTB packet which is temporarily on the
718 * control endpoint, respond with an ABORT.
720 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
721 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
722 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
725 /* Make sure that the COOKIE_ECHO chunk has a valid length.
726 * In this case, we check that we have enough for at least a
727 * chunk header. More detailed verification is done
728 * in sctp_unpack_cookie().
730 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
731 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
734 /* If the endpoint is not listening or if the number of associations
735 * on the TCP-style socket exceed the max backlog, respond with an
739 if (!sctp_sstate(sk, LISTENING) ||
740 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
741 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
743 /* "Decode" the chunk. We have no optional parameters so we
746 chunk->subh.cookie_hdr =
747 (struct sctp_signed_cookie *)chunk->skb->data;
748 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
749 sizeof(struct sctp_chunkhdr)))
752 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
753 * "Z" will reply with a COOKIE ACK chunk after building a TCB
754 * and moving to the ESTABLISHED state.
756 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
760 * If the re-build failed, what is the proper error path
763 * [We should abort the association. --piggy]
766 /* FIXME: Several errors are possible. A bad cookie should
767 * be silently discarded, but think about logging it too.
770 case -SCTP_IERROR_NOMEM:
773 case -SCTP_IERROR_STALE_COOKIE:
774 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
776 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
778 case -SCTP_IERROR_BAD_SIG:
780 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
784 if (security_sctp_assoc_request(new_asoc, chunk->head_skb ?: chunk->skb)) {
785 sctp_association_free(new_asoc);
786 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
789 /* Delay state machine commands until later.
791 * Re-build the bind address for the association is done in
792 * the sctp_unpack_cookie() already.
794 /* This is a brand-new association, so these are not yet side
795 * effects--it is safe to run them here.
797 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
799 if (!sctp_process_init(new_asoc, chunk,
800 &chunk->subh.cookie_hdr->c.peer_addr,
801 peer_init, GFP_ATOMIC))
804 /* SCTP-AUTH: Now that we've populate required fields in
805 * sctp_process_init, set up the association shared keys as
806 * necessary so that we can potentially authenticate the ACK
808 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
812 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
813 sctp_association_free(new_asoc);
814 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
817 repl = sctp_make_cookie_ack(new_asoc, chunk);
821 /* RFC 2960 5.1 Normal Establishment of an Association
823 * D) IMPLEMENTATION NOTE: An implementation may choose to
824 * send the Communication Up notification to the SCTP user
825 * upon reception of a valid COOKIE ECHO chunk.
827 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
828 new_asoc->c.sinit_num_ostreams,
829 new_asoc->c.sinit_max_instreams,
834 /* Sockets API Draft Section 5.3.1.6
835 * When a peer sends a Adaptation Layer Indication parameter , SCTP
836 * delivers this notification to inform the application that of the
837 * peers requested adaptation layer.
839 if (new_asoc->peer.adaptation_ind) {
840 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
846 if (!new_asoc->peer.auth_capable) {
847 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
854 /* Add all the state machine commands now since we've created
855 * everything. This way we don't introduce memory corruptions
856 * during side-effect processing and correctly count established
859 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
860 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
861 SCTP_STATE(SCTP_STATE_ESTABLISHED));
862 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
863 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
864 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
866 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
867 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
868 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
870 /* This will send the COOKIE ACK */
871 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
873 /* Queue the ASSOC_CHANGE event */
874 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
876 /* Send up the Adaptation Layer Indication event */
878 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
879 SCTP_ULPEVENT(ai_ev));
882 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
883 SCTP_ULPEVENT(auth_ev));
885 return SCTP_DISPOSITION_CONSUME;
888 sctp_ulpevent_free(ai_ev);
890 sctp_ulpevent_free(ev);
892 sctp_chunk_free(repl);
894 sctp_association_free(new_asoc);
896 return SCTP_DISPOSITION_NOMEM;
900 * Respond to a normal COOKIE ACK chunk.
901 * We are the side that is asking for an association.
903 * RFC 2960 5.1 Normal Establishment of an Association
905 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
906 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
907 * timer. It may also notify its ULP about the successful
908 * establishment of the association with a Communication Up
909 * notification (see Section 10).
913 * (endpoint, asoc, chunk)
916 * (asoc, reply_msg, msg_up, timers, counters)
918 * The return value is the disposition of the chunk.
920 enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
921 const struct sctp_endpoint *ep,
922 const struct sctp_association *asoc,
923 const union sctp_subtype type,
925 struct sctp_cmd_seq *commands)
927 struct sctp_chunk *chunk = arg;
928 struct sctp_ulpevent *ev;
930 if (!sctp_vtag_verify(chunk, asoc))
931 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
933 /* Set peer label for connection. */
934 if (security_sctp_assoc_established((struct sctp_association *)asoc,
935 chunk->head_skb ?: chunk->skb))
936 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
938 /* Verify that the chunk length for the COOKIE-ACK is OK.
939 * If we don't do this, any bundled chunks may be junked.
941 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
942 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
945 /* Reset init error count upon receipt of COOKIE-ACK,
946 * to avoid problems with the management of this
947 * counter in stale cookie situations when a transition back
948 * from the COOKIE-ECHOED state to the COOKIE-WAIT
949 * state is performed.
951 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
953 /* RFC 2960 5.1 Normal Establishment of an Association
955 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
956 * from the COOKIE-ECHOED state to the ESTABLISHED state,
957 * stopping the T1-cookie timer.
959 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
960 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
961 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
962 SCTP_STATE(SCTP_STATE_ESTABLISHED));
963 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
964 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
965 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
966 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
967 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
968 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
970 /* It may also notify its ULP about the successful
971 * establishment of the association with a Communication Up
972 * notification (see Section 10).
974 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
975 0, asoc->c.sinit_num_ostreams,
976 asoc->c.sinit_max_instreams,
982 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
984 /* Sockets API Draft Section 5.3.1.6
985 * When a peer sends a Adaptation Layer Indication parameter , SCTP
986 * delivers this notification to inform the application that of the
987 * peers requested adaptation layer.
989 if (asoc->peer.adaptation_ind) {
990 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
994 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
998 if (!asoc->peer.auth_capable) {
999 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
1003 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1007 return SCTP_DISPOSITION_CONSUME;
1009 return SCTP_DISPOSITION_NOMEM;
1012 /* Generate and sendout a heartbeat packet. */
1013 static enum sctp_disposition sctp_sf_heartbeat(
1014 const struct sctp_endpoint *ep,
1015 const struct sctp_association *asoc,
1016 const union sctp_subtype type,
1018 struct sctp_cmd_seq *commands)
1020 struct sctp_transport *transport = (struct sctp_transport *) arg;
1021 struct sctp_chunk *reply;
1023 /* Send a heartbeat to our peer. */
1024 reply = sctp_make_heartbeat(asoc, transport, 0);
1026 return SCTP_DISPOSITION_NOMEM;
1028 /* Set rto_pending indicating that an RTT measurement
1029 * is started with this heartbeat chunk.
1031 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1032 SCTP_TRANSPORT(transport));
1034 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1035 return SCTP_DISPOSITION_CONSUME;
1038 /* Generate a HEARTBEAT packet on the given transport. */
1039 enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1040 const struct sctp_endpoint *ep,
1041 const struct sctp_association *asoc,
1042 const union sctp_subtype type,
1044 struct sctp_cmd_seq *commands)
1046 struct sctp_transport *transport = (struct sctp_transport *) arg;
1048 if (asoc->overall_error_count >= asoc->max_retrans) {
1049 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1050 SCTP_ERROR(ETIMEDOUT));
1051 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1052 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1053 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1054 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1055 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1056 return SCTP_DISPOSITION_DELETE_TCB;
1060 * The Sender-specific Heartbeat Info field should normally include
1061 * information about the sender's current time when this HEARTBEAT
1062 * chunk is sent and the destination transport address to which this
1063 * HEARTBEAT is sent (see Section 8.3).
1066 if (transport->param_flags & SPP_HB_ENABLE) {
1067 if (SCTP_DISPOSITION_NOMEM ==
1068 sctp_sf_heartbeat(ep, asoc, type, arg,
1070 return SCTP_DISPOSITION_NOMEM;
1072 /* Set transport error counter and association error counter
1073 * when sending heartbeat.
1075 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1076 SCTP_TRANSPORT(transport));
1078 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1079 SCTP_TRANSPORT(transport));
1080 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1081 SCTP_TRANSPORT(transport));
1083 return SCTP_DISPOSITION_CONSUME;
1086 /* resend asoc strreset_chunk. */
1087 enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1088 const struct sctp_endpoint *ep,
1089 const struct sctp_association *asoc,
1090 const union sctp_subtype type,
1092 struct sctp_cmd_seq *commands)
1094 struct sctp_transport *transport = arg;
1096 if (asoc->overall_error_count >= asoc->max_retrans) {
1097 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1098 SCTP_ERROR(ETIMEDOUT));
1099 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1100 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1101 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1102 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1103 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1104 return SCTP_DISPOSITION_DELETE_TCB;
1107 sctp_chunk_hold(asoc->strreset_chunk);
1108 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1109 SCTP_CHUNK(asoc->strreset_chunk));
1110 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1112 return SCTP_DISPOSITION_CONSUME;
1115 /* send hb chunk with padding for PLPMUTD. */
1116 enum sctp_disposition sctp_sf_send_probe(struct net *net,
1117 const struct sctp_endpoint *ep,
1118 const struct sctp_association *asoc,
1119 const union sctp_subtype type,
1121 struct sctp_cmd_seq *commands)
1123 struct sctp_transport *transport = (struct sctp_transport *)arg;
1124 struct sctp_chunk *reply;
1126 if (!sctp_transport_pl_enabled(transport))
1127 return SCTP_DISPOSITION_CONSUME;
1129 sctp_transport_pl_send(transport);
1130 reply = sctp_make_heartbeat(asoc, transport, transport->pl.probe_size);
1132 return SCTP_DISPOSITION_NOMEM;
1133 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1134 sctp_add_cmd_sf(commands, SCTP_CMD_PROBE_TIMER_UPDATE,
1135 SCTP_TRANSPORT(transport));
1137 return SCTP_DISPOSITION_CONSUME;
1141 * Process an heartbeat request.
1143 * Section: 8.3 Path Heartbeat
1144 * The receiver of the HEARTBEAT should immediately respond with a
1145 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1146 * from the received HEARTBEAT chunk.
1148 * Verification Tag: 8.5 Verification Tag [Normal verification]
1149 * When receiving an SCTP packet, the endpoint MUST ensure that the
1150 * value in the Verification Tag field of the received SCTP packet
1151 * matches its own Tag. If the received Verification Tag value does not
1152 * match the receiver's own tag value, the receiver shall silently
1153 * discard the packet and shall not process it any further except for
1154 * those cases listed in Section 8.5.1 below.
1157 * (endpoint, asoc, chunk)
1160 * (asoc, reply_msg, msg_up, timers, counters)
1162 * The return value is the disposition of the chunk.
1164 enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1165 const struct sctp_endpoint *ep,
1166 const struct sctp_association *asoc,
1167 const union sctp_subtype type,
1168 void *arg, struct sctp_cmd_seq *commands)
1170 struct sctp_paramhdr *param_hdr;
1171 struct sctp_chunk *chunk = arg;
1172 struct sctp_chunk *reply;
1175 if (!sctp_vtag_verify(chunk, asoc))
1176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1178 /* Make sure that the HEARTBEAT chunk has a valid length. */
1179 if (!sctp_chunk_length_valid(chunk,
1180 sizeof(struct sctp_heartbeat_chunk)))
1181 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1184 /* 8.3 The receiver of the HEARTBEAT should immediately
1185 * respond with a HEARTBEAT ACK that contains the Heartbeat
1186 * Information field copied from the received HEARTBEAT chunk.
1188 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1189 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1190 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1192 if (ntohs(param_hdr->length) > paylen)
1193 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1194 param_hdr, commands);
1196 if (!pskb_pull(chunk->skb, paylen))
1199 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1203 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1204 return SCTP_DISPOSITION_CONSUME;
1207 return SCTP_DISPOSITION_NOMEM;
1211 * Process the returning HEARTBEAT ACK.
1213 * Section: 8.3 Path Heartbeat
1214 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1215 * should clear the error counter of the destination transport
1216 * address to which the HEARTBEAT was sent, and mark the destination
1217 * transport address as active if it is not so marked. The endpoint may
1218 * optionally report to the upper layer when an inactive destination
1219 * address is marked as active due to the reception of the latest
1220 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1221 * clear the association overall error count as well (as defined
1224 * The receiver of the HEARTBEAT ACK should also perform an RTT
1225 * measurement for that destination transport address using the time
1226 * value carried in the HEARTBEAT ACK chunk.
1228 * Verification Tag: 8.5 Verification Tag [Normal verification]
1231 * (endpoint, asoc, chunk)
1234 * (asoc, reply_msg, msg_up, timers, counters)
1236 * The return value is the disposition of the chunk.
1238 enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1239 const struct sctp_endpoint *ep,
1240 const struct sctp_association *asoc,
1241 const union sctp_subtype type,
1243 struct sctp_cmd_seq *commands)
1245 struct sctp_sender_hb_info *hbinfo;
1246 struct sctp_chunk *chunk = arg;
1247 struct sctp_transport *link;
1248 unsigned long max_interval;
1249 union sctp_addr from_addr;
1251 if (!sctp_vtag_verify(chunk, asoc))
1252 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1254 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1255 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1257 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1260 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1261 /* Make sure that the length of the parameter is what we expect */
1262 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1263 return SCTP_DISPOSITION_DISCARD;
1265 from_addr = hbinfo->daddr;
1266 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1268 /* This should never happen, but lets log it if so. */
1269 if (unlikely(!link)) {
1270 if (from_addr.sa.sa_family == AF_INET6) {
1271 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1274 &from_addr.v6.sin6_addr);
1276 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1279 &from_addr.v4.sin_addr.s_addr);
1281 return SCTP_DISPOSITION_DISCARD;
1284 /* Validate the 64-bit random nonce. */
1285 if (hbinfo->hb_nonce != link->hb_nonce)
1286 return SCTP_DISPOSITION_DISCARD;
1288 if (hbinfo->probe_size) {
1289 if (hbinfo->probe_size != link->pl.probe_size ||
1290 !sctp_transport_pl_enabled(link))
1291 return SCTP_DISPOSITION_DISCARD;
1293 if (sctp_transport_pl_recv(link))
1294 return SCTP_DISPOSITION_CONSUME;
1296 return sctp_sf_send_probe(net, ep, asoc, type, link, commands);
1299 max_interval = link->hbinterval + link->rto;
1301 /* Check if the timestamp looks valid. */
1302 if (time_after(hbinfo->sent_at, jiffies) ||
1303 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1304 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1305 "for transport:%p\n", __func__, link);
1307 return SCTP_DISPOSITION_DISCARD;
1310 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1311 * the HEARTBEAT should clear the error counter of the
1312 * destination transport address to which the HEARTBEAT was
1313 * sent and mark the destination transport address as active if
1314 * it is not so marked.
1316 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1318 return SCTP_DISPOSITION_CONSUME;
1321 /* Helper function to send out an abort for the restart
1324 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1325 struct sctp_chunk *init,
1326 struct sctp_cmd_seq *commands)
1328 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1329 union sctp_addr_param *addrparm;
1330 struct sctp_errhdr *errhdr;
1331 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1332 struct sctp_endpoint *ep;
1333 struct sctp_packet *pkt;
1336 /* Build the error on the stack. We are way to malloc crazy
1337 * throughout the code today.
1339 errhdr = (struct sctp_errhdr *)buffer;
1340 addrparm = (union sctp_addr_param *)errhdr->variable;
1342 /* Copy into a parm format. */
1343 len = af->to_addr_param(ssa, addrparm);
1344 len += sizeof(*errhdr);
1346 errhdr->cause = SCTP_ERROR_RESTART;
1347 errhdr->length = htons(len);
1349 /* Assign to the control socket. */
1350 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1352 /* Association is NULL since this may be a restart attack and we
1353 * want to send back the attacker's vtag.
1355 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1359 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1361 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1363 /* Discard the rest of the inbound packet. */
1364 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1367 /* Even if there is no memory, treat as a failure so
1368 * the packet will get dropped.
1373 static bool list_has_sctp_addr(const struct list_head *list,
1374 union sctp_addr *ipaddr)
1376 struct sctp_transport *addr;
1378 list_for_each_entry(addr, list, transports) {
1379 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1385 /* A restart is occurring, check to make sure no new addresses
1386 * are being added as we may be under a takeover attack.
1388 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1389 const struct sctp_association *asoc,
1390 struct sctp_chunk *init,
1391 struct sctp_cmd_seq *commands)
1393 struct net *net = new_asoc->base.net;
1394 struct sctp_transport *new_addr;
1397 /* Implementor's Guide - Section 5.2.2
1399 * Before responding the endpoint MUST check to see if the
1400 * unexpected INIT adds new addresses to the association. If new
1401 * addresses are added to the association, the endpoint MUST respond
1405 /* Search through all current addresses and make sure
1406 * we aren't adding any new ones.
1408 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1410 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1411 &new_addr->ipaddr)) {
1412 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1419 /* Return success if all addresses were found. */
1423 /* Populate the verification/tie tags based on overlapping INIT
1426 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1428 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1429 const struct sctp_association *asoc)
1431 switch (asoc->state) {
1433 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1435 case SCTP_STATE_COOKIE_WAIT:
1436 new_asoc->c.my_vtag = asoc->c.my_vtag;
1437 new_asoc->c.my_ttag = asoc->c.my_vtag;
1438 new_asoc->c.peer_ttag = 0;
1441 case SCTP_STATE_COOKIE_ECHOED:
1442 new_asoc->c.my_vtag = asoc->c.my_vtag;
1443 new_asoc->c.my_ttag = asoc->c.my_vtag;
1444 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1447 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1448 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1451 new_asoc->c.my_ttag = asoc->c.my_vtag;
1452 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1456 /* Other parameters for the endpoint SHOULD be copied from the
1457 * existing parameters of the association (e.g. number of
1458 * outbound streams) into the INIT ACK and cookie.
1460 new_asoc->rwnd = asoc->rwnd;
1461 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1462 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1463 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1467 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1470 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1472 * Returns value representing action to be taken. These action values
1473 * correspond to Action/Description values in RFC 2960, Table 2.
1475 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1476 const struct sctp_association *asoc)
1478 /* In this case, the peer may have restarted. */
1479 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1480 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1481 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1482 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1485 /* Collision case B. */
1486 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1487 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1488 (0 == asoc->c.peer_vtag))) {
1492 /* Collision case D. */
1493 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1494 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1497 /* Collision case C. */
1498 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1499 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1500 (0 == new_asoc->c.my_ttag) &&
1501 (0 == new_asoc->c.peer_ttag))
1504 /* No match to any of the special cases; discard this packet. */
1508 /* Common helper routine for both duplicate and simultaneous INIT
1511 static enum sctp_disposition sctp_sf_do_unexpected_init(
1513 const struct sctp_endpoint *ep,
1514 const struct sctp_association *asoc,
1515 const union sctp_subtype type,
1517 struct sctp_cmd_seq *commands)
1519 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1520 struct sctp_unrecognized_param *unk_param;
1521 struct sctp_association *new_asoc;
1522 enum sctp_disposition retval;
1523 struct sctp_packet *packet;
1527 * An endpoint MUST NOT bundle INIT, INIT ACK or
1528 * SHUTDOWN COMPLETE with any other chunks.
1531 * Furthermore, we require that the receiver of an INIT chunk MUST
1532 * enforce these rules by silently discarding an arriving packet
1533 * with an INIT chunk that is bundled with other chunks.
1535 if (!chunk->singleton)
1536 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1538 /* Make sure that the INIT chunk has a valid length. */
1539 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1540 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1542 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1545 if (chunk->sctp_hdr->vtag != 0)
1546 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1548 if (SCTP_INPUT_CB(chunk->skb)->encap_port != chunk->transport->encap_port)
1549 return sctp_sf_new_encap_port(net, ep, asoc, type, arg, commands);
1551 /* Grab the INIT header. */
1552 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1554 /* Tag the variable length parameters. */
1555 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1557 /* Verify the INIT chunk before processing it. */
1559 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1560 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1562 /* This chunk contains fatal error. It is to be discarded.
1563 * Send an ABORT, with causes if there is any.
1566 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1567 (__u8 *)(err_chunk->chunk_hdr) +
1568 sizeof(struct sctp_chunkhdr),
1569 ntohs(err_chunk->chunk_hdr->length) -
1570 sizeof(struct sctp_chunkhdr));
1573 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1574 SCTP_PACKET(packet));
1575 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1576 retval = SCTP_DISPOSITION_CONSUME;
1578 retval = SCTP_DISPOSITION_NOMEM;
1582 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1588 * Other parameters for the endpoint SHOULD be copied from the
1589 * existing parameters of the association (e.g. number of
1590 * outbound streams) into the INIT ACK and cookie.
1591 * FIXME: We are copying parameters from the endpoint not the
1594 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1598 /* Update socket peer label if first association. */
1599 if (security_sctp_assoc_request(new_asoc, chunk->skb)) {
1600 sctp_association_free(new_asoc);
1601 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1604 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1605 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1608 /* In the outbound INIT ACK the endpoint MUST copy its current
1609 * Verification Tag and Peers Verification tag into a reserved
1610 * place (local tie-tag and per tie-tag) within the state cookie.
1612 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1613 (struct sctp_init_chunk *)chunk->chunk_hdr,
1617 /* Make sure no new addresses are being added during the
1618 * restart. Do not do this check for COOKIE-WAIT state,
1619 * since there are no peer addresses to check against.
1620 * Upon return an ABORT will have been sent if needed.
1622 if (!sctp_state(asoc, COOKIE_WAIT)) {
1623 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1625 retval = SCTP_DISPOSITION_CONSUME;
1630 sctp_tietags_populate(new_asoc, asoc);
1632 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1634 /* If there are errors need to be reported for unknown parameters,
1635 * make sure to reserve enough room in the INIT ACK for them.
1639 len = ntohs(err_chunk->chunk_hdr->length) -
1640 sizeof(struct sctp_chunkhdr);
1643 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1647 /* If there are errors need to be reported for unknown parameters,
1648 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1652 /* Get the "Unrecognized parameter" parameter(s) out of the
1653 * ERROR chunk generated by sctp_verify_init(). Since the
1654 * error cause code for "unknown parameter" and the
1655 * "Unrecognized parameter" type is the same, we can
1656 * construct the parameters in INIT ACK by copying the
1657 * ERROR causes over.
1659 unk_param = (struct sctp_unrecognized_param *)
1660 ((__u8 *)(err_chunk->chunk_hdr) +
1661 sizeof(struct sctp_chunkhdr));
1662 /* Replace the cause code with the "Unrecognized parameter"
1665 sctp_addto_chunk(repl, len, unk_param);
1668 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1669 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1672 * Note: After sending out INIT ACK with the State Cookie parameter,
1673 * "Z" MUST NOT allocate any resources for this new association.
1674 * Otherwise, "Z" will be vulnerable to resource attacks.
1676 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1677 retval = SCTP_DISPOSITION_CONSUME;
1682 retval = SCTP_DISPOSITION_NOMEM;
1685 sctp_association_free(new_asoc);
1688 sctp_chunk_free(err_chunk);
1693 * Handle simultaneous INIT.
1694 * This means we started an INIT and then we got an INIT request from
1697 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1698 * This usually indicates an initialization collision, i.e., each
1699 * endpoint is attempting, at about the same time, to establish an
1700 * association with the other endpoint.
1702 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1703 * endpoint MUST respond with an INIT ACK using the same parameters it
1704 * sent in its original INIT chunk (including its Verification Tag,
1705 * unchanged). These original parameters are combined with those from the
1706 * newly received INIT chunk. The endpoint shall also generate a State
1707 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1708 * INIT to calculate the State Cookie.
1710 * After that, the endpoint MUST NOT change its state, the T1-init
1711 * timer shall be left running and the corresponding TCB MUST NOT be
1712 * destroyed. The normal procedures for handling State Cookies when
1713 * a TCB exists will resolve the duplicate INITs to a single association.
1715 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1716 * its Tie-Tags with the Tag information of itself and its peer (see
1717 * section 5.2.2 for a description of the Tie-Tags).
1719 * Verification Tag: Not explicit, but an INIT can not have a valid
1720 * verification tag, so we skip the check.
1723 * (endpoint, asoc, chunk)
1726 * (asoc, reply_msg, msg_up, timers, counters)
1728 * The return value is the disposition of the chunk.
1730 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1732 const struct sctp_endpoint *ep,
1733 const struct sctp_association *asoc,
1734 const union sctp_subtype type,
1736 struct sctp_cmd_seq *commands)
1738 /* Call helper to do the real work for both simultaneous and
1739 * duplicate INIT chunk handling.
1741 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1745 * Handle duplicated INIT messages. These are usually delayed
1748 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1749 * COOKIE-ECHOED and COOKIE-WAIT
1751 * Unless otherwise stated, upon reception of an unexpected INIT for
1752 * this association, the endpoint shall generate an INIT ACK with a
1753 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1754 * current Verification Tag and peer's Verification Tag into a reserved
1755 * place within the state cookie. We shall refer to these locations as
1756 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1757 * containing this INIT ACK MUST carry a Verification Tag value equal to
1758 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1759 * MUST contain a new Initiation Tag (randomly generated see Section
1760 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1761 * existing parameters of the association (e.g. number of outbound
1762 * streams) into the INIT ACK and cookie.
1764 * After sending out the INIT ACK, the endpoint shall take no further
1765 * actions, i.e., the existing association, including its current state,
1766 * and the corresponding TCB MUST NOT be changed.
1768 * Note: Only when a TCB exists and the association is not in a COOKIE-
1769 * WAIT state are the Tie-Tags populated. For a normal association INIT
1770 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1771 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1772 * State Cookie are populated as specified in section 5.2.1.
1774 * Verification Tag: Not specified, but an INIT has no way of knowing
1775 * what the verification tag could be, so we ignore it.
1778 * (endpoint, asoc, chunk)
1781 * (asoc, reply_msg, msg_up, timers, counters)
1783 * The return value is the disposition of the chunk.
1785 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1787 const struct sctp_endpoint *ep,
1788 const struct sctp_association *asoc,
1789 const union sctp_subtype type,
1791 struct sctp_cmd_seq *commands)
1793 /* Call helper to do the real work for both simultaneous and
1794 * duplicate INIT chunk handling.
1796 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1801 * Unexpected INIT-ACK handler.
1804 * If an INIT ACK received by an endpoint in any state other than the
1805 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1806 * An unexpected INIT ACK usually indicates the processing of an old or
1807 * duplicated INIT chunk.
1809 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1811 const struct sctp_endpoint *ep,
1812 const struct sctp_association *asoc,
1813 const union sctp_subtype type,
1815 struct sctp_cmd_seq *commands)
1817 /* Per the above section, we'll discard the chunk if we have an
1818 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1820 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1821 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1823 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1826 static int sctp_sf_do_assoc_update(struct sctp_association *asoc,
1827 struct sctp_association *new,
1828 struct sctp_cmd_seq *cmds)
1830 struct net *net = asoc->base.net;
1831 struct sctp_chunk *abort;
1833 if (!sctp_assoc_update(asoc, new))
1836 abort = sctp_make_abort(asoc, NULL, sizeof(struct sctp_errhdr));
1838 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
1839 sctp_add_cmd_sf(cmds, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
1841 sctp_add_cmd_sf(cmds, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNABORTED));
1842 sctp_add_cmd_sf(cmds, SCTP_CMD_ASSOC_FAILED,
1843 SCTP_PERR(SCTP_ERROR_RSRC_LOW));
1844 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1845 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1850 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1853 * A) In this case, the peer may have restarted.
1855 static enum sctp_disposition sctp_sf_do_dupcook_a(
1857 const struct sctp_endpoint *ep,
1858 const struct sctp_association *asoc,
1859 struct sctp_chunk *chunk,
1860 struct sctp_cmd_seq *commands,
1861 struct sctp_association *new_asoc)
1863 struct sctp_init_chunk *peer_init;
1864 enum sctp_disposition disposition;
1865 struct sctp_ulpevent *ev;
1866 struct sctp_chunk *repl;
1867 struct sctp_chunk *err;
1869 /* new_asoc is a brand-new association, so these are not yet
1870 * side effects--it is safe to run them here.
1872 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1874 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1878 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1881 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1882 return SCTP_DISPOSITION_DISCARD;
1884 /* Make sure no new addresses are being added during the
1885 * restart. Though this is a pretty complicated attack
1886 * since you'd have to get inside the cookie.
1888 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1889 return SCTP_DISPOSITION_CONSUME;
1891 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1892 * the peer has restarted (Action A), it MUST NOT setup a new
1893 * association but instead resend the SHUTDOWN ACK and send an ERROR
1894 * chunk with a "Cookie Received while Shutting Down" error cause to
1897 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1898 disposition = __sctp_sf_do_9_2_reshutack(net, ep, asoc,
1899 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1901 if (SCTP_DISPOSITION_NOMEM == disposition)
1904 err = sctp_make_op_error(asoc, chunk,
1905 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1908 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1911 return SCTP_DISPOSITION_CONSUME;
1914 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1915 * data. Consider the optional choice of resending of this data.
1917 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1919 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1920 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1922 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1923 * and ASCONF-ACK cache.
1925 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1926 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1927 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1929 /* Update the content of current association. */
1930 if (sctp_sf_do_assoc_update((struct sctp_association *)asoc, new_asoc, commands))
1933 repl = sctp_make_cookie_ack(asoc, chunk);
1937 /* Report association restart to upper layer. */
1938 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1939 asoc->c.sinit_num_ostreams,
1940 asoc->c.sinit_max_instreams,
1945 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1946 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1947 sctp_state(asoc, SHUTDOWN_SENT)) &&
1948 (sctp_sstate(asoc->base.sk, CLOSING) ||
1949 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1950 /* If the socket has been closed by user, don't
1951 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1952 * bundled with COOKIE_ACK.
1954 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1955 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1956 SCTP_ST_CHUNK(0), repl,
1959 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1960 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1961 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1963 return SCTP_DISPOSITION_CONSUME;
1966 sctp_chunk_free(repl);
1968 return SCTP_DISPOSITION_NOMEM;
1971 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1974 * B) In this case, both sides may be attempting to start an association
1975 * at about the same time but the peer endpoint started its INIT
1976 * after responding to the local endpoint's INIT
1978 /* This case represents an initialization collision. */
1979 static enum sctp_disposition sctp_sf_do_dupcook_b(
1981 const struct sctp_endpoint *ep,
1982 const struct sctp_association *asoc,
1983 struct sctp_chunk *chunk,
1984 struct sctp_cmd_seq *commands,
1985 struct sctp_association *new_asoc)
1987 struct sctp_init_chunk *peer_init;
1988 struct sctp_chunk *repl;
1990 /* new_asoc is a brand-new association, so these are not yet
1991 * side effects--it is safe to run them here.
1993 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1994 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1998 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
2001 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
2002 return SCTP_DISPOSITION_DISCARD;
2004 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2005 SCTP_STATE(SCTP_STATE_ESTABLISHED));
2006 if (asoc->state < SCTP_STATE_ESTABLISHED)
2007 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2008 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
2010 /* Update the content of current association. */
2011 if (sctp_sf_do_assoc_update((struct sctp_association *)asoc, new_asoc, commands))
2014 repl = sctp_make_cookie_ack(asoc, chunk);
2018 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2020 /* RFC 2960 5.1 Normal Establishment of an Association
2022 * D) IMPLEMENTATION NOTE: An implementation may choose to
2023 * send the Communication Up notification to the SCTP user
2024 * upon reception of a valid COOKIE ECHO chunk.
2026 * Sadly, this needs to be implemented as a side-effect, because
2027 * we are not guaranteed to have set the association id of the real
2028 * association and so these notifications need to be delayed until
2029 * the association id is allocated.
2032 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
2034 /* Sockets API Draft Section 5.3.1.6
2035 * When a peer sends a Adaptation Layer Indication parameter , SCTP
2036 * delivers this notification to inform the application that of the
2037 * peers requested adaptation layer.
2039 * This also needs to be done as a side effect for the same reason as
2042 if (asoc->peer.adaptation_ind)
2043 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
2045 if (!asoc->peer.auth_capable)
2046 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
2048 return SCTP_DISPOSITION_CONSUME;
2051 return SCTP_DISPOSITION_NOMEM;
2054 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
2057 * C) In this case, the local endpoint's cookie has arrived late.
2058 * Before it arrived, the local endpoint sent an INIT and received an
2059 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
2060 * but a new tag of its own.
2062 /* This case represents an initialization collision. */
2063 static enum sctp_disposition sctp_sf_do_dupcook_c(
2065 const struct sctp_endpoint *ep,
2066 const struct sctp_association *asoc,
2067 struct sctp_chunk *chunk,
2068 struct sctp_cmd_seq *commands,
2069 struct sctp_association *new_asoc)
2071 /* The cookie should be silently discarded.
2072 * The endpoint SHOULD NOT change states and should leave
2073 * any timers running.
2075 return SCTP_DISPOSITION_DISCARD;
2078 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
2082 * D) When both local and remote tags match the endpoint should always
2083 * enter the ESTABLISHED state, if it has not already done so.
2085 /* This case represents an initialization collision. */
2086 static enum sctp_disposition sctp_sf_do_dupcook_d(
2088 const struct sctp_endpoint *ep,
2089 const struct sctp_association *asoc,
2090 struct sctp_chunk *chunk,
2091 struct sctp_cmd_seq *commands,
2092 struct sctp_association *new_asoc)
2094 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
2095 struct sctp_chunk *repl;
2097 /* Clarification from Implementor's Guide:
2098 * D) When both local and remote tags match the endpoint should
2099 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2100 * It should stop any cookie timer that may be running and send
2104 if (!sctp_auth_chunk_verify(net, chunk, asoc))
2105 return SCTP_DISPOSITION_DISCARD;
2107 /* Don't accidentally move back into established state. */
2108 if (asoc->state < SCTP_STATE_ESTABLISHED) {
2109 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2110 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2111 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2112 SCTP_STATE(SCTP_STATE_ESTABLISHED));
2113 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2114 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2117 /* RFC 2960 5.1 Normal Establishment of an Association
2119 * D) IMPLEMENTATION NOTE: An implementation may choose
2120 * to send the Communication Up notification to the
2121 * SCTP user upon reception of a valid COOKIE
2124 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2126 asoc->c.sinit_num_ostreams,
2127 asoc->c.sinit_max_instreams,
2132 /* Sockets API Draft Section 5.3.1.6
2133 * When a peer sends a Adaptation Layer Indication parameter,
2134 * SCTP delivers this notification to inform the application
2135 * that of the peers requested adaptation layer.
2137 if (asoc->peer.adaptation_ind) {
2138 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2145 if (!asoc->peer.auth_capable) {
2146 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2154 repl = sctp_make_cookie_ack(asoc, chunk);
2158 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2161 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2164 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2165 SCTP_ULPEVENT(ai_ev));
2167 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2168 SCTP_ULPEVENT(auth_ev));
2170 return SCTP_DISPOSITION_CONSUME;
2174 sctp_ulpevent_free(auth_ev);
2176 sctp_ulpevent_free(ai_ev);
2178 sctp_ulpevent_free(ev);
2179 return SCTP_DISPOSITION_NOMEM;
2183 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2184 * chunk was retransmitted and then delayed in the network.
2186 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2188 * Verification Tag: None. Do cookie validation.
2191 * (endpoint, asoc, chunk)
2194 * (asoc, reply_msg, msg_up, timers, counters)
2196 * The return value is the disposition of the chunk.
2198 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2200 const struct sctp_endpoint *ep,
2201 const struct sctp_association *asoc,
2202 const union sctp_subtype type,
2204 struct sctp_cmd_seq *commands)
2206 struct sctp_association *new_asoc;
2207 struct sctp_chunk *chunk = arg;
2208 enum sctp_disposition retval;
2209 struct sctp_chunk *err_chk_p;
2213 /* Make sure that the chunk has a valid length from the protocol
2214 * perspective. In this case check to make sure we have at least
2215 * enough for the chunk header. Cookie length verification is
2218 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr))) {
2219 if (!sctp_vtag_verify(chunk, asoc))
2221 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg, commands);
2224 /* "Decode" the chunk. We have no optional parameters so we
2225 * are in good shape.
2227 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2228 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2229 sizeof(struct sctp_chunkhdr)))
2232 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2233 * of a duplicate COOKIE ECHO match the Verification Tags of the
2234 * current association, consider the State Cookie valid even if
2235 * the lifespan is exceeded.
2237 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2241 * If the re-build failed, what is the proper error path
2244 * [We should abort the association. --piggy]
2247 /* FIXME: Several errors are possible. A bad cookie should
2248 * be silently discarded, but think about logging it too.
2251 case -SCTP_IERROR_NOMEM:
2254 case -SCTP_IERROR_STALE_COOKIE:
2255 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2257 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2258 case -SCTP_IERROR_BAD_SIG:
2260 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2264 /* Update socket peer label if first association. */
2265 if (security_sctp_assoc_request(new_asoc, chunk->head_skb ?: chunk->skb)) {
2266 sctp_association_free(new_asoc);
2267 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2270 /* Set temp so that it won't be added into hashtable */
2273 /* Compare the tie_tag in cookie with the verification tag of
2274 * current association.
2276 action = sctp_tietags_compare(new_asoc, asoc);
2279 case 'A': /* Association restart. */
2280 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2284 case 'B': /* Collision case B. */
2285 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2289 case 'C': /* Collision case C. */
2290 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2294 case 'D': /* Collision case D. */
2295 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2299 default: /* Discard packet for all others. */
2300 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2304 /* Delete the temporary new association. */
2305 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2306 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2308 /* Restore association pointer to provide SCTP command interpreter
2309 * with a valid context in case it needs to manipulate
2311 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2312 SCTP_ASOC((struct sctp_association *)asoc));
2317 return SCTP_DISPOSITION_NOMEM;
2321 * Process an ABORT. (SHUTDOWN-PENDING state)
2323 * See sctp_sf_do_9_1_abort().
2325 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2327 const struct sctp_endpoint *ep,
2328 const struct sctp_association *asoc,
2329 const union sctp_subtype type,
2331 struct sctp_cmd_seq *commands)
2333 struct sctp_chunk *chunk = arg;
2335 if (!sctp_vtag_verify_either(chunk, asoc))
2336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2338 /* Make sure that the ABORT chunk has a valid length.
2339 * Since this is an ABORT chunk, we have to discard it
2340 * because of the following text:
2341 * RFC 2960, Section 3.3.7
2342 * If an endpoint receives an ABORT with a format error or for an
2343 * association that doesn't exist, it MUST silently discard it.
2344 * Because the length is "invalid", we can't really discard just
2345 * as we do not know its true length. So, to be safe, discard the
2348 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2349 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2351 /* ADD-IP: Special case for ABORT chunks
2352 * F4) One special consideration is that ABORT Chunks arriving
2353 * destined to the IP address being deleted MUST be
2354 * ignored (see Section 5.3.1 for further details).
2356 if (SCTP_ADDR_DEL ==
2357 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2360 if (!sctp_err_chunk_valid(chunk))
2361 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2363 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2367 * Process an ABORT. (SHUTDOWN-SENT state)
2369 * See sctp_sf_do_9_1_abort().
2371 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2373 const struct sctp_endpoint *ep,
2374 const struct sctp_association *asoc,
2375 const union sctp_subtype type,
2377 struct sctp_cmd_seq *commands)
2379 struct sctp_chunk *chunk = arg;
2381 if (!sctp_vtag_verify_either(chunk, asoc))
2382 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2384 /* Make sure that the ABORT chunk has a valid length.
2385 * Since this is an ABORT chunk, we have to discard it
2386 * because of the following text:
2387 * RFC 2960, Section 3.3.7
2388 * If an endpoint receives an ABORT with a format error or for an
2389 * association that doesn't exist, it MUST silently discard it.
2390 * Because the length is "invalid", we can't really discard just
2391 * as we do not know its true length. So, to be safe, discard the
2394 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2395 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2397 /* ADD-IP: Special case for ABORT chunks
2398 * F4) One special consideration is that ABORT Chunks arriving
2399 * destined to the IP address being deleted MUST be
2400 * ignored (see Section 5.3.1 for further details).
2402 if (SCTP_ADDR_DEL ==
2403 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2404 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2406 if (!sctp_err_chunk_valid(chunk))
2407 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2409 /* Stop the T2-shutdown timer. */
2410 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2411 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2413 /* Stop the T5-shutdown guard timer. */
2414 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2415 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2417 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2421 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2423 * See sctp_sf_do_9_1_abort().
2425 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2427 const struct sctp_endpoint *ep,
2428 const struct sctp_association *asoc,
2429 const union sctp_subtype type,
2431 struct sctp_cmd_seq *commands)
2433 /* The same T2 timer, so we should be able to use
2434 * common function with the SHUTDOWN-SENT state.
2436 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2440 * Handle an Error received in COOKIE_ECHOED state.
2442 * Only handle the error type of stale COOKIE Error, the other errors will
2446 * (endpoint, asoc, chunk)
2449 * (asoc, reply_msg, msg_up, timers, counters)
2451 * The return value is the disposition of the chunk.
2453 enum sctp_disposition sctp_sf_cookie_echoed_err(
2455 const struct sctp_endpoint *ep,
2456 const struct sctp_association *asoc,
2457 const union sctp_subtype type,
2459 struct sctp_cmd_seq *commands)
2461 struct sctp_chunk *chunk = arg;
2462 struct sctp_errhdr *err;
2464 if (!sctp_vtag_verify(chunk, asoc))
2465 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2467 /* Make sure that the ERROR chunk has a valid length.
2468 * The parameter walking depends on this as well.
2470 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2471 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2474 /* Process the error here */
2475 /* FUTURE FIXME: When PR-SCTP related and other optional
2476 * parms are emitted, this will have to change to handle multiple
2479 sctp_walk_errors(err, chunk->chunk_hdr) {
2480 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2481 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2485 /* It is possible to have malformed error causes, and that
2486 * will cause us to end the walk early. However, since
2487 * we are discarding the packet, there should be no adverse
2490 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2494 * Handle a Stale COOKIE Error
2496 * Section: 5.2.6 Handle Stale COOKIE Error
2497 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2498 * one of the following three alternatives.
2500 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2501 * Preservative parameter requesting an extension to the lifetime of
2502 * the State Cookie. When calculating the time extension, an
2503 * implementation SHOULD use the RTT information measured based on the
2504 * previous COOKIE ECHO / ERROR exchange, and should add no more
2505 * than 1 second beyond the measured RTT, due to long State Cookie
2506 * lifetimes making the endpoint more subject to a replay attack.
2508 * Verification Tag: Not explicit, but safe to ignore.
2511 * (endpoint, asoc, chunk)
2514 * (asoc, reply_msg, msg_up, timers, counters)
2516 * The return value is the disposition of the chunk.
2518 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2520 const struct sctp_endpoint *ep,
2521 const struct sctp_association *asoc,
2522 const union sctp_subtype type,
2524 struct sctp_cmd_seq *commands)
2526 int attempts = asoc->init_err_counter + 1;
2527 struct sctp_chunk *chunk = arg, *reply;
2528 struct sctp_cookie_preserve_param bht;
2529 struct sctp_bind_addr *bp;
2530 struct sctp_errhdr *err;
2533 if (attempts > asoc->max_init_attempts) {
2534 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2535 SCTP_ERROR(ETIMEDOUT));
2536 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2537 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2538 return SCTP_DISPOSITION_DELETE_TCB;
2541 err = (struct sctp_errhdr *)(chunk->skb->data);
2543 /* When calculating the time extension, an implementation
2544 * SHOULD use the RTT information measured based on the
2545 * previous COOKIE ECHO / ERROR exchange, and should add no
2546 * more than 1 second beyond the measured RTT, due to long
2547 * State Cookie lifetimes making the endpoint more subject to
2549 * Measure of Staleness's unit is usec. (1/1000000 sec)
2550 * Suggested Cookie Life-span Increment's unit is msec.
2552 * In general, if you use the suggested cookie life, the value
2553 * found in the field of measure of staleness should be doubled
2554 * to give ample time to retransmit the new cookie and thus
2555 * yield a higher probability of success on the reattempt.
2557 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2558 stale = (stale * 2) / 1000;
2560 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2561 bht.param_hdr.length = htons(sizeof(bht));
2562 bht.lifespan_increment = htonl(stale);
2564 /* Build that new INIT chunk. */
2565 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2566 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2570 sctp_addto_chunk(reply, sizeof(bht), &bht);
2572 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2573 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2575 /* Stop pending T3-rtx and heartbeat timers */
2576 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2577 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2579 /* Delete non-primary peer ip addresses since we are transitioning
2580 * back to the COOKIE-WAIT state
2582 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2584 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2587 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2588 SCTP_TRANSPORT(asoc->peer.primary_path));
2590 /* Cast away the const modifier, as we want to just
2591 * rerun it through as a sideffect.
2593 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2595 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2596 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2597 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2598 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2599 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2600 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2602 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2604 return SCTP_DISPOSITION_CONSUME;
2607 return SCTP_DISPOSITION_NOMEM;
2614 * After checking the Verification Tag, the receiving endpoint shall
2615 * remove the association from its record, and shall report the
2616 * termination to its upper layer.
2618 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2619 * B) Rules for packet carrying ABORT:
2621 * - The endpoint shall always fill in the Verification Tag field of the
2622 * outbound packet with the destination endpoint's tag value if it
2625 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2626 * MUST follow the procedure described in Section 8.4.
2628 * - The receiver MUST accept the packet if the Verification Tag
2629 * matches either its own tag, OR the tag of its peer. Otherwise, the
2630 * receiver MUST silently discard the packet and take no further
2634 * (endpoint, asoc, chunk)
2637 * (asoc, reply_msg, msg_up, timers, counters)
2639 * The return value is the disposition of the chunk.
2641 enum sctp_disposition sctp_sf_do_9_1_abort(
2643 const struct sctp_endpoint *ep,
2644 const struct sctp_association *asoc,
2645 const union sctp_subtype type,
2647 struct sctp_cmd_seq *commands)
2649 struct sctp_chunk *chunk = arg;
2651 if (!sctp_vtag_verify_either(chunk, asoc))
2652 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2654 /* Make sure that the ABORT chunk has a valid length.
2655 * Since this is an ABORT chunk, we have to discard it
2656 * because of the following text:
2657 * RFC 2960, Section 3.3.7
2658 * If an endpoint receives an ABORT with a format error or for an
2659 * association that doesn't exist, it MUST silently discard it.
2660 * Because the length is "invalid", we can't really discard just
2661 * as we do not know its true length. So, to be safe, discard the
2664 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2665 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2667 /* ADD-IP: Special case for ABORT chunks
2668 * F4) One special consideration is that ABORT Chunks arriving
2669 * destined to the IP address being deleted MUST be
2670 * ignored (see Section 5.3.1 for further details).
2672 if (SCTP_ADDR_DEL ==
2673 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2674 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2676 if (!sctp_err_chunk_valid(chunk))
2677 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2679 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2682 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2684 const struct sctp_endpoint *ep,
2685 const struct sctp_association *asoc,
2686 const union sctp_subtype type,
2688 struct sctp_cmd_seq *commands)
2690 __be16 error = SCTP_ERROR_NO_ERROR;
2691 struct sctp_chunk *chunk = arg;
2694 /* See if we have an error cause code in the chunk. */
2695 len = ntohs(chunk->chunk_hdr->length);
2696 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2697 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2699 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2700 /* ASSOC_FAILED will DELETE_TCB. */
2701 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2702 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2703 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2705 return SCTP_DISPOSITION_ABORT;
2709 * Process an ABORT. (COOKIE-WAIT state)
2711 * See sctp_sf_do_9_1_abort() above.
2713 enum sctp_disposition sctp_sf_cookie_wait_abort(
2715 const struct sctp_endpoint *ep,
2716 const struct sctp_association *asoc,
2717 const union sctp_subtype type,
2719 struct sctp_cmd_seq *commands)
2721 __be16 error = SCTP_ERROR_NO_ERROR;
2722 struct sctp_chunk *chunk = arg;
2725 if (!sctp_vtag_verify_either(chunk, asoc))
2726 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2728 /* Make sure that the ABORT chunk has a valid length.
2729 * Since this is an ABORT chunk, we have to discard it
2730 * because of the following text:
2731 * RFC 2960, Section 3.3.7
2732 * If an endpoint receives an ABORT with a format error or for an
2733 * association that doesn't exist, it MUST silently discard it.
2734 * Because the length is "invalid", we can't really discard just
2735 * as we do not know its true length. So, to be safe, discard the
2738 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2739 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2741 /* See if we have an error cause code in the chunk. */
2742 len = ntohs(chunk->chunk_hdr->length);
2743 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2744 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2746 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2751 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2753 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2755 const struct sctp_endpoint *ep,
2756 const struct sctp_association *asoc,
2757 const union sctp_subtype type,
2759 struct sctp_cmd_seq *commands)
2761 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2763 (struct sctp_transport *)arg);
2767 * Process an ABORT. (COOKIE-ECHOED state)
2769 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2771 const struct sctp_endpoint *ep,
2772 const struct sctp_association *asoc,
2773 const union sctp_subtype type,
2775 struct sctp_cmd_seq *commands)
2777 /* There is a single T1 timer, so we should be able to use
2778 * common function with the COOKIE-WAIT state.
2780 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2784 * Stop T1 timer and abort association with "INIT failed".
2786 * This is common code called by several sctp_sf_*_abort() functions above.
2788 static enum sctp_disposition sctp_stop_t1_and_abort(
2790 struct sctp_cmd_seq *commands,
2791 __be16 error, int sk_err,
2792 const struct sctp_association *asoc,
2793 struct sctp_transport *transport)
2795 pr_debug("%s: ABORT received (INIT)\n", __func__);
2797 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2798 SCTP_STATE(SCTP_STATE_CLOSED));
2799 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2800 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2801 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2802 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2803 /* CMD_INIT_FAILED will DELETE_TCB. */
2804 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2807 return SCTP_DISPOSITION_ABORT;
2811 * sctp_sf_do_9_2_shut
2814 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2815 * - enter the SHUTDOWN-RECEIVED state,
2817 * - stop accepting new data from its SCTP user
2819 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2820 * that all its outstanding DATA chunks have been received by the
2823 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2824 * send a SHUTDOWN in response to a ULP request. And should discard
2825 * subsequent SHUTDOWN chunks.
2827 * If there are still outstanding DATA chunks left, the SHUTDOWN
2828 * receiver shall continue to follow normal data transmission
2829 * procedures defined in Section 6 until all outstanding DATA chunks
2830 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2831 * new data from its SCTP user.
2833 * Verification Tag: 8.5 Verification Tag [Normal verification]
2836 * (endpoint, asoc, chunk)
2839 * (asoc, reply_msg, msg_up, timers, counters)
2841 * The return value is the disposition of the chunk.
2843 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2845 const struct sctp_endpoint *ep,
2846 const struct sctp_association *asoc,
2847 const union sctp_subtype type,
2849 struct sctp_cmd_seq *commands)
2851 enum sctp_disposition disposition;
2852 struct sctp_chunk *chunk = arg;
2853 struct sctp_shutdownhdr *sdh;
2854 struct sctp_ulpevent *ev;
2857 if (!sctp_vtag_verify(chunk, asoc))
2858 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2860 /* Make sure that the SHUTDOWN chunk has a valid length. */
2861 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2862 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2865 /* Convert the elaborate header. */
2866 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2867 skb_pull(chunk->skb, sizeof(*sdh));
2868 chunk->subh.shutdown_hdr = sdh;
2869 ctsn = ntohl(sdh->cum_tsn_ack);
2871 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2872 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2873 asoc->ctsn_ack_point);
2875 return SCTP_DISPOSITION_DISCARD;
2878 /* If Cumulative TSN Ack beyond the max tsn currently
2879 * send, terminating the association and respond to the
2880 * sender with an ABORT.
2882 if (!TSN_lt(ctsn, asoc->next_tsn))
2883 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2885 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2886 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2887 * inform the application that it should cease sending data.
2889 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2891 disposition = SCTP_DISPOSITION_NOMEM;
2894 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2896 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2897 * - enter the SHUTDOWN-RECEIVED state,
2898 * - stop accepting new data from its SCTP user
2900 * [This is implicit in the new state.]
2902 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2903 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2904 disposition = SCTP_DISPOSITION_CONSUME;
2906 if (sctp_outq_is_empty(&asoc->outqueue)) {
2907 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2911 if (SCTP_DISPOSITION_NOMEM == disposition)
2914 /* - verify, by checking the Cumulative TSN Ack field of the
2915 * chunk, that all its outstanding DATA chunks have been
2916 * received by the SHUTDOWN sender.
2918 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2919 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2926 * sctp_sf_do_9_2_shut_ctsn
2928 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2929 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2930 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2931 * MUST be processed.
2933 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2935 const struct sctp_endpoint *ep,
2936 const struct sctp_association *asoc,
2937 const union sctp_subtype type,
2939 struct sctp_cmd_seq *commands)
2941 struct sctp_chunk *chunk = arg;
2942 struct sctp_shutdownhdr *sdh;
2945 if (!sctp_vtag_verify(chunk, asoc))
2946 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2948 /* Make sure that the SHUTDOWN chunk has a valid length. */
2949 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2950 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2953 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2954 ctsn = ntohl(sdh->cum_tsn_ack);
2956 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2957 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2958 asoc->ctsn_ack_point);
2960 return SCTP_DISPOSITION_DISCARD;
2963 /* If Cumulative TSN Ack beyond the max tsn currently
2964 * send, terminating the association and respond to the
2965 * sender with an ABORT.
2967 if (!TSN_lt(ctsn, asoc->next_tsn))
2968 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2970 /* verify, by checking the Cumulative TSN Ack field of the
2971 * chunk, that all its outstanding DATA chunks have been
2972 * received by the SHUTDOWN sender.
2974 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2975 SCTP_BE32(sdh->cum_tsn_ack));
2977 return SCTP_DISPOSITION_CONSUME;
2981 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2982 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2983 * transport addresses (either in the IP addresses or in the INIT chunk)
2984 * that belong to this association, it should discard the INIT chunk and
2985 * retransmit the SHUTDOWN ACK chunk.
2987 static enum sctp_disposition
2988 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2989 const struct sctp_association *asoc,
2990 const union sctp_subtype type, void *arg,
2991 struct sctp_cmd_seq *commands)
2993 struct sctp_chunk *chunk = arg;
2994 struct sctp_chunk *reply;
2996 /* Make sure that the chunk has a valid length */
2997 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2998 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3001 /* Since we are not going to really process this INIT, there
3002 * is no point in verifying chunk boundaries. Just generate
3005 reply = sctp_make_shutdown_ack(asoc, chunk);
3009 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
3010 * the T2-SHUTDOWN timer.
3012 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
3014 /* and restart the T2-shutdown timer. */
3015 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3016 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3018 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3020 return SCTP_DISPOSITION_CONSUME;
3022 return SCTP_DISPOSITION_NOMEM;
3025 enum sctp_disposition
3026 sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
3027 const struct sctp_association *asoc,
3028 const union sctp_subtype type, void *arg,
3029 struct sctp_cmd_seq *commands)
3031 struct sctp_chunk *chunk = arg;
3033 if (!chunk->singleton)
3034 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3036 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
3037 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3039 if (chunk->sctp_hdr->vtag != 0)
3040 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3042 return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
3046 * sctp_sf_do_ecn_cwr
3048 * Section: Appendix A: Explicit Congestion Notification
3052 * RFC 2481 details a specific bit for a sender to send in the header of
3053 * its next outbound TCP segment to indicate to its peer that it has
3054 * reduced its congestion window. This is termed the CWR bit. For
3055 * SCTP the same indication is made by including the CWR chunk.
3056 * This chunk contains one data element, i.e. the TSN number that
3057 * was sent in the ECNE chunk. This element represents the lowest
3058 * TSN number in the datagram that was originally marked with the
3061 * Verification Tag: 8.5 Verification Tag [Normal verification]
3063 * (endpoint, asoc, chunk)
3066 * (asoc, reply_msg, msg_up, timers, counters)
3068 * The return value is the disposition of the chunk.
3070 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
3071 const struct sctp_endpoint *ep,
3072 const struct sctp_association *asoc,
3073 const union sctp_subtype type,
3075 struct sctp_cmd_seq *commands)
3077 struct sctp_chunk *chunk = arg;
3078 struct sctp_cwrhdr *cwr;
3081 if (!sctp_vtag_verify(chunk, asoc))
3082 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3084 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3085 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3088 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
3089 skb_pull(chunk->skb, sizeof(*cwr));
3091 lowest_tsn = ntohl(cwr->lowest_tsn);
3093 /* Does this CWR ack the last sent congestion notification? */
3094 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
3095 /* Stop sending ECNE. */
3096 sctp_add_cmd_sf(commands,
3098 SCTP_U32(lowest_tsn));
3100 return SCTP_DISPOSITION_CONSUME;
3106 * Section: Appendix A: Explicit Congestion Notification
3110 * RFC 2481 details a specific bit for a receiver to send back in its
3111 * TCP acknowledgements to notify the sender of the Congestion
3112 * Experienced (CE) bit having arrived from the network. For SCTP this
3113 * same indication is made by including the ECNE chunk. This chunk
3114 * contains one data element, i.e. the lowest TSN associated with the IP
3115 * datagram marked with the CE bit.....
3117 * Verification Tag: 8.5 Verification Tag [Normal verification]
3119 * (endpoint, asoc, chunk)
3122 * (asoc, reply_msg, msg_up, timers, counters)
3124 * The return value is the disposition of the chunk.
3126 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3127 const struct sctp_endpoint *ep,
3128 const struct sctp_association *asoc,
3129 const union sctp_subtype type,
3130 void *arg, struct sctp_cmd_seq *commands)
3132 struct sctp_chunk *chunk = arg;
3133 struct sctp_ecnehdr *ecne;
3135 if (!sctp_vtag_verify(chunk, asoc))
3136 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3138 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3139 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3142 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3143 skb_pull(chunk->skb, sizeof(*ecne));
3145 /* If this is a newer ECNE than the last CWR packet we sent out */
3146 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3147 SCTP_U32(ntohl(ecne->lowest_tsn)));
3149 return SCTP_DISPOSITION_CONSUME;
3153 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3155 * The SCTP endpoint MUST always acknowledge the reception of each valid
3158 * The guidelines on delayed acknowledgement algorithm specified in
3159 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3160 * acknowledgement SHOULD be generated for at least every second packet
3161 * (not every second DATA chunk) received, and SHOULD be generated within
3162 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3163 * situations it may be beneficial for an SCTP transmitter to be more
3164 * conservative than the algorithms detailed in this document allow.
3165 * However, an SCTP transmitter MUST NOT be more aggressive than the
3166 * following algorithms allow.
3168 * A SCTP receiver MUST NOT generate more than one SACK for every
3169 * incoming packet, other than to update the offered window as the
3170 * receiving application consumes new data.
3172 * Verification Tag: 8.5 Verification Tag [Normal verification]
3175 * (endpoint, asoc, chunk)
3178 * (asoc, reply_msg, msg_up, timers, counters)
3180 * The return value is the disposition of the chunk.
3182 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3183 const struct sctp_endpoint *ep,
3184 const struct sctp_association *asoc,
3185 const union sctp_subtype type,
3187 struct sctp_cmd_seq *commands)
3189 union sctp_arg force = SCTP_NOFORCE();
3190 struct sctp_chunk *chunk = arg;
3193 if (!sctp_vtag_verify(chunk, asoc)) {
3194 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3199 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3200 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3203 error = sctp_eat_data(asoc, chunk, commands);
3205 case SCTP_IERROR_NO_ERROR:
3207 case SCTP_IERROR_HIGH_TSN:
3208 case SCTP_IERROR_BAD_STREAM:
3209 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3210 goto discard_noforce;
3211 case SCTP_IERROR_DUP_TSN:
3212 case SCTP_IERROR_IGNORE_TSN:
3213 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3215 case SCTP_IERROR_NO_DATA:
3216 return SCTP_DISPOSITION_ABORT;
3217 case SCTP_IERROR_PROTO_VIOLATION:
3218 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3219 (u8 *)chunk->subh.data_hdr,
3220 sctp_datahdr_len(&asoc->stream));
3225 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3226 force = SCTP_FORCE();
3228 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3229 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3230 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3233 /* If this is the last chunk in a packet, we need to count it
3234 * toward sack generation. Note that we need to SACK every
3235 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3236 * THEM. We elect to NOT generate SACK's if the chunk fails
3237 * the verification tag test.
3239 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3241 * The SCTP endpoint MUST always acknowledge the reception of
3242 * each valid DATA chunk.
3244 * The guidelines on delayed acknowledgement algorithm
3245 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3246 * Specifically, an acknowledgement SHOULD be generated for at
3247 * least every second packet (not every second DATA chunk)
3248 * received, and SHOULD be generated within 200 ms of the
3249 * arrival of any unacknowledged DATA chunk. In some
3250 * situations it may be beneficial for an SCTP transmitter to
3251 * be more conservative than the algorithms detailed in this
3252 * document allow. However, an SCTP transmitter MUST NOT be
3253 * more aggressive than the following algorithms allow.
3255 if (chunk->end_of_packet)
3256 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3258 return SCTP_DISPOSITION_CONSUME;
3261 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3263 * When a packet arrives with duplicate DATA chunk(s) and with
3264 * no new DATA chunk(s), the endpoint MUST immediately send a
3265 * SACK with no delay. If a packet arrives with duplicate
3266 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3267 * MAY immediately send a SACK. Normally receipt of duplicate
3268 * DATA chunks will occur when the original SACK chunk was lost
3269 * and the peer's RTO has expired. The duplicate TSN number(s)
3270 * SHOULD be reported in the SACK as duplicate.
3272 /* In our case, we split the MAY SACK advice up whether or not
3273 * the last chunk is a duplicate.'
3275 if (chunk->end_of_packet)
3276 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3277 return SCTP_DISPOSITION_DISCARD;
3280 if (chunk->end_of_packet)
3281 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3283 return SCTP_DISPOSITION_DISCARD;
3287 * sctp_sf_eat_data_fast_4_4
3290 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3291 * DATA chunks without delay.
3293 * Verification Tag: 8.5 Verification Tag [Normal verification]
3295 * (endpoint, asoc, chunk)
3298 * (asoc, reply_msg, msg_up, timers, counters)
3300 * The return value is the disposition of the chunk.
3302 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3304 const struct sctp_endpoint *ep,
3305 const struct sctp_association *asoc,
3306 const union sctp_subtype type,
3308 struct sctp_cmd_seq *commands)
3310 struct sctp_chunk *chunk = arg;
3313 if (!sctp_vtag_verify(chunk, asoc)) {
3314 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3319 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3323 error = sctp_eat_data(asoc, chunk, commands);
3325 case SCTP_IERROR_NO_ERROR:
3326 case SCTP_IERROR_HIGH_TSN:
3327 case SCTP_IERROR_DUP_TSN:
3328 case SCTP_IERROR_IGNORE_TSN:
3329 case SCTP_IERROR_BAD_STREAM:
3331 case SCTP_IERROR_NO_DATA:
3332 return SCTP_DISPOSITION_ABORT;
3333 case SCTP_IERROR_PROTO_VIOLATION:
3334 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3335 (u8 *)chunk->subh.data_hdr,
3336 sctp_datahdr_len(&asoc->stream));
3341 /* Go a head and force a SACK, since we are shutting down. */
3343 /* Implementor's Guide.
3345 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3346 * respond to each received packet containing one or more DATA chunk(s)
3347 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3349 if (chunk->end_of_packet) {
3350 /* We must delay the chunk creation since the cumulative
3351 * TSN has not been updated yet.
3353 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3354 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3355 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3356 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3359 return SCTP_DISPOSITION_CONSUME;
3363 * Section: 6.2 Processing a Received SACK
3364 * D) Any time a SACK arrives, the endpoint performs the following:
3366 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3367 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3368 * increasing, a SACK whose Cumulative TSN Ack is less than the
3369 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3371 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3372 * of bytes still outstanding after processing the Cumulative TSN Ack
3373 * and the Gap Ack Blocks.
3375 * iii) If the SACK is missing a TSN that was previously
3376 * acknowledged via a Gap Ack Block (e.g., the data receiver
3377 * reneged on the data), then mark the corresponding DATA chunk
3378 * as available for retransmit: Mark it as missing for fast
3379 * retransmit as described in Section 7.2.4 and if no retransmit
3380 * timer is running for the destination address to which the DATA
3381 * chunk was originally transmitted, then T3-rtx is started for
3382 * that destination address.
3384 * Verification Tag: 8.5 Verification Tag [Normal verification]
3387 * (endpoint, asoc, chunk)
3390 * (asoc, reply_msg, msg_up, timers, counters)
3392 * The return value is the disposition of the chunk.
3394 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3395 const struct sctp_endpoint *ep,
3396 const struct sctp_association *asoc,
3397 const union sctp_subtype type,
3399 struct sctp_cmd_seq *commands)
3401 struct sctp_chunk *chunk = arg;
3402 struct sctp_sackhdr *sackh;
3405 if (!sctp_vtag_verify(chunk, asoc))
3406 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3408 /* Make sure that the SACK chunk has a valid length. */
3409 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3410 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3413 /* Pull the SACK chunk from the data buffer */
3414 sackh = sctp_sm_pull_sack(chunk);
3415 /* Was this a bogus SACK? */
3417 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3418 chunk->subh.sack_hdr = sackh;
3419 ctsn = ntohl(sackh->cum_tsn_ack);
3421 /* If Cumulative TSN Ack beyond the max tsn currently
3422 * send, terminating the association and respond to the
3423 * sender with an ABORT.
3425 if (TSN_lte(asoc->next_tsn, ctsn))
3426 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3428 trace_sctp_probe(ep, asoc, chunk);
3430 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3431 * Ack Point, then drop the SACK. Since Cumulative TSN
3432 * Ack is monotonically increasing, a SACK whose
3433 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3434 * Point indicates an out-of-order SACK.
3436 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3437 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3438 asoc->ctsn_ack_point);
3440 return SCTP_DISPOSITION_DISCARD;
3443 /* Return this SACK for further processing. */
3444 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3446 /* Note: We do the rest of the work on the PROCESS_SACK
3449 return SCTP_DISPOSITION_CONSUME;
3453 * Generate an ABORT in response to a packet.
3455 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3457 * 8) The receiver should respond to the sender of the OOTB packet with
3458 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3459 * MUST fill in the Verification Tag field of the outbound packet
3460 * with the value found in the Verification Tag field of the OOTB
3461 * packet and set the T-bit in the Chunk Flags to indicate that the
3462 * Verification Tag is reflected. After sending this ABORT, the
3463 * receiver of the OOTB packet shall discard the OOTB packet and take
3464 * no further action.
3468 * The return value is the disposition of the chunk.
3470 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3472 const struct sctp_endpoint *ep,
3473 const struct sctp_association *asoc,
3474 const union sctp_subtype type,
3476 struct sctp_cmd_seq *commands)
3478 struct sctp_packet *packet = NULL;
3479 struct sctp_chunk *chunk = arg;
3480 struct sctp_chunk *abort;
3482 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3484 return SCTP_DISPOSITION_NOMEM;
3486 /* Make an ABORT. The T bit will be set if the asoc
3489 abort = sctp_make_abort(asoc, chunk, 0);
3491 sctp_ootb_pkt_free(packet);
3492 return SCTP_DISPOSITION_NOMEM;
3495 /* Reflect vtag if T-Bit is set */
3496 if (sctp_test_T_bit(abort))
3497 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3499 /* Set the skb to the belonging sock for accounting. */
3500 abort->skb->sk = ep->base.sk;
3502 sctp_packet_append_chunk(packet, abort);
3504 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(packet));
3506 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3508 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3509 return SCTP_DISPOSITION_CONSUME;
3512 /* Handling of SCTP Packets Containing an INIT Chunk Matching an
3513 * Existing Associations when the UDP encap port is incorrect.
3515 * From Section 4 at draft-tuexen-tsvwg-sctp-udp-encaps-cons-03.
3517 static enum sctp_disposition sctp_sf_new_encap_port(
3519 const struct sctp_endpoint *ep,
3520 const struct sctp_association *asoc,
3521 const union sctp_subtype type,
3523 struct sctp_cmd_seq *commands)
3525 struct sctp_packet *packet = NULL;
3526 struct sctp_chunk *chunk = arg;
3527 struct sctp_chunk *abort;
3529 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3531 return SCTP_DISPOSITION_NOMEM;
3533 abort = sctp_make_new_encap_port(asoc, chunk);
3535 sctp_ootb_pkt_free(packet);
3536 return SCTP_DISPOSITION_NOMEM;
3539 abort->skb->sk = ep->base.sk;
3541 sctp_packet_append_chunk(packet, abort);
3543 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3544 SCTP_PACKET(packet));
3546 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3548 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3549 return SCTP_DISPOSITION_CONSUME;
3553 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3554 * event as ULP notification for each cause included in the chunk.
3556 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3558 * The return value is the disposition of the chunk.
3560 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3561 const struct sctp_endpoint *ep,
3562 const struct sctp_association *asoc,
3563 const union sctp_subtype type,
3565 struct sctp_cmd_seq *commands)
3567 struct sctp_chunk *chunk = arg;
3568 struct sctp_errhdr *err;
3570 if (!sctp_vtag_verify(chunk, asoc))
3571 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3573 /* Make sure that the ERROR chunk has a valid length. */
3574 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3575 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3577 sctp_walk_errors(err, chunk->chunk_hdr);
3578 if ((void *)err != (void *)chunk->chunk_end)
3579 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3580 (void *)err, commands);
3582 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3585 return SCTP_DISPOSITION_CONSUME;
3589 * Process an inbound SHUTDOWN ACK.
3592 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3593 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3594 * peer, and remove all record of the association.
3596 * The return value is the disposition.
3598 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3599 const struct sctp_endpoint *ep,
3600 const struct sctp_association *asoc,
3601 const union sctp_subtype type,
3603 struct sctp_cmd_seq *commands)
3605 struct sctp_chunk *chunk = arg;
3606 struct sctp_chunk *reply;
3607 struct sctp_ulpevent *ev;
3609 if (!sctp_vtag_verify(chunk, asoc))
3610 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3612 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3613 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3614 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3616 /* 10.2 H) SHUTDOWN COMPLETE notification
3618 * When SCTP completes the shutdown procedures (section 9.2) this
3619 * notification is passed to the upper layer.
3621 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3622 0, 0, 0, NULL, GFP_ATOMIC);
3626 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3627 reply = sctp_make_shutdown_complete(asoc, chunk);
3631 /* Do all the commands now (after allocation), so that we
3632 * have consistent state if memory allocation fails
3634 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3636 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3637 * stop the T2-shutdown timer,
3639 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3640 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3642 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3643 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3645 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3646 SCTP_STATE(SCTP_STATE_CLOSED));
3647 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3648 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3649 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3651 /* ...and remove all record of the association. */
3652 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3653 return SCTP_DISPOSITION_DELETE_TCB;
3656 sctp_ulpevent_free(ev);
3658 return SCTP_DISPOSITION_NOMEM;
3662 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3664 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3665 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3666 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3667 * packet must fill in the Verification Tag field of the outbound
3668 * packet with the Verification Tag received in the SHUTDOWN ACK and
3669 * set the T-bit in the Chunk Flags to indicate that the Verification
3672 * 8) The receiver should respond to the sender of the OOTB packet with
3673 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3674 * MUST fill in the Verification Tag field of the outbound packet
3675 * with the value found in the Verification Tag field of the OOTB
3676 * packet and set the T-bit in the Chunk Flags to indicate that the
3677 * Verification Tag is reflected. After sending this ABORT, the
3678 * receiver of the OOTB packet shall discard the OOTB packet and take
3679 * no further action.
3681 enum sctp_disposition sctp_sf_ootb(struct net *net,
3682 const struct sctp_endpoint *ep,
3683 const struct sctp_association *asoc,
3684 const union sctp_subtype type,
3685 void *arg, struct sctp_cmd_seq *commands)
3687 struct sctp_chunk *chunk = arg;
3688 struct sk_buff *skb = chunk->skb;
3689 struct sctp_chunkhdr *ch;
3690 struct sctp_errhdr *err;
3691 int ootb_cookie_ack = 0;
3692 int ootb_shut_ack = 0;
3695 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3697 if (asoc && !sctp_vtag_verify(chunk, asoc))
3700 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3702 /* Report violation if the chunk is less then minimal */
3703 if (ntohs(ch->length) < sizeof(*ch))
3704 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3707 /* Report violation if chunk len overflows */
3708 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3709 if (ch_end > skb_tail_pointer(skb))
3710 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3713 /* Now that we know we at least have a chunk header,
3714 * do things that are type appropriate.
3716 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3719 /* RFC 2960, Section 3.3.7
3720 * Moreover, under any circumstances, an endpoint that
3721 * receives an ABORT MUST NOT respond to that ABORT by
3722 * sending an ABORT of its own.
3724 if (SCTP_CID_ABORT == ch->type)
3725 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3727 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3728 * or a COOKIE ACK the SCTP Packet should be silently
3732 if (SCTP_CID_COOKIE_ACK == ch->type)
3733 ootb_cookie_ack = 1;
3735 if (SCTP_CID_ERROR == ch->type) {
3736 sctp_walk_errors(err, ch) {
3737 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3738 ootb_cookie_ack = 1;
3744 ch = (struct sctp_chunkhdr *)ch_end;
3745 } while (ch_end < skb_tail_pointer(skb));
3748 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3749 else if (ootb_cookie_ack)
3750 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3752 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3756 * Handle an "Out of the blue" SHUTDOWN ACK.
3758 * Section: 8.4 5, sctpimpguide 2.41.
3760 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3761 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3762 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3763 * packet must fill in the Verification Tag field of the outbound
3764 * packet with the Verification Tag received in the SHUTDOWN ACK and
3765 * set the T-bit in the Chunk Flags to indicate that the Verification
3769 * (endpoint, asoc, type, arg, commands)
3772 * (enum sctp_disposition)
3774 * The return value is the disposition of the chunk.
3776 static enum sctp_disposition sctp_sf_shut_8_4_5(
3778 const struct sctp_endpoint *ep,
3779 const struct sctp_association *asoc,
3780 const union sctp_subtype type,
3782 struct sctp_cmd_seq *commands)
3784 struct sctp_packet *packet = NULL;
3785 struct sctp_chunk *chunk = arg;
3786 struct sctp_chunk *shut;
3788 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3790 return SCTP_DISPOSITION_NOMEM;
3792 /* Make an SHUTDOWN_COMPLETE.
3793 * The T bit will be set if the asoc is NULL.
3795 shut = sctp_make_shutdown_complete(asoc, chunk);
3797 sctp_ootb_pkt_free(packet);
3798 return SCTP_DISPOSITION_NOMEM;
3801 /* Reflect vtag if T-Bit is set */
3802 if (sctp_test_T_bit(shut))
3803 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3805 /* Set the skb to the belonging sock for accounting. */
3806 shut->skb->sk = ep->base.sk;
3808 sctp_packet_append_chunk(packet, shut);
3810 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3811 SCTP_PACKET(packet));
3813 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3815 /* We need to discard the rest of the packet to prevent
3816 * potential boomming attacks from additional bundled chunks.
3817 * This is documented in SCTP Threats ID.
3819 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3823 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3825 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3826 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3827 * procedures in section 8.4 SHOULD be followed, in other words it
3828 * should be treated as an Out Of The Blue packet.
3829 * [This means that we do NOT check the Verification Tag on these
3833 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3834 const struct sctp_endpoint *ep,
3835 const struct sctp_association *asoc,
3836 const union sctp_subtype type,
3838 struct sctp_cmd_seq *commands)
3840 struct sctp_chunk *chunk = arg;
3842 if (!sctp_vtag_verify(chunk, asoc))
3845 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3846 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3847 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3850 /* Although we do have an association in this case, it corresponds
3851 * to a restarted association. So the packet is treated as an OOTB
3852 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3853 * called with a NULL association.
3855 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3857 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3860 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3861 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3862 const struct sctp_endpoint *ep,
3863 const struct sctp_association *asoc,
3864 const union sctp_subtype type,
3866 struct sctp_cmd_seq *commands)
3868 struct sctp_paramhdr *err_param = NULL;
3869 struct sctp_chunk *asconf_ack = NULL;
3870 struct sctp_chunk *chunk = arg;
3871 struct sctp_addiphdr *hdr;
3874 if (!sctp_vtag_verify(chunk, asoc)) {
3875 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3877 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3880 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3881 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3882 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3885 /* ADD-IP: Section 4.1.1
3886 * This chunk MUST be sent in an authenticated way by using
3887 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3888 * is received unauthenticated it MUST be silently discarded as
3889 * described in [I-D.ietf-tsvwg-sctp-auth].
3891 if (!asoc->peer.asconf_capable ||
3892 (!net->sctp.addip_noauth && !chunk->auth))
3893 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3895 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3896 serial = ntohl(hdr->serial);
3898 /* Verify the ASCONF chunk before processing it. */
3899 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3900 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3901 (void *)err_param, commands);
3903 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3904 * the endpoint stored in a new association variable
3905 * 'Peer-Serial-Number'.
3907 if (serial == asoc->peer.addip_serial + 1) {
3908 /* If this is the first instance of ASCONF in the packet,
3909 * we can clean our old ASCONF-ACKs.
3911 if (!chunk->has_asconf)
3912 sctp_assoc_clean_asconf_ack_cache(asoc);
3914 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3915 * expected, process the ASCONF as described below and after
3916 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3917 * the response packet and cache a copy of it (in the event it
3918 * later needs to be retransmitted).
3920 * Essentially, do V1-V5.
3922 asconf_ack = sctp_process_asconf((struct sctp_association *)
3925 return SCTP_DISPOSITION_NOMEM;
3926 } else if (serial < asoc->peer.addip_serial + 1) {
3928 * If the value found in the Sequence Number is less than the
3929 * ('Peer- Sequence-Number' + 1), simply skip to the next
3930 * ASCONF, and include in the outbound response packet
3931 * any previously cached ASCONF-ACK response that was
3932 * sent and saved that matches the Sequence Number of the
3933 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3934 * Chunk exists. This will occur when an older ASCONF
3935 * arrives out of order. In such a case, the receiver
3936 * should skip the ASCONF Chunk and not include ASCONF-ACK
3937 * Chunk for that chunk.
3939 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3941 return SCTP_DISPOSITION_DISCARD;
3943 /* Reset the transport so that we select the correct one
3944 * this time around. This is to make sure that we don't
3945 * accidentally use a stale transport that's been removed.
3947 asconf_ack->transport = NULL;
3949 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3950 * it must be either a stale packet or from an attacker.
3952 return SCTP_DISPOSITION_DISCARD;
3955 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3956 * containing the ASCONF-ACK Chunks MUST be the source address of
3957 * the SCTP packet that held the ASCONF Chunks.
3959 * To do this properly, we'll set the destination address of the chunk
3960 * and at the transmit time, will try look up the transport to use.
3961 * Since ASCONFs may be bundled, the correct transport may not be
3962 * created until we process the entire packet, thus this workaround.
3964 asconf_ack->dest = chunk->source;
3965 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3966 if (asoc->new_transport) {
3967 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3968 ((struct sctp_association *)asoc)->new_transport = NULL;
3971 return SCTP_DISPOSITION_CONSUME;
3974 static enum sctp_disposition sctp_send_next_asconf(
3976 const struct sctp_endpoint *ep,
3977 struct sctp_association *asoc,
3978 const union sctp_subtype type,
3979 struct sctp_cmd_seq *commands)
3981 struct sctp_chunk *asconf;
3982 struct list_head *entry;
3984 if (list_empty(&asoc->addip_chunk_list))
3985 return SCTP_DISPOSITION_CONSUME;
3987 entry = asoc->addip_chunk_list.next;
3988 asconf = list_entry(entry, struct sctp_chunk, list);
3990 list_del_init(entry);
3991 sctp_chunk_hold(asconf);
3992 asoc->addip_last_asconf = asconf;
3994 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3998 * ADDIP Section 4.3 General rules for address manipulation
3999 * When building TLV parameters for the ASCONF Chunk that will add or
4000 * delete IP addresses the D0 to D13 rules should be applied:
4002 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
4003 const struct sctp_endpoint *ep,
4004 const struct sctp_association *asoc,
4005 const union sctp_subtype type,
4007 struct sctp_cmd_seq *commands)
4009 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
4010 struct sctp_paramhdr *err_param = NULL;
4011 struct sctp_chunk *asconf_ack = arg;
4012 struct sctp_addiphdr *addip_hdr;
4013 __u32 sent_serial, rcvd_serial;
4014 struct sctp_chunk *abort;
4016 if (!sctp_vtag_verify(asconf_ack, asoc)) {
4017 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4019 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4022 /* Make sure that the ADDIP chunk has a valid length. */
4023 if (!sctp_chunk_length_valid(asconf_ack,
4024 sizeof(struct sctp_addip_chunk)))
4025 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4028 /* ADD-IP, Section 4.1.2:
4029 * This chunk MUST be sent in an authenticated way by using
4030 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
4031 * is received unauthenticated it MUST be silently discarded as
4032 * described in [I-D.ietf-tsvwg-sctp-auth].
4034 if (!asoc->peer.asconf_capable ||
4035 (!net->sctp.addip_noauth && !asconf_ack->auth))
4036 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4038 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
4039 rcvd_serial = ntohl(addip_hdr->serial);
4041 /* Verify the ASCONF-ACK chunk before processing it. */
4042 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
4043 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4044 (void *)err_param, commands);
4047 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
4048 sent_serial = ntohl(addip_hdr->serial);
4050 sent_serial = asoc->addip_serial - 1;
4053 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
4054 * equal to the next serial number to be used but no ASCONF chunk is
4055 * outstanding the endpoint MUST ABORT the association. Note that a
4056 * sequence number is greater than if it is no more than 2^^31-1
4057 * larger than the current sequence number (using serial arithmetic).
4059 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
4060 !(asoc->addip_last_asconf)) {
4061 abort = sctp_make_abort(asoc, asconf_ack,
4062 sizeof(struct sctp_errhdr));
4064 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
4065 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4068 /* We are going to ABORT, so we might as well stop
4069 * processing the rest of the chunks in the packet.
4071 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4072 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4073 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4074 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4075 SCTP_ERROR(ECONNABORTED));
4076 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4077 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4078 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4079 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4080 return SCTP_DISPOSITION_ABORT;
4083 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
4084 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4085 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4087 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
4089 return sctp_send_next_asconf(net, ep,
4090 (struct sctp_association *)asoc,
4093 abort = sctp_make_abort(asoc, asconf_ack,
4094 sizeof(struct sctp_errhdr));
4096 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
4097 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4100 /* We are going to ABORT, so we might as well stop
4101 * processing the rest of the chunks in the packet.
4103 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4104 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4105 SCTP_ERROR(ECONNABORTED));
4106 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4107 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4108 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4109 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4110 return SCTP_DISPOSITION_ABORT;
4113 return SCTP_DISPOSITION_DISCARD;
4116 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
4117 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
4118 const struct sctp_endpoint *ep,
4119 const struct sctp_association *asoc,
4120 const union sctp_subtype type,
4122 struct sctp_cmd_seq *commands)
4124 struct sctp_paramhdr *err_param = NULL;
4125 struct sctp_chunk *chunk = arg;
4126 struct sctp_reconf_chunk *hdr;
4127 union sctp_params param;
4129 if (!sctp_vtag_verify(chunk, asoc)) {
4130 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4135 /* Make sure that the RECONF chunk has a valid length. */
4136 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
4137 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4140 if (!sctp_verify_reconf(asoc, chunk, &err_param))
4141 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4142 (void *)err_param, commands);
4144 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
4145 sctp_walk_params(param, hdr, params) {
4146 struct sctp_chunk *reply = NULL;
4147 struct sctp_ulpevent *ev = NULL;
4149 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4150 reply = sctp_process_strreset_outreq(
4151 (struct sctp_association *)asoc, param, &ev);
4152 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4153 reply = sctp_process_strreset_inreq(
4154 (struct sctp_association *)asoc, param, &ev);
4155 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4156 reply = sctp_process_strreset_tsnreq(
4157 (struct sctp_association *)asoc, param, &ev);
4158 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4159 reply = sctp_process_strreset_addstrm_out(
4160 (struct sctp_association *)asoc, param, &ev);
4161 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4162 reply = sctp_process_strreset_addstrm_in(
4163 (struct sctp_association *)asoc, param, &ev);
4164 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4165 reply = sctp_process_strreset_resp(
4166 (struct sctp_association *)asoc, param, &ev);
4169 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4173 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4177 return SCTP_DISPOSITION_CONSUME;
4181 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4183 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4184 * its cumulative TSN point to the value carried in the FORWARD TSN
4185 * chunk, and then MUST further advance its cumulative TSN point locally
4187 * After the above processing, the data receiver MUST stop reporting any
4188 * missing TSNs earlier than or equal to the new cumulative TSN point.
4190 * Verification Tag: 8.5 Verification Tag [Normal verification]
4192 * The return value is the disposition of the chunk.
4194 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4195 const struct sctp_endpoint *ep,
4196 const struct sctp_association *asoc,
4197 const union sctp_subtype type,
4199 struct sctp_cmd_seq *commands)
4201 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4202 struct sctp_chunk *chunk = arg;
4206 if (!sctp_vtag_verify(chunk, asoc)) {
4207 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4209 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4212 if (!asoc->peer.prsctp_capable)
4213 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4215 /* Make sure that the FORWARD_TSN chunk has valid length. */
4216 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4217 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4220 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4221 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4222 len = ntohs(chunk->chunk_hdr->length);
4223 len -= sizeof(struct sctp_chunkhdr);
4224 skb_pull(chunk->skb, len);
4226 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4227 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4229 /* The TSN is too high--silently discard the chunk and count on it
4230 * getting retransmitted later.
4232 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4233 goto discard_noforce;
4235 if (!asoc->stream.si->validate_ftsn(chunk))
4236 goto discard_noforce;
4238 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4239 if (len > sctp_ftsnhdr_len(&asoc->stream))
4240 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4243 /* Count this as receiving DATA. */
4244 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4245 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4246 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4249 /* FIXME: For now send a SACK, but DATA processing may
4252 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4254 return SCTP_DISPOSITION_CONSUME;
4257 return SCTP_DISPOSITION_DISCARD;
4260 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4262 const struct sctp_endpoint *ep,
4263 const struct sctp_association *asoc,
4264 const union sctp_subtype type,
4266 struct sctp_cmd_seq *commands)
4268 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4269 struct sctp_chunk *chunk = arg;
4273 if (!sctp_vtag_verify(chunk, asoc)) {
4274 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4276 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4279 if (!asoc->peer.prsctp_capable)
4280 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4282 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4283 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4284 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4287 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4288 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4289 len = ntohs(chunk->chunk_hdr->length);
4290 len -= sizeof(struct sctp_chunkhdr);
4291 skb_pull(chunk->skb, len);
4293 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4294 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4296 /* The TSN is too high--silently discard the chunk and count on it
4297 * getting retransmitted later.
4299 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4302 if (!asoc->stream.si->validate_ftsn(chunk))
4305 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4306 if (len > sctp_ftsnhdr_len(&asoc->stream))
4307 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4310 /* Go a head and force a SACK, since we are shutting down. */
4312 /* Implementor's Guide.
4314 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4315 * respond to each received packet containing one or more DATA chunk(s)
4316 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4318 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4319 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4320 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4321 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4323 return SCTP_DISPOSITION_CONSUME;
4327 * SCTP-AUTH Section 6.3 Receiving authenticated chunks
4329 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4330 * Identifier field. If this algorithm was not specified by the
4331 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4332 * during association setup, the AUTH chunk and all chunks after it MUST
4333 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4334 * defined in Section 4.1.
4336 * If an endpoint with no shared key receives a Shared Key Identifier
4337 * other than 0, it MUST silently discard all authenticated chunks. If
4338 * the endpoint has at least one endpoint pair shared key for the peer,
4339 * it MUST use the key specified by the Shared Key Identifier if a
4340 * key has been configured for that Shared Key Identifier. If no
4341 * endpoint pair shared key has been configured for that Shared Key
4342 * Identifier, all authenticated chunks MUST be silently discarded.
4344 * Verification Tag: 8.5 Verification Tag [Normal verification]
4346 * The return value is the disposition of the chunk.
4348 static enum sctp_ierror sctp_sf_authenticate(
4349 const struct sctp_association *asoc,
4350 struct sctp_chunk *chunk)
4352 struct sctp_shared_key *sh_key = NULL;
4353 struct sctp_authhdr *auth_hdr;
4354 __u8 *save_digest, *digest;
4355 struct sctp_hmac *hmac;
4356 unsigned int sig_len;
4359 /* Pull in the auth header, so we can do some more verification */
4360 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4361 chunk->subh.auth_hdr = auth_hdr;
4362 skb_pull(chunk->skb, sizeof(*auth_hdr));
4364 /* Make sure that we support the HMAC algorithm from the auth
4367 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4368 return SCTP_IERROR_AUTH_BAD_HMAC;
4370 /* Make sure that the provided shared key identifier has been
4373 key_id = ntohs(auth_hdr->shkey_id);
4374 if (key_id != asoc->active_key_id) {
4375 sh_key = sctp_auth_get_shkey(asoc, key_id);
4377 return SCTP_IERROR_AUTH_BAD_KEYID;
4380 /* Make sure that the length of the signature matches what
4383 sig_len = ntohs(chunk->chunk_hdr->length) -
4384 sizeof(struct sctp_auth_chunk);
4385 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4386 if (sig_len != hmac->hmac_len)
4387 return SCTP_IERROR_PROTO_VIOLATION;
4389 /* Now that we've done validation checks, we can compute and
4390 * verify the hmac. The steps involved are:
4391 * 1. Save the digest from the chunk.
4392 * 2. Zero out the digest in the chunk.
4393 * 3. Compute the new digest
4394 * 4. Compare saved and new digests.
4396 digest = auth_hdr->hmac;
4397 skb_pull(chunk->skb, sig_len);
4399 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4403 memset(digest, 0, sig_len);
4405 sctp_auth_calculate_hmac(asoc, chunk->skb,
4406 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4407 sh_key, GFP_ATOMIC);
4409 /* Discard the packet if the digests do not match */
4410 if (memcmp(save_digest, digest, sig_len)) {
4412 return SCTP_IERROR_BAD_SIG;
4418 return SCTP_IERROR_NO_ERROR;
4420 return SCTP_IERROR_NOMEM;
4423 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4424 const struct sctp_endpoint *ep,
4425 const struct sctp_association *asoc,
4426 const union sctp_subtype type,
4427 void *arg, struct sctp_cmd_seq *commands)
4429 struct sctp_chunk *chunk = arg;
4430 struct sctp_authhdr *auth_hdr;
4431 struct sctp_chunk *err_chunk;
4432 enum sctp_ierror error;
4434 /* Make sure that the peer has AUTH capable */
4435 if (!asoc->peer.auth_capable)
4436 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4438 if (!sctp_vtag_verify(chunk, asoc)) {
4439 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4441 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4444 /* Make sure that the AUTH chunk has valid length. */
4445 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4446 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4449 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4450 error = sctp_sf_authenticate(asoc, chunk);
4452 case SCTP_IERROR_AUTH_BAD_HMAC:
4453 /* Generate the ERROR chunk and discard the rest
4456 err_chunk = sctp_make_op_error(asoc, chunk,
4457 SCTP_ERROR_UNSUP_HMAC,
4461 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4462 SCTP_CHUNK(err_chunk));
4465 case SCTP_IERROR_AUTH_BAD_KEYID:
4466 case SCTP_IERROR_BAD_SIG:
4467 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4469 case SCTP_IERROR_PROTO_VIOLATION:
4470 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4473 case SCTP_IERROR_NOMEM:
4474 return SCTP_DISPOSITION_NOMEM;
4476 default: /* Prevent gcc warnings */
4480 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4481 struct sctp_ulpevent *ev;
4483 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4484 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4487 return SCTP_DISPOSITION_NOMEM;
4489 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4493 return SCTP_DISPOSITION_CONSUME;
4497 * Process an unknown chunk.
4499 * Section: 3.2. Also, 2.1 in the implementor's guide.
4501 * Chunk Types are encoded such that the highest-order two bits specify
4502 * the action that must be taken if the processing endpoint does not
4503 * recognize the Chunk Type.
4505 * 00 - Stop processing this SCTP packet and discard it, do not process
4506 * any further chunks within it.
4508 * 01 - Stop processing this SCTP packet and discard it, do not process
4509 * any further chunks within it, and report the unrecognized
4510 * chunk in an 'Unrecognized Chunk Type'.
4512 * 10 - Skip this chunk and continue processing.
4514 * 11 - Skip this chunk and continue processing, but report in an ERROR
4515 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4517 * The return value is the disposition of the chunk.
4519 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4520 const struct sctp_endpoint *ep,
4521 const struct sctp_association *asoc,
4522 const union sctp_subtype type,
4524 struct sctp_cmd_seq *commands)
4526 struct sctp_chunk *unk_chunk = arg;
4527 struct sctp_chunk *err_chunk;
4528 struct sctp_chunkhdr *hdr;
4530 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4532 if (!sctp_vtag_verify(unk_chunk, asoc))
4533 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4535 /* Make sure that the chunk has a valid length.
4536 * Since we don't know the chunk type, we use a general
4537 * chunkhdr structure to make a comparison.
4539 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4540 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4543 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4544 case SCTP_CID_ACTION_DISCARD:
4545 /* Discard the packet. */
4546 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4547 case SCTP_CID_ACTION_DISCARD_ERR:
4548 /* Generate an ERROR chunk as response. */
4549 hdr = unk_chunk->chunk_hdr;
4550 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4551 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4552 SCTP_PAD4(ntohs(hdr->length)),
4555 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4556 SCTP_CHUNK(err_chunk));
4559 /* Discard the packet. */
4560 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4561 return SCTP_DISPOSITION_CONSUME;
4562 case SCTP_CID_ACTION_SKIP:
4563 /* Skip the chunk. */
4564 return SCTP_DISPOSITION_DISCARD;
4565 case SCTP_CID_ACTION_SKIP_ERR:
4566 /* Generate an ERROR chunk as response. */
4567 hdr = unk_chunk->chunk_hdr;
4568 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4569 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4570 SCTP_PAD4(ntohs(hdr->length)),
4573 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4574 SCTP_CHUNK(err_chunk));
4576 /* Skip the chunk. */
4577 return SCTP_DISPOSITION_CONSUME;
4582 return SCTP_DISPOSITION_DISCARD;
4586 * Discard the chunk.
4588 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4589 * [Too numerous to mention...]
4590 * Verification Tag: No verification needed.
4592 * (endpoint, asoc, chunk)
4595 * (asoc, reply_msg, msg_up, timers, counters)
4597 * The return value is the disposition of the chunk.
4599 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4600 const struct sctp_endpoint *ep,
4601 const struct sctp_association *asoc,
4602 const union sctp_subtype type,
4604 struct sctp_cmd_seq *commands)
4606 struct sctp_chunk *chunk = arg;
4608 if (asoc && !sctp_vtag_verify(chunk, asoc))
4609 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4611 /* Make sure that the chunk has a valid length.
4612 * Since we don't know the chunk type, we use a general
4613 * chunkhdr structure to make a comparison.
4615 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4616 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4619 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4621 return SCTP_DISPOSITION_DISCARD;
4625 * Discard the whole packet.
4629 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4630 * silently discard the OOTB packet and take no further action.
4632 * Verification Tag: No verification necessary
4635 * (endpoint, asoc, chunk)
4638 * (asoc, reply_msg, msg_up, timers, counters)
4640 * The return value is the disposition of the chunk.
4642 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4643 const struct sctp_endpoint *ep,
4644 const struct sctp_association *asoc,
4645 const union sctp_subtype type,
4646 void *arg, struct sctp_cmd_seq *commands)
4648 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4649 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4651 return SCTP_DISPOSITION_CONSUME;
4656 * The other end is violating protocol.
4658 * Section: Not specified
4659 * Verification Tag: Not specified
4661 * (endpoint, asoc, chunk)
4664 * (asoc, reply_msg, msg_up, timers, counters)
4666 * We simply tag the chunk as a violation. The state machine will log
4667 * the violation and continue.
4669 enum sctp_disposition sctp_sf_violation(struct net *net,
4670 const struct sctp_endpoint *ep,
4671 const struct sctp_association *asoc,
4672 const union sctp_subtype type,
4674 struct sctp_cmd_seq *commands)
4676 struct sctp_chunk *chunk = arg;
4678 if (!sctp_vtag_verify(chunk, asoc))
4679 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4681 /* Make sure that the chunk has a valid length. */
4682 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4683 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4686 return SCTP_DISPOSITION_VIOLATION;
4690 * Common function to handle a protocol violation.
4692 static enum sctp_disposition sctp_sf_abort_violation(
4694 const struct sctp_endpoint *ep,
4695 const struct sctp_association *asoc,
4697 struct sctp_cmd_seq *commands,
4698 const __u8 *payload,
4699 const size_t paylen)
4701 struct sctp_packet *packet = NULL;
4702 struct sctp_chunk *chunk = arg;
4703 struct sctp_chunk *abort = NULL;
4705 /* SCTP-AUTH, Section 6.3:
4706 * It should be noted that if the receiver wants to tear
4707 * down an association in an authenticated way only, the
4708 * handling of malformed packets should not result in
4709 * tearing down the association.
4711 * This means that if we only want to abort associations
4712 * in an authenticated way (i.e AUTH+ABORT), then we
4713 * can't destroy this association just because the packet
4716 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4719 /* Make the abort chunk. */
4720 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4725 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4726 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4727 !asoc->peer.i.init_tag) {
4728 struct sctp_initack_chunk *initack;
4730 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4731 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4732 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4734 unsigned int inittag;
4736 inittag = ntohl(initack->init_hdr.init_tag);
4737 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4742 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4743 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4745 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4746 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4747 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4748 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4749 SCTP_ERROR(ECONNREFUSED));
4750 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4751 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4753 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4754 SCTP_ERROR(ECONNABORTED));
4755 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4756 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4757 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4760 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4765 if (sctp_test_T_bit(abort))
4766 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4768 abort->skb->sk = ep->base.sk;
4770 sctp_packet_append_chunk(packet, abort);
4772 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4773 SCTP_PACKET(packet));
4775 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4778 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4781 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4782 return SCTP_DISPOSITION_ABORT;
4785 sctp_chunk_free(abort);
4787 return SCTP_DISPOSITION_NOMEM;
4791 * Handle a protocol violation when the chunk length is invalid.
4792 * "Invalid" length is identified as smaller than the minimal length a
4793 * given chunk can be. For example, a SACK chunk has invalid length
4794 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4796 * We inform the other end by sending an ABORT with a Protocol Violation
4799 * Section: Not specified
4800 * Verification Tag: Nothing to do
4802 * (endpoint, asoc, chunk)
4805 * (reply_msg, msg_up, counters)
4807 * Generate an ABORT chunk and terminate the association.
4809 static enum sctp_disposition sctp_sf_violation_chunklen(
4811 const struct sctp_endpoint *ep,
4812 const struct sctp_association *asoc,
4813 const union sctp_subtype type,
4815 struct sctp_cmd_seq *commands)
4817 static const char err_str[] = "The following chunk had invalid length:";
4819 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4824 * Handle a protocol violation when the parameter length is invalid.
4825 * If the length is smaller than the minimum length of a given parameter,
4826 * or accumulated length in multi parameters exceeds the end of the chunk,
4827 * the length is considered as invalid.
4829 static enum sctp_disposition sctp_sf_violation_paramlen(
4831 const struct sctp_endpoint *ep,
4832 const struct sctp_association *asoc,
4833 const union sctp_subtype type,
4834 void *arg, void *ext,
4835 struct sctp_cmd_seq *commands)
4837 struct sctp_paramhdr *param = ext;
4838 struct sctp_chunk *abort = NULL;
4839 struct sctp_chunk *chunk = arg;
4841 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4844 /* Make the abort chunk. */
4845 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4849 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4850 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4852 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4853 SCTP_ERROR(ECONNABORTED));
4854 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4855 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4856 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4857 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4860 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4861 return SCTP_DISPOSITION_ABORT;
4863 return SCTP_DISPOSITION_NOMEM;
4866 /* Handle a protocol violation when the peer trying to advance the
4867 * cumulative tsn ack to a point beyond the max tsn currently sent.
4869 * We inform the other end by sending an ABORT with a Protocol Violation
4872 static enum sctp_disposition sctp_sf_violation_ctsn(
4874 const struct sctp_endpoint *ep,
4875 const struct sctp_association *asoc,
4876 const union sctp_subtype type,
4878 struct sctp_cmd_seq *commands)
4880 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4882 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4886 /* Handle protocol violation of an invalid chunk bundling. For example,
4887 * when we have an association and we receive bundled INIT-ACK, or
4888 * SHUTDOWN-COMPLETE, our peer is clearly violating the "MUST NOT bundle"
4889 * statement from the specs. Additionally, there might be an attacker
4890 * on the path and we may not want to continue this communication.
4892 static enum sctp_disposition sctp_sf_violation_chunk(
4894 const struct sctp_endpoint *ep,
4895 const struct sctp_association *asoc,
4896 const union sctp_subtype type,
4898 struct sctp_cmd_seq *commands)
4900 static const char err_str[] = "The following chunk violates protocol:";
4902 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4905 /***************************************************************************
4906 * These are the state functions for handling primitive (Section 10) events.
4907 ***************************************************************************/
4909 * sctp_sf_do_prm_asoc
4911 * Section: 10.1 ULP-to-SCTP
4914 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4915 * outbound stream count)
4916 * -> association id [,destination transport addr list] [,outbound stream
4919 * This primitive allows the upper layer to initiate an association to a
4920 * specific peer endpoint.
4922 * The peer endpoint shall be specified by one of the transport addresses
4923 * which defines the endpoint (see Section 1.4). If the local SCTP
4924 * instance has not been initialized, the ASSOCIATE is considered an
4926 * [This is not relevant for the kernel implementation since we do all
4927 * initialization at boot time. It we hadn't initialized we wouldn't
4928 * get anywhere near this code.]
4930 * An association id, which is a local handle to the SCTP association,
4931 * will be returned on successful establishment of the association. If
4932 * SCTP is not able to open an SCTP association with the peer endpoint,
4933 * an error is returned.
4934 * [In the kernel implementation, the struct sctp_association needs to
4935 * be created BEFORE causing this primitive to run.]
4937 * Other association parameters may be returned, including the
4938 * complete destination transport addresses of the peer as well as the
4939 * outbound stream count of the local endpoint. One of the transport
4940 * address from the returned destination addresses will be selected by
4941 * the local endpoint as default primary path for sending SCTP packets
4942 * to this peer. The returned "destination transport addr list" can
4943 * be used by the ULP to change the default primary path or to force
4944 * sending a packet to a specific transport address. [All of this
4945 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4948 * Mandatory attributes:
4950 * o local SCTP instance name - obtained from the INITIALIZE operation.
4951 * [This is the argument asoc.]
4952 * o destination transport addr - specified as one of the transport
4953 * addresses of the peer endpoint with which the association is to be
4955 * [This is asoc->peer.active_path.]
4956 * o outbound stream count - the number of outbound streams the ULP
4957 * would like to open towards this peer endpoint.
4958 * [BUG: This is not currently implemented.]
4959 * Optional attributes:
4963 * The return value is a disposition.
4965 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4966 const struct sctp_endpoint *ep,
4967 const struct sctp_association *asoc,
4968 const union sctp_subtype type,
4970 struct sctp_cmd_seq *commands)
4972 struct sctp_association *my_asoc;
4973 struct sctp_chunk *repl;
4975 /* The comment below says that we enter COOKIE-WAIT AFTER
4976 * sending the INIT, but that doesn't actually work in our
4979 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4980 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4982 /* RFC 2960 5.1 Normal Establishment of an Association
4984 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4985 * must provide its Verification Tag (Tag_A) in the Initiate
4986 * Tag field. Tag_A SHOULD be a random number in the range of
4987 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4990 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4994 /* Choose transport for INIT. */
4995 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4998 /* Cast away the const modifier, as we want to just
4999 * rerun it through as a sideffect.
5001 my_asoc = (struct sctp_association *)asoc;
5002 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
5004 /* After sending the INIT, "A" starts the T1-init timer and
5005 * enters the COOKIE-WAIT state.
5007 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5008 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5009 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5010 return SCTP_DISPOSITION_CONSUME;
5013 return SCTP_DISPOSITION_NOMEM;
5017 * Process the SEND primitive.
5019 * Section: 10.1 ULP-to-SCTP
5022 * Format: SEND(association id, buffer address, byte count [,context]
5023 * [,stream id] [,life time] [,destination transport address]
5024 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
5027 * This is the main method to send user data via SCTP.
5029 * Mandatory attributes:
5031 * o association id - local handle to the SCTP association
5033 * o buffer address - the location where the user message to be
5034 * transmitted is stored;
5036 * o byte count - The size of the user data in number of bytes;
5038 * Optional attributes:
5040 * o context - an optional 32 bit integer that will be carried in the
5041 * sending failure notification to the ULP if the transportation of
5042 * this User Message fails.
5044 * o stream id - to indicate which stream to send the data on. If not
5045 * specified, stream 0 will be used.
5047 * o life time - specifies the life time of the user data. The user data
5048 * will not be sent by SCTP after the life time expires. This
5049 * parameter can be used to avoid efforts to transmit stale
5050 * user messages. SCTP notifies the ULP if the data cannot be
5051 * initiated to transport (i.e. sent to the destination via SCTP's
5052 * send primitive) within the life time variable. However, the
5053 * user data will be transmitted if SCTP has attempted to transmit a
5054 * chunk before the life time expired.
5056 * o destination transport address - specified as one of the destination
5057 * transport addresses of the peer endpoint to which this packet
5058 * should be sent. Whenever possible, SCTP should use this destination
5059 * transport address for sending the packets, instead of the current
5062 * o unorder flag - this flag, if present, indicates that the user
5063 * would like the data delivered in an unordered fashion to the peer
5064 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
5067 * o no-bundle flag - instructs SCTP not to bundle this user data with
5068 * other outbound DATA chunks. SCTP MAY still bundle even when
5069 * this flag is present, when faced with network congestion.
5071 * o payload protocol-id - A 32 bit unsigned integer that is to be
5072 * passed to the peer indicating the type of payload protocol data
5073 * being transmitted. This value is passed as opaque data by SCTP.
5075 * The return value is the disposition.
5077 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
5078 const struct sctp_endpoint *ep,
5079 const struct sctp_association *asoc,
5080 const union sctp_subtype type,
5082 struct sctp_cmd_seq *commands)
5084 struct sctp_datamsg *msg = arg;
5086 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
5087 return SCTP_DISPOSITION_CONSUME;
5091 * Process the SHUTDOWN primitive.
5096 * Format: SHUTDOWN(association id)
5099 * Gracefully closes an association. Any locally queued user data
5100 * will be delivered to the peer. The association will be terminated only
5101 * after the peer acknowledges all the SCTP packets sent. A success code
5102 * will be returned on successful termination of the association. If
5103 * attempting to terminate the association results in a failure, an error
5104 * code shall be returned.
5106 * Mandatory attributes:
5108 * o association id - local handle to the SCTP association
5110 * Optional attributes:
5114 * The return value is the disposition.
5116 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
5118 const struct sctp_endpoint *ep,
5119 const struct sctp_association *asoc,
5120 const union sctp_subtype type,
5122 struct sctp_cmd_seq *commands)
5124 enum sctp_disposition disposition;
5126 /* From 9.2 Shutdown of an Association
5127 * Upon receipt of the SHUTDOWN primitive from its upper
5128 * layer, the endpoint enters SHUTDOWN-PENDING state and
5129 * remains there until all outstanding data has been
5130 * acknowledged by its peer. The endpoint accepts no new data
5131 * from its upper layer, but retransmits data to the far end
5132 * if necessary to fill gaps.
5134 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5135 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5137 disposition = SCTP_DISPOSITION_CONSUME;
5138 if (sctp_outq_is_empty(&asoc->outqueue)) {
5139 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5147 * Process the ABORT primitive.
5152 * Format: Abort(association id [, cause code])
5155 * Ungracefully closes an association. Any locally queued user data
5156 * will be discarded and an ABORT chunk is sent to the peer. A success code
5157 * will be returned on successful abortion of the association. If
5158 * attempting to abort the association results in a failure, an error
5159 * code shall be returned.
5161 * Mandatory attributes:
5163 * o association id - local handle to the SCTP association
5165 * Optional attributes:
5167 * o cause code - reason of the abort to be passed to the peer
5171 * The return value is the disposition.
5173 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5175 const struct sctp_endpoint *ep,
5176 const struct sctp_association *asoc,
5177 const union sctp_subtype type,
5179 struct sctp_cmd_seq *commands)
5181 /* From 9.1 Abort of an Association
5182 * Upon receipt of the ABORT primitive from its upper
5183 * layer, the endpoint enters CLOSED state and
5184 * discard all outstanding data has been
5185 * acknowledged by its peer. The endpoint accepts no new data
5186 * from its upper layer, but retransmits data to the far end
5187 * if necessary to fill gaps.
5189 struct sctp_chunk *abort = arg;
5192 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5194 /* Even if we can't send the ABORT due to low memory delete the
5195 * TCB. This is a departure from our typical NOMEM handling.
5198 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5199 SCTP_ERROR(ECONNABORTED));
5200 /* Delete the established association. */
5201 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5202 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5204 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5205 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5207 return SCTP_DISPOSITION_ABORT;
5210 /* We tried an illegal operation on an association which is closed. */
5211 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5212 const struct sctp_endpoint *ep,
5213 const struct sctp_association *asoc,
5214 const union sctp_subtype type,
5216 struct sctp_cmd_seq *commands)
5218 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5219 return SCTP_DISPOSITION_CONSUME;
5222 /* We tried an illegal operation on an association which is shutting
5225 enum sctp_disposition sctp_sf_error_shutdown(
5227 const struct sctp_endpoint *ep,
5228 const struct sctp_association *asoc,
5229 const union sctp_subtype type,
5231 struct sctp_cmd_seq *commands)
5233 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5234 SCTP_ERROR(-ESHUTDOWN));
5235 return SCTP_DISPOSITION_CONSUME;
5239 * sctp_cookie_wait_prm_shutdown
5241 * Section: 4 Note: 2
5246 * The RFC does not explicitly address this issue, but is the route through the
5247 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5252 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5254 const struct sctp_endpoint *ep,
5255 const struct sctp_association *asoc,
5256 const union sctp_subtype type,
5258 struct sctp_cmd_seq *commands)
5260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5261 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5263 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5264 SCTP_STATE(SCTP_STATE_CLOSED));
5266 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5268 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5270 return SCTP_DISPOSITION_DELETE_TCB;
5274 * sctp_cookie_echoed_prm_shutdown
5276 * Section: 4 Note: 2
5281 * The RFC does not explicitly address this issue, but is the route through the
5282 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5287 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5289 const struct sctp_endpoint *ep,
5290 const struct sctp_association *asoc,
5291 const union sctp_subtype type,
5293 struct sctp_cmd_seq *commands)
5295 /* There is a single T1 timer, so we should be able to use
5296 * common function with the COOKIE-WAIT state.
5298 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5302 * sctp_sf_cookie_wait_prm_abort
5304 * Section: 4 Note: 2
5309 * The RFC does not explicitly address this issue, but is the route through the
5310 * state table when someone issues an abort while in COOKIE_WAIT state.
5315 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5317 const struct sctp_endpoint *ep,
5318 const struct sctp_association *asoc,
5319 const union sctp_subtype type,
5321 struct sctp_cmd_seq *commands)
5323 struct sctp_chunk *abort = arg;
5325 /* Stop T1-init timer */
5326 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5327 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5330 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5332 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5333 SCTP_STATE(SCTP_STATE_CLOSED));
5335 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5337 /* Even if we can't send the ABORT due to low memory delete the
5338 * TCB. This is a departure from our typical NOMEM handling.
5341 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5342 SCTP_ERROR(ECONNREFUSED));
5343 /* Delete the established association. */
5344 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5345 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5347 return SCTP_DISPOSITION_ABORT;
5351 * sctp_sf_cookie_echoed_prm_abort
5353 * Section: 4 Note: 3
5358 * The RFC does not explcitly address this issue, but is the route through the
5359 * state table when someone issues an abort while in COOKIE_ECHOED state.
5364 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5366 const struct sctp_endpoint *ep,
5367 const struct sctp_association *asoc,
5368 const union sctp_subtype type,
5370 struct sctp_cmd_seq *commands)
5372 /* There is a single T1 timer, so we should be able to use
5373 * common function with the COOKIE-WAIT state.
5375 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5379 * sctp_sf_shutdown_pending_prm_abort
5384 * The RFC does not explicitly address this issue, but is the route through the
5385 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5390 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5392 const struct sctp_endpoint *ep,
5393 const struct sctp_association *asoc,
5394 const union sctp_subtype type,
5396 struct sctp_cmd_seq *commands)
5398 /* Stop the T5-shutdown guard timer. */
5399 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5400 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5402 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5406 * sctp_sf_shutdown_sent_prm_abort
5411 * The RFC does not explicitly address this issue, but is the route through the
5412 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5417 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5419 const struct sctp_endpoint *ep,
5420 const struct sctp_association *asoc,
5421 const union sctp_subtype type,
5423 struct sctp_cmd_seq *commands)
5425 /* Stop the T2-shutdown timer. */
5426 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5427 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5429 /* Stop the T5-shutdown guard timer. */
5430 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5431 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5433 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5437 * sctp_sf_cookie_echoed_prm_abort
5442 * The RFC does not explcitly address this issue, but is the route through the
5443 * state table when someone issues an abort while in COOKIE_ECHOED state.
5448 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5450 const struct sctp_endpoint *ep,
5451 const struct sctp_association *asoc,
5452 const union sctp_subtype type,
5454 struct sctp_cmd_seq *commands)
5456 /* The same T2 timer, so we should be able to use
5457 * common function with the SHUTDOWN-SENT state.
5459 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5463 * Process the REQUESTHEARTBEAT primitive
5466 * J) Request Heartbeat
5468 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5472 * Instructs the local endpoint to perform a HeartBeat on the specified
5473 * destination transport address of the given association. The returned
5474 * result should indicate whether the transmission of the HEARTBEAT
5475 * chunk to the destination address is successful.
5477 * Mandatory attributes:
5479 * o association id - local handle to the SCTP association
5481 * o destination transport address - the transport address of the
5482 * association on which a heartbeat should be issued.
5484 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5486 const struct sctp_endpoint *ep,
5487 const struct sctp_association *asoc,
5488 const union sctp_subtype type,
5490 struct sctp_cmd_seq *commands)
5492 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5493 (struct sctp_transport *)arg, commands))
5494 return SCTP_DISPOSITION_NOMEM;
5497 * RFC 2960 (bis), section 8.3
5499 * D) Request an on-demand HEARTBEAT on a specific destination
5500 * transport address of a given association.
5502 * The endpoint should increment the respective error counter of
5503 * the destination transport address each time a HEARTBEAT is sent
5504 * to that address and not acknowledged within one RTO.
5507 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5508 SCTP_TRANSPORT(arg));
5509 return SCTP_DISPOSITION_CONSUME;
5513 * ADDIP Section 4.1 ASCONF Chunk Procedures
5514 * When an endpoint has an ASCONF signaled change to be sent to the
5515 * remote endpoint it should do A1 to A9
5517 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5518 const struct sctp_endpoint *ep,
5519 const struct sctp_association *asoc,
5520 const union sctp_subtype type,
5522 struct sctp_cmd_seq *commands)
5524 struct sctp_chunk *chunk = arg;
5526 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5527 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5528 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5529 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5530 return SCTP_DISPOSITION_CONSUME;
5533 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5534 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5535 const struct sctp_endpoint *ep,
5536 const struct sctp_association *asoc,
5537 const union sctp_subtype type,
5539 struct sctp_cmd_seq *commands)
5541 struct sctp_chunk *chunk = arg;
5543 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5544 return SCTP_DISPOSITION_CONSUME;
5548 * Ignore the primitive event
5550 * The return value is the disposition of the primitive.
5552 enum sctp_disposition sctp_sf_ignore_primitive(
5554 const struct sctp_endpoint *ep,
5555 const struct sctp_association *asoc,
5556 const union sctp_subtype type,
5558 struct sctp_cmd_seq *commands)
5560 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5563 return SCTP_DISPOSITION_DISCARD;
5566 /***************************************************************************
5567 * These are the state functions for the OTHER events.
5568 ***************************************************************************/
5571 * When the SCTP stack has no more user data to send or retransmit, this
5572 * notification is given to the user. Also, at the time when a user app
5573 * subscribes to this event, if there is no data to be sent or
5574 * retransmit, the stack will immediately send up this notification.
5576 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5578 const struct sctp_endpoint *ep,
5579 const struct sctp_association *asoc,
5580 const union sctp_subtype type,
5582 struct sctp_cmd_seq *commands)
5584 struct sctp_ulpevent *event;
5586 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5588 return SCTP_DISPOSITION_NOMEM;
5590 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5592 return SCTP_DISPOSITION_CONSUME;
5596 * Start the shutdown negotiation.
5599 * Once all its outstanding data has been acknowledged, the endpoint
5600 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5601 * TSN Ack field the last sequential TSN it has received from the peer.
5602 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5603 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5604 * with the updated last sequential TSN received from its peer.
5606 * The return value is the disposition.
5608 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5610 const struct sctp_endpoint *ep,
5611 const struct sctp_association *asoc,
5612 const union sctp_subtype type,
5614 struct sctp_cmd_seq *commands)
5616 struct sctp_chunk *reply;
5618 /* Once all its outstanding data has been acknowledged, the
5619 * endpoint shall send a SHUTDOWN chunk to its peer including
5620 * in the Cumulative TSN Ack field the last sequential TSN it
5621 * has received from the peer.
5623 reply = sctp_make_shutdown(asoc, arg);
5627 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5628 * T2-shutdown timer.
5630 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5632 /* It shall then start the T2-shutdown timer */
5633 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5634 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5636 /* RFC 4960 Section 9.2
5637 * The sender of the SHUTDOWN MAY also start an overall guard timer
5638 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5640 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5641 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5643 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5644 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5645 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5647 /* and enter the SHUTDOWN-SENT state. */
5648 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5649 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5651 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5653 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5656 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5658 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5660 return SCTP_DISPOSITION_CONSUME;
5663 return SCTP_DISPOSITION_NOMEM;
5667 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5671 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5672 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5673 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5674 * endpoint must re-send the SHUTDOWN ACK.
5676 * The return value is the disposition.
5678 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5680 const struct sctp_endpoint *ep,
5681 const struct sctp_association *asoc,
5682 const union sctp_subtype type,
5684 struct sctp_cmd_seq *commands)
5686 struct sctp_chunk *chunk = arg;
5687 struct sctp_chunk *reply;
5689 /* There are 2 ways of getting here:
5690 * 1) called in response to a SHUTDOWN chunk
5691 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5693 * For the case (2), the arg parameter is set to NULL. We need
5694 * to check that we have a chunk before accessing it's fields.
5697 if (!sctp_vtag_verify(chunk, asoc))
5698 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5701 /* Make sure that the SHUTDOWN chunk has a valid length. */
5702 if (!sctp_chunk_length_valid(
5703 chunk, sizeof(struct sctp_shutdown_chunk)))
5704 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5708 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5709 * shall send a SHUTDOWN ACK ...
5711 reply = sctp_make_shutdown_ack(asoc, chunk);
5715 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5716 * the T2-shutdown timer.
5718 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5720 /* and start/restart a T2-shutdown timer of its own, */
5721 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5722 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5724 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5725 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5726 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5728 /* Enter the SHUTDOWN-ACK-SENT state. */
5729 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5730 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5732 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5734 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5737 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5739 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5741 return SCTP_DISPOSITION_CONSUME;
5744 return SCTP_DISPOSITION_NOMEM;
5748 * Ignore the event defined as other
5750 * The return value is the disposition of the event.
5752 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5753 const struct sctp_endpoint *ep,
5754 const struct sctp_association *asoc,
5755 const union sctp_subtype type,
5757 struct sctp_cmd_seq *commands)
5759 pr_debug("%s: the event other type:%d is ignored\n",
5760 __func__, type.other);
5762 return SCTP_DISPOSITION_DISCARD;
5765 /************************************************************
5766 * These are the state functions for handling timeout events.
5767 ************************************************************/
5772 * Section: 6.3.3 Handle T3-rtx Expiration
5774 * Whenever the retransmission timer T3-rtx expires for a destination
5775 * address, do the following:
5778 * The return value is the disposition of the chunk.
5780 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5781 const struct sctp_endpoint *ep,
5782 const struct sctp_association *asoc,
5783 const union sctp_subtype type,
5785 struct sctp_cmd_seq *commands)
5787 struct sctp_transport *transport = arg;
5789 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5791 if (asoc->overall_error_count >= asoc->max_retrans) {
5792 if (asoc->peer.zero_window_announced &&
5793 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5795 * We are here likely because the receiver had its rwnd
5796 * closed for a while and we have not been able to
5797 * transmit the locally queued data within the maximum
5798 * retransmission attempts limit. Start the T5
5799 * shutdown guard timer to give the receiver one last
5800 * chance and some additional time to recover before
5803 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5804 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5806 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5807 SCTP_ERROR(ETIMEDOUT));
5808 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5809 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5810 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5811 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5812 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5813 return SCTP_DISPOSITION_DELETE_TCB;
5817 /* E1) For the destination address for which the timer
5818 * expires, adjust its ssthresh with rules defined in Section
5819 * 7.2.3 and set the cwnd <- MTU.
5822 /* E2) For the destination address for which the timer
5823 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5824 * maximum value discussed in rule C7 above (RTO.max) may be
5825 * used to provide an upper bound to this doubling operation.
5828 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5829 * outstanding DATA chunks for the address for which the
5830 * T3-rtx has expired will fit into a single packet, subject
5831 * to the MTU constraint for the path corresponding to the
5832 * destination transport address to which the retransmission
5833 * is being sent (this may be different from the address for
5834 * which the timer expires [see Section 6.4]). Call this
5835 * value K. Bundle and retransmit those K DATA chunks in a
5836 * single packet to the destination endpoint.
5838 * Note: Any DATA chunks that were sent to the address for
5839 * which the T3-rtx timer expired but did not fit in one MTU
5840 * (rule E3 above), should be marked for retransmission and
5841 * sent as soon as cwnd allows (normally when a SACK arrives).
5844 /* Do some failure management (Section 8.2). */
5845 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5847 /* NB: Rules E4 and F1 are implicit in R1. */
5848 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5850 return SCTP_DISPOSITION_CONSUME;
5854 * Generate delayed SACK on timeout
5856 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5858 * The guidelines on delayed acknowledgement algorithm specified in
5859 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5860 * acknowledgement SHOULD be generated for at least every second packet
5861 * (not every second DATA chunk) received, and SHOULD be generated
5862 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5863 * some situations it may be beneficial for an SCTP transmitter to be
5864 * more conservative than the algorithms detailed in this document
5865 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5866 * the following algorithms allow.
5868 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5869 const struct sctp_endpoint *ep,
5870 const struct sctp_association *asoc,
5871 const union sctp_subtype type,
5873 struct sctp_cmd_seq *commands)
5875 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5876 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5877 return SCTP_DISPOSITION_CONSUME;
5881 * sctp_sf_t1_init_timer_expire
5883 * Section: 4 Note: 2
5888 * RFC 2960 Section 4 Notes
5889 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5890 * and re-start the T1-init timer without changing state. This MUST
5891 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5892 * endpoint MUST abort the initialization process and report the
5893 * error to SCTP user.
5899 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5901 const struct sctp_endpoint *ep,
5902 const struct sctp_association *asoc,
5903 const union sctp_subtype type,
5905 struct sctp_cmd_seq *commands)
5907 int attempts = asoc->init_err_counter + 1;
5908 struct sctp_chunk *repl = NULL;
5909 struct sctp_bind_addr *bp;
5911 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5913 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5915 if (attempts <= asoc->max_init_attempts) {
5916 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5917 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5919 return SCTP_DISPOSITION_NOMEM;
5921 /* Choose transport for INIT. */
5922 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5925 /* Issue a sideeffect to do the needed accounting. */
5926 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5927 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5929 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5931 pr_debug("%s: giving up on INIT, attempts:%d "
5932 "max_init_attempts:%d\n", __func__, attempts,
5933 asoc->max_init_attempts);
5935 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5936 SCTP_ERROR(ETIMEDOUT));
5937 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5938 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5939 return SCTP_DISPOSITION_DELETE_TCB;
5942 return SCTP_DISPOSITION_CONSUME;
5946 * sctp_sf_t1_cookie_timer_expire
5948 * Section: 4 Note: 2
5953 * RFC 2960 Section 4 Notes
5954 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5955 * COOKIE ECHO and re-start the T1-cookie timer without changing
5956 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5957 * After that, the endpoint MUST abort the initialization process and
5958 * report the error to SCTP user.
5964 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5966 const struct sctp_endpoint *ep,
5967 const struct sctp_association *asoc,
5968 const union sctp_subtype type,
5970 struct sctp_cmd_seq *commands)
5972 int attempts = asoc->init_err_counter + 1;
5973 struct sctp_chunk *repl = NULL;
5975 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5977 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5979 if (attempts <= asoc->max_init_attempts) {
5980 repl = sctp_make_cookie_echo(asoc, NULL);
5982 return SCTP_DISPOSITION_NOMEM;
5984 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5986 /* Issue a sideeffect to do the needed accounting. */
5987 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5988 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5990 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5992 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5993 SCTP_ERROR(ETIMEDOUT));
5994 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5995 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5996 return SCTP_DISPOSITION_DELETE_TCB;
5999 return SCTP_DISPOSITION_CONSUME;
6002 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
6003 * with the updated last sequential TSN received from its peer.
6005 * An endpoint should limit the number of retransmission of the
6006 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
6007 * If this threshold is exceeded the endpoint should destroy the TCB and
6008 * MUST report the peer endpoint unreachable to the upper layer (and
6009 * thus the association enters the CLOSED state). The reception of any
6010 * packet from its peer (i.e. as the peer sends all of its queued DATA
6011 * chunks) should clear the endpoint's retransmission count and restart
6012 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
6013 * all of its queued DATA chunks that have not yet been sent.
6015 enum sctp_disposition sctp_sf_t2_timer_expire(
6017 const struct sctp_endpoint *ep,
6018 const struct sctp_association *asoc,
6019 const union sctp_subtype type,
6021 struct sctp_cmd_seq *commands)
6023 struct sctp_chunk *reply = NULL;
6025 pr_debug("%s: timer T2 expired\n", __func__);
6027 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
6029 ((struct sctp_association *)asoc)->shutdown_retries++;
6031 if (asoc->overall_error_count >= asoc->max_retrans) {
6032 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6033 SCTP_ERROR(ETIMEDOUT));
6034 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
6035 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6036 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6037 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6038 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6039 return SCTP_DISPOSITION_DELETE_TCB;
6042 switch (asoc->state) {
6043 case SCTP_STATE_SHUTDOWN_SENT:
6044 reply = sctp_make_shutdown(asoc, NULL);
6047 case SCTP_STATE_SHUTDOWN_ACK_SENT:
6048 reply = sctp_make_shutdown_ack(asoc, NULL);
6059 /* Do some failure management (Section 8.2).
6060 * If we remove the transport an SHUTDOWN was last sent to, don't
6061 * do failure management.
6063 if (asoc->shutdown_last_sent_to)
6064 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6065 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
6067 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
6068 * the T2-shutdown timer.
6070 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
6072 /* Restart the T2-shutdown timer. */
6073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6074 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
6075 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6076 return SCTP_DISPOSITION_CONSUME;
6079 return SCTP_DISPOSITION_NOMEM;
6083 * ADDIP Section 4.1 ASCONF Chunk Procedures
6084 * If the T4 RTO timer expires the endpoint should do B1 to B5
6086 enum sctp_disposition sctp_sf_t4_timer_expire(
6088 const struct sctp_endpoint *ep,
6089 const struct sctp_association *asoc,
6090 const union sctp_subtype type,
6092 struct sctp_cmd_seq *commands)
6094 struct sctp_chunk *chunk = asoc->addip_last_asconf;
6095 struct sctp_transport *transport = chunk->transport;
6097 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
6099 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
6100 * detection on the appropriate destination address as defined in
6101 * RFC2960 [5] section 8.1 and 8.2.
6104 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6105 SCTP_TRANSPORT(transport));
6107 /* Reconfig T4 timer and transport. */
6108 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
6110 /* ADDIP 4.1 B2) Increment the association error counters and perform
6111 * endpoint failure detection on the association as defined in
6112 * RFC2960 [5] section 8.1 and 8.2.
6113 * association error counter is incremented in SCTP_CMD_STRIKE.
6115 if (asoc->overall_error_count >= asoc->max_retrans) {
6116 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
6117 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6118 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6119 SCTP_ERROR(ETIMEDOUT));
6120 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6121 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6122 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6123 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6124 return SCTP_DISPOSITION_ABORT;
6127 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
6128 * the ASCONF chunk was sent by doubling the RTO timer value.
6129 * This is done in SCTP_CMD_STRIKE.
6132 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
6133 * choose an alternate destination address (please refer to RFC2960
6134 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
6135 * chunk, it MUST be the same (including its serial number) as the last
6138 sctp_chunk_hold(asoc->addip_last_asconf);
6139 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6140 SCTP_CHUNK(asoc->addip_last_asconf));
6142 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
6143 * destination is selected, then the RTO used will be that of the new
6144 * destination address.
6146 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6147 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6149 return SCTP_DISPOSITION_CONSUME;
6152 /* sctpimpguide-05 Section 2.12.2
6153 * The sender of the SHUTDOWN MAY also start an overall guard timer
6154 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6155 * At the expiration of this timer the sender SHOULD abort the association
6156 * by sending an ABORT chunk.
6158 enum sctp_disposition sctp_sf_t5_timer_expire(
6160 const struct sctp_endpoint *ep,
6161 const struct sctp_association *asoc,
6162 const union sctp_subtype type,
6164 struct sctp_cmd_seq *commands)
6166 struct sctp_chunk *reply = NULL;
6168 pr_debug("%s: timer T5 expired\n", __func__);
6170 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6172 reply = sctp_make_abort(asoc, NULL, 0);
6176 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6177 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6178 SCTP_ERROR(ETIMEDOUT));
6179 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6180 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6182 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6183 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6185 return SCTP_DISPOSITION_DELETE_TCB;
6187 return SCTP_DISPOSITION_NOMEM;
6190 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6191 * the association is automatically closed by starting the shutdown process.
6192 * The work that needs to be done is same as when SHUTDOWN is initiated by
6193 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6195 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6197 const struct sctp_endpoint *ep,
6198 const struct sctp_association *asoc,
6199 const union sctp_subtype type,
6201 struct sctp_cmd_seq *commands)
6203 enum sctp_disposition disposition;
6205 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6207 /* From 9.2 Shutdown of an Association
6208 * Upon receipt of the SHUTDOWN primitive from its upper
6209 * layer, the endpoint enters SHUTDOWN-PENDING state and
6210 * remains there until all outstanding data has been
6211 * acknowledged by its peer. The endpoint accepts no new data
6212 * from its upper layer, but retransmits data to the far end
6213 * if necessary to fill gaps.
6215 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6216 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6218 disposition = SCTP_DISPOSITION_CONSUME;
6219 if (sctp_outq_is_empty(&asoc->outqueue)) {
6220 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6227 /*****************************************************************************
6228 * These are sa state functions which could apply to all types of events.
6229 ****************************************************************************/
6232 * This table entry is not implemented.
6235 * (endpoint, asoc, chunk)
6237 * The return value is the disposition of the chunk.
6239 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6240 const struct sctp_endpoint *ep,
6241 const struct sctp_association *asoc,
6242 const union sctp_subtype type,
6243 void *arg, struct sctp_cmd_seq *commands)
6245 return SCTP_DISPOSITION_NOT_IMPL;
6249 * This table entry represents a bug.
6252 * (endpoint, asoc, chunk)
6254 * The return value is the disposition of the chunk.
6256 enum sctp_disposition sctp_sf_bug(struct net *net,
6257 const struct sctp_endpoint *ep,
6258 const struct sctp_association *asoc,
6259 const union sctp_subtype type,
6260 void *arg, struct sctp_cmd_seq *commands)
6262 return SCTP_DISPOSITION_BUG;
6266 * This table entry represents the firing of a timer in the wrong state.
6267 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6268 * when the association is in the wrong state. This event should
6269 * be ignored, so as to prevent any rearming of the timer.
6272 * (endpoint, asoc, chunk)
6274 * The return value is the disposition of the chunk.
6276 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6277 const struct sctp_endpoint *ep,
6278 const struct sctp_association *asoc,
6279 const union sctp_subtype type,
6281 struct sctp_cmd_seq *commands)
6283 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6285 return SCTP_DISPOSITION_CONSUME;
6288 /********************************************************************
6289 * 2nd Level Abstractions
6290 ********************************************************************/
6292 /* Pull the SACK chunk based on the SACK header. */
6293 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6295 struct sctp_sackhdr *sack;
6300 /* Protect ourselves from reading too far into
6301 * the skb from a bogus sender.
6303 sack = (struct sctp_sackhdr *) chunk->skb->data;
6305 num_blocks = ntohs(sack->num_gap_ack_blocks);
6306 num_dup_tsns = ntohs(sack->num_dup_tsns);
6307 len = sizeof(struct sctp_sackhdr);
6308 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6309 if (len > chunk->skb->len)
6312 skb_pull(chunk->skb, len);
6317 /* Create an ABORT packet to be sent as a response, with the specified
6320 static struct sctp_packet *sctp_abort_pkt_new(
6322 const struct sctp_endpoint *ep,
6323 const struct sctp_association *asoc,
6324 struct sctp_chunk *chunk,
6325 const void *payload, size_t paylen)
6327 struct sctp_packet *packet;
6328 struct sctp_chunk *abort;
6330 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6334 * The T bit will be set if the asoc is NULL.
6336 abort = sctp_make_abort(asoc, chunk, paylen);
6338 sctp_ootb_pkt_free(packet);
6342 /* Reflect vtag if T-Bit is set */
6343 if (sctp_test_T_bit(abort))
6344 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6346 /* Add specified error causes, i.e., payload, to the
6349 sctp_addto_chunk(abort, paylen, payload);
6351 /* Set the skb to the belonging sock for accounting. */
6352 abort->skb->sk = ep->base.sk;
6354 sctp_packet_append_chunk(packet, abort);
6361 /* Allocate a packet for responding in the OOTB conditions. */
6362 static struct sctp_packet *sctp_ootb_pkt_new(
6364 const struct sctp_association *asoc,
6365 const struct sctp_chunk *chunk)
6367 struct sctp_transport *transport;
6368 struct sctp_packet *packet;
6372 /* Get the source and destination port from the inbound packet. */
6373 sport = ntohs(chunk->sctp_hdr->dest);
6374 dport = ntohs(chunk->sctp_hdr->source);
6376 /* The V-tag is going to be the same as the inbound packet if no
6377 * association exists, otherwise, use the peer's vtag.
6380 /* Special case the INIT-ACK as there is no peer's vtag
6383 switch (chunk->chunk_hdr->type) {
6385 case SCTP_CID_INIT_ACK:
6387 struct sctp_initack_chunk *initack;
6389 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6390 vtag = ntohl(initack->init_hdr.init_tag);
6394 vtag = asoc->peer.i.init_tag;
6398 /* Special case the INIT and stale COOKIE_ECHO as there is no
6401 switch (chunk->chunk_hdr->type) {
6404 struct sctp_init_chunk *init;
6406 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6407 vtag = ntohl(init->init_hdr.init_tag);
6411 vtag = ntohl(chunk->sctp_hdr->vtag);
6416 /* Make a transport for the bucket, Eliza... */
6417 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6421 transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
6423 /* Cache a route for the transport with the chunk's destination as
6424 * the source address.
6426 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6427 sctp_sk(net->sctp.ctl_sock));
6429 packet = &transport->packet;
6430 sctp_packet_init(packet, transport, sport, dport);
6431 sctp_packet_config(packet, vtag, 0);
6439 /* Free the packet allocated earlier for responding in the OOTB condition. */
6440 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6442 sctp_transport_free(packet->transport);
6445 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6446 static void sctp_send_stale_cookie_err(struct net *net,
6447 const struct sctp_endpoint *ep,
6448 const struct sctp_association *asoc,
6449 const struct sctp_chunk *chunk,
6450 struct sctp_cmd_seq *commands,
6451 struct sctp_chunk *err_chunk)
6453 struct sctp_packet *packet;
6456 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6458 struct sctp_signed_cookie *cookie;
6460 /* Override the OOTB vtag from the cookie. */
6461 cookie = chunk->subh.cookie_hdr;
6462 packet->vtag = cookie->c.peer_vtag;
6464 /* Set the skb to the belonging sock for accounting. */
6465 err_chunk->skb->sk = ep->base.sk;
6466 sctp_packet_append_chunk(packet, err_chunk);
6467 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6468 SCTP_PACKET(packet));
6469 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6471 sctp_chunk_free (err_chunk);
6476 /* Process a data chunk */
6477 static int sctp_eat_data(const struct sctp_association *asoc,
6478 struct sctp_chunk *chunk,
6479 struct sctp_cmd_seq *commands)
6481 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6482 struct sock *sk = asoc->base.sk;
6483 struct net *net = sock_net(sk);
6484 struct sctp_datahdr *data_hdr;
6485 struct sctp_chunk *err;
6486 enum sctp_verb deliver;
6491 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6492 chunk->subh.data_hdr = data_hdr;
6493 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6495 tsn = ntohl(data_hdr->tsn);
6496 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6498 /* ASSERT: Now skb->data is really the user data. */
6500 /* Process ECN based congestion.
6502 * Since the chunk structure is reused for all chunks within
6503 * a packet, we use ecn_ce_done to track if we've already
6504 * done CE processing for this packet.
6506 * We need to do ECN processing even if we plan to discard the
6510 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6511 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6512 chunk->ecn_ce_done = 1;
6514 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6515 /* Do real work as side effect. */
6516 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6521 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6523 /* The TSN is too high--silently discard the chunk and
6524 * count on it getting retransmitted later.
6527 chunk->asoc->stats.outofseqtsns++;
6528 return SCTP_IERROR_HIGH_TSN;
6529 } else if (tmp > 0) {
6530 /* This is a duplicate. Record it. */
6531 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6532 return SCTP_IERROR_DUP_TSN;
6535 /* This is a new TSN. */
6537 /* Discard if there is no room in the receive window.
6538 * Actually, allow a little bit of overflow (up to a MTU).
6540 datalen = ntohs(chunk->chunk_hdr->length);
6541 datalen -= sctp_datachk_len(&asoc->stream);
6543 deliver = SCTP_CMD_CHUNK_ULP;
6545 /* Think about partial delivery. */
6546 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6548 /* Even if we don't accept this chunk there is
6551 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6554 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6555 * seems a bit troublesome in that frag_point varies based on
6556 * PMTU. In cases, such as loopback, this might be a rather
6559 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6560 (datalen > asoc->rwnd + asoc->frag_point))) {
6562 /* If this is the next TSN, consider reneging to make
6563 * room. Note: Playing nice with a confused sender. A
6564 * malicious sender can still eat up all our buffer
6565 * space and in the future we may want to detect and
6566 * do more drastic reneging.
6568 if (sctp_tsnmap_has_gap(map) &&
6569 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6570 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6571 deliver = SCTP_CMD_RENEGE;
6573 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6574 __func__, tsn, datalen, asoc->rwnd);
6576 return SCTP_IERROR_IGNORE_TSN;
6581 * Also try to renege to limit our memory usage in the event that
6582 * we are under memory pressure
6583 * If we can't renege, don't worry about it, the sk_rmem_schedule
6584 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6585 * memory usage too much
6587 if (sk_under_memory_pressure(sk)) {
6588 if (sctp_tsnmap_has_gap(map) &&
6589 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6590 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6592 deliver = SCTP_CMD_RENEGE;
6597 * Section 3.3.10.9 No User Data (9)
6601 * No User Data: This error cause is returned to the originator of a
6602 * DATA chunk if a received DATA chunk has no user data.
6604 if (unlikely(0 == datalen)) {
6605 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6607 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6610 /* We are going to ABORT, so we might as well stop
6611 * processing the rest of the chunks in the packet.
6613 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6614 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6615 SCTP_ERROR(ECONNABORTED));
6616 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6617 SCTP_PERR(SCTP_ERROR_NO_DATA));
6618 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6619 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6620 return SCTP_IERROR_NO_DATA;
6623 chunk->data_accepted = 1;
6625 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6626 * if we renege and the chunk arrives again.
6628 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6629 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6631 chunk->asoc->stats.iuodchunks++;
6633 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6635 chunk->asoc->stats.iodchunks++;
6638 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6640 * If an endpoint receive a DATA chunk with an invalid stream
6641 * identifier, it shall acknowledge the reception of the DATA chunk
6642 * following the normal procedure, immediately send an ERROR chunk
6643 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6644 * and discard the DATA chunk.
6646 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6647 /* Mark tsn as received even though we drop it */
6648 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6650 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6652 sizeof(data_hdr->stream),
6655 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6657 return SCTP_IERROR_BAD_STREAM;
6660 /* Check to see if the SSN is possible for this TSN.
6661 * The biggest gap we can record is 4K wide. Since SSNs wrap
6662 * at an unsigned short, there is no way that an SSN can
6663 * wrap and for a valid TSN. We can simply check if the current
6664 * SSN is smaller then the next expected one. If it is, it wrapped
6667 if (!asoc->stream.si->validate_data(chunk))
6668 return SCTP_IERROR_PROTO_VIOLATION;
6670 /* Send the data up to the user. Note: Schedule the
6671 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6672 * chunk needs the updated rwnd.
6674 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6676 return SCTP_IERROR_NO_ERROR;