1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
8 * This is part of the SCTP Linux Kernel Implementation.
10 * These are the state functions for the state machine.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <linux-sctp@vger.kernel.org>
32 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/ipv6.h>
52 #include <linux/net.h>
53 #include <linux/inet.h>
54 #include <linux/slab.h>
56 #include <net/inet_ecn.h>
57 #include <linux/skbuff.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
60 #include <net/sctp/structs.h>
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/sctp.h>
65 static struct sctp_packet *sctp_abort_pkt_new(
67 const struct sctp_endpoint *ep,
68 const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 const void *payload, size_t paylen);
71 static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 struct sctp_cmd_seq *commands);
74 static struct sctp_packet *sctp_ootb_pkt_new(
76 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk);
78 static void sctp_send_stale_cookie_err(struct net *net,
79 const struct sctp_endpoint *ep,
80 const struct sctp_association *asoc,
81 const struct sctp_chunk *chunk,
82 struct sctp_cmd_seq *commands,
83 struct sctp_chunk *err_chunk);
84 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
86 const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const union sctp_subtype type,
90 struct sctp_cmd_seq *commands);
91 static enum sctp_disposition sctp_sf_shut_8_4_5(
93 const struct sctp_endpoint *ep,
94 const struct sctp_association *asoc,
95 const union sctp_subtype type,
97 struct sctp_cmd_seq *commands);
98 static enum sctp_disposition sctp_sf_tabort_8_4_8(
100 const struct sctp_endpoint *ep,
101 const struct sctp_association *asoc,
102 const union sctp_subtype type,
104 struct sctp_cmd_seq *commands);
105 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
107 static enum sctp_disposition sctp_stop_t1_and_abort(
109 struct sctp_cmd_seq *commands,
110 __be16 error, int sk_err,
111 const struct sctp_association *asoc,
112 struct sctp_transport *transport);
114 static enum sctp_disposition sctp_sf_abort_violation(
116 const struct sctp_endpoint *ep,
117 const struct sctp_association *asoc,
119 struct sctp_cmd_seq *commands,
121 const size_t paylen);
123 static enum sctp_disposition sctp_sf_violation_chunklen(
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const union sctp_subtype type,
129 struct sctp_cmd_seq *commands);
131 static enum sctp_disposition sctp_sf_violation_paramlen(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const union sctp_subtype type,
136 void *arg, void *ext,
137 struct sctp_cmd_seq *commands);
139 static enum sctp_disposition sctp_sf_violation_ctsn(
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_disposition sctp_sf_violation_chunk(
149 const struct sctp_endpoint *ep,
150 const struct sctp_association *asoc,
151 const union sctp_subtype type,
153 struct sctp_cmd_seq *commands);
155 static enum sctp_ierror sctp_sf_authenticate(
156 const struct sctp_association *asoc,
157 struct sctp_chunk *chunk);
159 static enum sctp_disposition __sctp_sf_do_9_1_abort(
161 const struct sctp_endpoint *ep,
162 const struct sctp_association *asoc,
163 const union sctp_subtype type,
165 struct sctp_cmd_seq *commands);
167 static enum sctp_disposition
168 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
169 const struct sctp_association *asoc,
170 const union sctp_subtype type, void *arg,
171 struct sctp_cmd_seq *commands);
173 /* Small helper function that checks if the chunk length
174 * is of the appropriate length. The 'required_length' argument
175 * is set to be the size of a specific chunk we are testing.
176 * Return Values: true = Valid length
177 * false = Invalid length
180 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
181 __u16 required_length)
183 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
185 /* Previously already marked? */
186 if (unlikely(chunk->pdiscard))
188 if (unlikely(chunk_length < required_length))
194 /* Check for format error in an ABORT chunk */
195 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
197 struct sctp_errhdr *err;
199 sctp_walk_errors(err, chunk->chunk_hdr);
201 return (void *)err == (void *)chunk->chunk_end;
204 /**********************************************************
205 * These are the state functions for handling chunk events.
206 **********************************************************/
209 * Process the final SHUTDOWN COMPLETE.
211 * Section: 4 (C) (diagram), 9.2
212 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
213 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
214 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
215 * should stop the T2-shutdown timer and remove all knowledge of the
216 * association (and thus the association enters the CLOSED state).
218 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
219 * C) Rules for packet carrying SHUTDOWN COMPLETE:
221 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
222 * if the Verification Tag field of the packet matches its own tag and
223 * the T bit is not set
225 * it is set to its peer's tag and the T bit is set in the Chunk
227 * Otherwise, the receiver MUST silently discard the packet
228 * and take no further action. An endpoint MUST ignore the
229 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
232 * (endpoint, asoc, chunk)
235 * (asoc, reply_msg, msg_up, timers, counters)
237 * The return value is the disposition of the chunk.
239 enum sctp_disposition sctp_sf_do_4_C(struct net *net,
240 const struct sctp_endpoint *ep,
241 const struct sctp_association *asoc,
242 const union sctp_subtype type,
243 void *arg, struct sctp_cmd_seq *commands)
245 struct sctp_chunk *chunk = arg;
246 struct sctp_ulpevent *ev;
248 if (!sctp_vtag_verify_either(chunk, asoc))
249 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
251 /* RFC 2960 6.10 Bundling
253 * An endpoint MUST NOT bundle INIT, INIT ACK or
254 * SHUTDOWN COMPLETE with any other chunks.
256 if (!chunk->singleton)
257 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
259 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
260 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
261 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
264 /* RFC 2960 10.2 SCTP-to-ULP
266 * H) SHUTDOWN COMPLETE notification
268 * When SCTP completes the shutdown procedures (section 9.2) this
269 * notification is passed to the upper layer.
271 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
272 0, 0, 0, NULL, GFP_ATOMIC);
274 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
277 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
278 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
279 * not the chunk should be discarded. If the endpoint is in
280 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
281 * T2-shutdown timer and remove all knowledge of the
282 * association (and thus the association enters the CLOSED
285 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
286 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
288 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
289 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
291 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
292 SCTP_STATE(SCTP_STATE_CLOSED));
294 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
295 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
297 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
299 return SCTP_DISPOSITION_DELETE_TCB;
303 * Respond to a normal INIT chunk.
304 * We are the side that is being asked for an association.
306 * Section: 5.1 Normal Establishment of an Association, B
307 * B) "Z" shall respond immediately with an INIT ACK chunk. The
308 * destination IP address of the INIT ACK MUST be set to the source
309 * IP address of the INIT to which this INIT ACK is responding. In
310 * the response, besides filling in other parameters, "Z" must set the
311 * Verification Tag field to Tag_A, and also provide its own
312 * Verification Tag (Tag_Z) in the Initiate Tag field.
314 * Verification Tag: Must be 0.
317 * (endpoint, asoc, chunk)
320 * (asoc, reply_msg, msg_up, timers, counters)
322 * The return value is the disposition of the chunk.
324 enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
325 const struct sctp_endpoint *ep,
326 const struct sctp_association *asoc,
327 const union sctp_subtype type,
329 struct sctp_cmd_seq *commands)
331 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
332 struct sctp_unrecognized_param *unk_param;
333 struct sctp_association *new_asoc;
334 struct sctp_packet *packet;
337 /* Update socket peer label if first association. */
338 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
340 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
343 * An endpoint MUST NOT bundle INIT, INIT ACK or
344 * SHUTDOWN COMPLETE with any other chunks.
347 * Furthermore, we require that the receiver of an INIT chunk MUST
348 * enforce these rules by silently discarding an arriving packet
349 * with an INIT chunk that is bundled with other chunks.
351 if (!chunk->singleton)
352 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
354 /* Make sure that the INIT chunk has a valid length.
355 * Normally, this would cause an ABORT with a Protocol Violation
356 * error, but since we don't have an association, we'll
357 * just discard the packet.
359 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
360 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
362 /* If the packet is an OOTB packet which is temporarily on the
363 * control endpoint, respond with an ABORT.
365 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
366 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
367 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
370 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
373 if (chunk->sctp_hdr->vtag != 0)
374 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
376 /* If the INIT is coming toward a closing socket, we'll send back
377 * and ABORT. Essentially, this catches the race of INIT being
378 * backloged to the socket at the same time as the user isses close().
379 * Since the socket and all its associations are going away, we
380 * can treat this OOTB
382 if (sctp_sstate(ep->base.sk, CLOSING))
383 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
385 /* Verify the INIT chunk before processing it. */
387 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
388 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
390 /* This chunk contains fatal error. It is to be discarded.
391 * Send an ABORT, with causes if there is any.
394 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
395 (__u8 *)(err_chunk->chunk_hdr) +
396 sizeof(struct sctp_chunkhdr),
397 ntohs(err_chunk->chunk_hdr->length) -
398 sizeof(struct sctp_chunkhdr));
400 sctp_chunk_free(err_chunk);
403 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
404 SCTP_PACKET(packet));
405 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
406 return SCTP_DISPOSITION_CONSUME;
408 return SCTP_DISPOSITION_NOMEM;
411 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
416 /* Grab the INIT header. */
417 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
419 /* Tag the variable length parameters. */
420 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
422 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
426 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
427 sctp_scope(sctp_source(chunk)),
431 /* The call, sctp_process_init(), can fail on memory allocation. */
432 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
433 (struct sctp_init_chunk *)chunk->chunk_hdr,
437 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
439 /* If there are errors need to be reported for unknown parameters,
440 * make sure to reserve enough room in the INIT ACK for them.
444 len = ntohs(err_chunk->chunk_hdr->length) -
445 sizeof(struct sctp_chunkhdr);
447 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
451 /* If there are errors need to be reported for unknown parameters,
452 * include them in the outgoing INIT ACK as "Unrecognized parameter"
456 /* Get the "Unrecognized parameter" parameter(s) out of the
457 * ERROR chunk generated by sctp_verify_init(). Since the
458 * error cause code for "unknown parameter" and the
459 * "Unrecognized parameter" type is the same, we can
460 * construct the parameters in INIT ACK by copying the
463 unk_param = (struct sctp_unrecognized_param *)
464 ((__u8 *)(err_chunk->chunk_hdr) +
465 sizeof(struct sctp_chunkhdr));
466 /* Replace the cause code with the "Unrecognized parameter"
469 sctp_addto_chunk(repl, len, unk_param);
470 sctp_chunk_free(err_chunk);
473 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
475 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
478 * Note: After sending out INIT ACK with the State Cookie parameter,
479 * "Z" MUST NOT allocate any resources, nor keep any states for the
480 * new association. Otherwise, "Z" will be vulnerable to resource
483 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
485 return SCTP_DISPOSITION_DELETE_TCB;
488 sctp_association_free(new_asoc);
491 sctp_chunk_free(err_chunk);
492 return SCTP_DISPOSITION_NOMEM;
496 * Respond to a normal INIT ACK chunk.
497 * We are the side that is initiating the association.
499 * Section: 5.1 Normal Establishment of an Association, C
500 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
501 * timer and leave COOKIE-WAIT state. "A" shall then send the State
502 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
503 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
505 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
506 * DATA chunks, but it MUST be the first chunk in the packet and
507 * until the COOKIE ACK is returned the sender MUST NOT send any
508 * other packets to the peer.
510 * Verification Tag: 3.3.3
511 * If the value of the Initiate Tag in a received INIT ACK chunk is
512 * found to be 0, the receiver MUST treat it as an error and close the
513 * association by transmitting an ABORT.
516 * (endpoint, asoc, chunk)
519 * (asoc, reply_msg, msg_up, timers, counters)
521 * The return value is the disposition of the chunk.
523 enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
524 const struct sctp_endpoint *ep,
525 const struct sctp_association *asoc,
526 const union sctp_subtype type,
528 struct sctp_cmd_seq *commands)
530 struct sctp_init_chunk *initchunk;
531 struct sctp_chunk *chunk = arg;
532 struct sctp_chunk *err_chunk;
533 struct sctp_packet *packet;
535 if (!sctp_vtag_verify(chunk, asoc))
536 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
539 * An endpoint MUST NOT bundle INIT, INIT ACK or
540 * SHUTDOWN COMPLETE with any other chunks.
542 if (!chunk->singleton)
543 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
545 /* Make sure that the INIT-ACK chunk has a valid length */
546 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
547 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
549 /* Grab the INIT header. */
550 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
552 /* Verify the INIT chunk before processing it. */
554 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
555 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
558 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
560 /* This chunk contains fatal error. It is to be discarded.
561 * Send an ABORT, with causes. If there are no causes,
562 * then there wasn't enough memory. Just terminate
566 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
567 (__u8 *)(err_chunk->chunk_hdr) +
568 sizeof(struct sctp_chunkhdr),
569 ntohs(err_chunk->chunk_hdr->length) -
570 sizeof(struct sctp_chunkhdr));
572 sctp_chunk_free(err_chunk);
575 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
576 SCTP_PACKET(packet));
577 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
578 error = SCTP_ERROR_INV_PARAM;
582 /* SCTP-AUTH, Section 6.3:
583 * It should be noted that if the receiver wants to tear
584 * down an association in an authenticated way only, the
585 * handling of malformed packets should not result in
586 * tearing down the association.
588 * This means that if we only want to abort associations
589 * in an authenticated way (i.e AUTH+ABORT), then we
590 * can't destroy this association just because the packet
593 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
594 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
596 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
597 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
598 asoc, chunk->transport);
601 /* Tag the variable length parameters. Note that we never
602 * convert the parameters in an INIT chunk.
604 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
606 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
608 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
609 SCTP_PEER_INIT(initchunk));
611 /* Reset init error count upon receipt of INIT-ACK. */
612 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
614 /* 5.1 C) "A" shall stop the T1-init timer and leave
615 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
616 * timer, and enter the COOKIE-ECHOED state.
618 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
619 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
620 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
621 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
623 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
625 /* SCTP-AUTH: genereate the assocition shared keys so that
626 * we can potentially signe the COOKIE-ECHO.
628 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
630 /* 5.1 C) "A" shall then send the State Cookie received in the
631 * INIT ACK chunk in a COOKIE ECHO chunk, ...
633 /* If there is any errors to report, send the ERROR chunk generated
634 * for unknown parameters as well.
636 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
637 SCTP_CHUNK(err_chunk));
639 return SCTP_DISPOSITION_CONSUME;
642 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
643 const struct sctp_association *asoc)
645 struct sctp_chunk auth;
647 if (!chunk->auth_chunk)
650 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
651 * is supposed to be authenticated and we have to do delayed
652 * authentication. We've just recreated the association using
653 * the information in the cookie and now it's much easier to
654 * do the authentication.
657 /* Make sure that we and the peer are AUTH capable */
658 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
661 /* set-up our fake chunk so that we can process it */
662 auth.skb = chunk->auth_chunk;
663 auth.asoc = chunk->asoc;
664 auth.sctp_hdr = chunk->sctp_hdr;
665 auth.chunk_hdr = (struct sctp_chunkhdr *)
666 skb_push(chunk->auth_chunk,
667 sizeof(struct sctp_chunkhdr));
668 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
669 auth.transport = chunk->transport;
671 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
675 * Respond to a normal COOKIE ECHO chunk.
676 * We are the side that is being asked for an association.
678 * Section: 5.1 Normal Establishment of an Association, D
679 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
680 * with a COOKIE ACK chunk after building a TCB and moving to
681 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
682 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
683 * chunk MUST be the first chunk in the packet.
685 * IMPLEMENTATION NOTE: An implementation may choose to send the
686 * Communication Up notification to the SCTP user upon reception
687 * of a valid COOKIE ECHO chunk.
689 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
690 * D) Rules for packet carrying a COOKIE ECHO
692 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
693 * Initial Tag received in the INIT ACK.
695 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
698 * (endpoint, asoc, chunk)
701 * (asoc, reply_msg, msg_up, timers, counters)
703 * The return value is the disposition of the chunk.
705 enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
706 const struct sctp_endpoint *ep,
707 const struct sctp_association *asoc,
708 const union sctp_subtype type,
710 struct sctp_cmd_seq *commands)
712 struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
713 struct sctp_association *new_asoc;
714 struct sctp_init_chunk *peer_init;
715 struct sctp_chunk *chunk = arg;
716 struct sctp_chunk *err_chk_p;
717 struct sctp_chunk *repl;
721 if (asoc && !sctp_vtag_verify(chunk, asoc))
722 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
724 /* If the packet is an OOTB packet which is temporarily on the
725 * control endpoint, respond with an ABORT.
727 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
728 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
729 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
732 /* Make sure that the COOKIE_ECHO chunk has a valid length.
733 * In this case, we check that we have enough for at least a
734 * chunk header. More detailed verification is done
735 * in sctp_unpack_cookie().
737 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
738 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
741 /* If the endpoint is not listening or if the number of associations
742 * on the TCP-style socket exceed the max backlog, respond with an
746 if (!sctp_sstate(sk, LISTENING) ||
747 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
748 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
750 /* "Decode" the chunk. We have no optional parameters so we
753 chunk->subh.cookie_hdr =
754 (struct sctp_signed_cookie *)chunk->skb->data;
755 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
756 sizeof(struct sctp_chunkhdr)))
759 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
760 * "Z" will reply with a COOKIE ACK chunk after building a TCB
761 * and moving to the ESTABLISHED state.
763 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
767 * If the re-build failed, what is the proper error path
770 * [We should abort the association. --piggy]
773 /* FIXME: Several errors are possible. A bad cookie should
774 * be silently discarded, but think about logging it too.
777 case -SCTP_IERROR_NOMEM:
780 case -SCTP_IERROR_STALE_COOKIE:
781 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
783 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
785 case -SCTP_IERROR_BAD_SIG:
787 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
792 /* Delay state machine commands until later.
794 * Re-build the bind address for the association is done in
795 * the sctp_unpack_cookie() already.
797 /* This is a brand-new association, so these are not yet side
798 * effects--it is safe to run them here.
800 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
802 if (!sctp_process_init(new_asoc, chunk,
803 &chunk->subh.cookie_hdr->c.peer_addr,
804 peer_init, GFP_ATOMIC))
807 /* SCTP-AUTH: Now that we've populate required fields in
808 * sctp_process_init, set up the assocaition shared keys as
809 * necessary so that we can potentially authenticate the ACK
811 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
815 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
816 sctp_association_free(new_asoc);
817 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
820 repl = sctp_make_cookie_ack(new_asoc, chunk);
824 /* RFC 2960 5.1 Normal Establishment of an Association
826 * D) IMPLEMENTATION NOTE: An implementation may choose to
827 * send the Communication Up notification to the SCTP user
828 * upon reception of a valid COOKIE ECHO chunk.
830 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
831 new_asoc->c.sinit_num_ostreams,
832 new_asoc->c.sinit_max_instreams,
837 /* Sockets API Draft Section 5.3.1.6
838 * When a peer sends a Adaptation Layer Indication parameter , SCTP
839 * delivers this notification to inform the application that of the
840 * peers requested adaptation layer.
842 if (new_asoc->peer.adaptation_ind) {
843 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
849 if (!new_asoc->peer.auth_capable) {
850 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
857 /* Add all the state machine commands now since we've created
858 * everything. This way we don't introduce memory corruptions
859 * during side-effect processing and correclty count established
862 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
863 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
864 SCTP_STATE(SCTP_STATE_ESTABLISHED));
865 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
866 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
867 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
869 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
870 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
871 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
873 /* This will send the COOKIE ACK */
874 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
876 /* Queue the ASSOC_CHANGE event */
877 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
879 /* Send up the Adaptation Layer Indication event */
881 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
882 SCTP_ULPEVENT(ai_ev));
885 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
886 SCTP_ULPEVENT(auth_ev));
888 return SCTP_DISPOSITION_CONSUME;
891 sctp_ulpevent_free(ai_ev);
893 sctp_ulpevent_free(ev);
895 sctp_chunk_free(repl);
897 sctp_association_free(new_asoc);
899 return SCTP_DISPOSITION_NOMEM;
903 * Respond to a normal COOKIE ACK chunk.
904 * We are the side that is asking for an association.
906 * RFC 2960 5.1 Normal Establishment of an Association
908 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
909 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
910 * timer. It may also notify its ULP about the successful
911 * establishment of the association with a Communication Up
912 * notification (see Section 10).
916 * (endpoint, asoc, chunk)
919 * (asoc, reply_msg, msg_up, timers, counters)
921 * The return value is the disposition of the chunk.
923 enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
924 const struct sctp_endpoint *ep,
925 const struct sctp_association *asoc,
926 const union sctp_subtype type,
928 struct sctp_cmd_seq *commands)
930 struct sctp_chunk *chunk = arg;
931 struct sctp_ulpevent *ev;
933 if (!sctp_vtag_verify(chunk, asoc))
934 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
936 /* Verify that the chunk length for the COOKIE-ACK is OK.
937 * If we don't do this, any bundled chunks may be junked.
939 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
940 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
943 /* Reset init error count upon receipt of COOKIE-ACK,
944 * to avoid problems with the managemement of this
945 * counter in stale cookie situations when a transition back
946 * from the COOKIE-ECHOED state to the COOKIE-WAIT
947 * state is performed.
949 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
951 /* Set peer label for connection. */
952 security_inet_conn_established(ep->base.sk, chunk->skb);
954 /* RFC 2960 5.1 Normal Establishment of an Association
956 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
957 * from the COOKIE-ECHOED state to the ESTABLISHED state,
958 * stopping the T1-cookie timer.
960 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
961 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
962 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
963 SCTP_STATE(SCTP_STATE_ESTABLISHED));
964 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
965 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
966 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
967 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
968 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
969 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
971 /* It may also notify its ULP about the successful
972 * establishment of the association with a Communication Up
973 * notification (see Section 10).
975 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
976 0, asoc->c.sinit_num_ostreams,
977 asoc->c.sinit_max_instreams,
983 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
985 /* Sockets API Draft Section 5.3.1.6
986 * When a peer sends a Adaptation Layer Indication parameter , SCTP
987 * delivers this notification to inform the application that of the
988 * peers requested adaptation layer.
990 if (asoc->peer.adaptation_ind) {
991 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
995 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
999 if (!asoc->peer.auth_capable) {
1000 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
1004 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1008 return SCTP_DISPOSITION_CONSUME;
1010 return SCTP_DISPOSITION_NOMEM;
1013 /* Generate and sendout a heartbeat packet. */
1014 static enum sctp_disposition sctp_sf_heartbeat(
1015 const struct sctp_endpoint *ep,
1016 const struct sctp_association *asoc,
1017 const union sctp_subtype type,
1019 struct sctp_cmd_seq *commands)
1021 struct sctp_transport *transport = (struct sctp_transport *) arg;
1022 struct sctp_chunk *reply;
1024 /* Send a heartbeat to our peer. */
1025 reply = sctp_make_heartbeat(asoc, transport);
1027 return SCTP_DISPOSITION_NOMEM;
1029 /* Set rto_pending indicating that an RTT measurement
1030 * is started with this heartbeat chunk.
1032 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1033 SCTP_TRANSPORT(transport));
1035 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1036 return SCTP_DISPOSITION_CONSUME;
1039 /* Generate a HEARTBEAT packet on the given transport. */
1040 enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1041 const struct sctp_endpoint *ep,
1042 const struct sctp_association *asoc,
1043 const union sctp_subtype type,
1045 struct sctp_cmd_seq *commands)
1047 struct sctp_transport *transport = (struct sctp_transport *) arg;
1049 if (asoc->overall_error_count >= asoc->max_retrans) {
1050 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1051 SCTP_ERROR(ETIMEDOUT));
1052 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1053 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1054 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1055 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1056 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1057 return SCTP_DISPOSITION_DELETE_TCB;
1061 * The Sender-specific Heartbeat Info field should normally include
1062 * information about the sender's current time when this HEARTBEAT
1063 * chunk is sent and the destination transport address to which this
1064 * HEARTBEAT is sent (see Section 8.3).
1067 if (transport->param_flags & SPP_HB_ENABLE) {
1068 if (SCTP_DISPOSITION_NOMEM ==
1069 sctp_sf_heartbeat(ep, asoc, type, arg,
1071 return SCTP_DISPOSITION_NOMEM;
1073 /* Set transport error counter and association error counter
1074 * when sending heartbeat.
1076 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1077 SCTP_TRANSPORT(transport));
1079 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1080 SCTP_TRANSPORT(transport));
1081 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1082 SCTP_TRANSPORT(transport));
1084 return SCTP_DISPOSITION_CONSUME;
1087 /* resend asoc strreset_chunk. */
1088 enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1089 const struct sctp_endpoint *ep,
1090 const struct sctp_association *asoc,
1091 const union sctp_subtype type,
1093 struct sctp_cmd_seq *commands)
1095 struct sctp_transport *transport = arg;
1097 if (asoc->overall_error_count >= asoc->max_retrans) {
1098 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1099 SCTP_ERROR(ETIMEDOUT));
1100 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1101 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1102 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1103 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1104 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1105 return SCTP_DISPOSITION_DELETE_TCB;
1108 sctp_chunk_hold(asoc->strreset_chunk);
1109 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1110 SCTP_CHUNK(asoc->strreset_chunk));
1111 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1113 return SCTP_DISPOSITION_CONSUME;
1117 * Process an heartbeat request.
1119 * Section: 8.3 Path Heartbeat
1120 * The receiver of the HEARTBEAT should immediately respond with a
1121 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1122 * from the received HEARTBEAT chunk.
1124 * Verification Tag: 8.5 Verification Tag [Normal verification]
1125 * When receiving an SCTP packet, the endpoint MUST ensure that the
1126 * value in the Verification Tag field of the received SCTP packet
1127 * matches its own Tag. If the received Verification Tag value does not
1128 * match the receiver's own tag value, the receiver shall silently
1129 * discard the packet and shall not process it any further except for
1130 * those cases listed in Section 8.5.1 below.
1133 * (endpoint, asoc, chunk)
1136 * (asoc, reply_msg, msg_up, timers, counters)
1138 * The return value is the disposition of the chunk.
1140 enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1141 const struct sctp_endpoint *ep,
1142 const struct sctp_association *asoc,
1143 const union sctp_subtype type,
1144 void *arg, struct sctp_cmd_seq *commands)
1146 struct sctp_paramhdr *param_hdr;
1147 struct sctp_chunk *chunk = arg;
1148 struct sctp_chunk *reply;
1151 if (!sctp_vtag_verify(chunk, asoc))
1152 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1154 /* Make sure that the HEARTBEAT chunk has a valid length. */
1155 if (!sctp_chunk_length_valid(chunk,
1156 sizeof(struct sctp_heartbeat_chunk)))
1157 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1160 /* 8.3 The receiver of the HEARTBEAT should immediately
1161 * respond with a HEARTBEAT ACK that contains the Heartbeat
1162 * Information field copied from the received HEARTBEAT chunk.
1164 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1165 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1166 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1168 if (ntohs(param_hdr->length) > paylen)
1169 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1170 param_hdr, commands);
1172 if (!pskb_pull(chunk->skb, paylen))
1175 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1179 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1180 return SCTP_DISPOSITION_CONSUME;
1183 return SCTP_DISPOSITION_NOMEM;
1187 * Process the returning HEARTBEAT ACK.
1189 * Section: 8.3 Path Heartbeat
1190 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1191 * should clear the error counter of the destination transport
1192 * address to which the HEARTBEAT was sent, and mark the destination
1193 * transport address as active if it is not so marked. The endpoint may
1194 * optionally report to the upper layer when an inactive destination
1195 * address is marked as active due to the reception of the latest
1196 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1197 * clear the association overall error count as well (as defined
1200 * The receiver of the HEARTBEAT ACK should also perform an RTT
1201 * measurement for that destination transport address using the time
1202 * value carried in the HEARTBEAT ACK chunk.
1204 * Verification Tag: 8.5 Verification Tag [Normal verification]
1207 * (endpoint, asoc, chunk)
1210 * (asoc, reply_msg, msg_up, timers, counters)
1212 * The return value is the disposition of the chunk.
1214 enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1215 const struct sctp_endpoint *ep,
1216 const struct sctp_association *asoc,
1217 const union sctp_subtype type,
1219 struct sctp_cmd_seq *commands)
1221 struct sctp_sender_hb_info *hbinfo;
1222 struct sctp_chunk *chunk = arg;
1223 struct sctp_transport *link;
1224 unsigned long max_interval;
1225 union sctp_addr from_addr;
1227 if (!sctp_vtag_verify(chunk, asoc))
1228 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1230 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1231 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1233 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1236 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1237 /* Make sure that the length of the parameter is what we expect */
1238 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1239 return SCTP_DISPOSITION_DISCARD;
1241 from_addr = hbinfo->daddr;
1242 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1244 /* This should never happen, but lets log it if so. */
1245 if (unlikely(!link)) {
1246 if (from_addr.sa.sa_family == AF_INET6) {
1247 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1250 &from_addr.v6.sin6_addr);
1252 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1255 &from_addr.v4.sin_addr.s_addr);
1257 return SCTP_DISPOSITION_DISCARD;
1260 /* Validate the 64-bit random nonce. */
1261 if (hbinfo->hb_nonce != link->hb_nonce)
1262 return SCTP_DISPOSITION_DISCARD;
1264 max_interval = link->hbinterval + link->rto;
1266 /* Check if the timestamp looks valid. */
1267 if (time_after(hbinfo->sent_at, jiffies) ||
1268 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1269 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1270 "for transport:%p\n", __func__, link);
1272 return SCTP_DISPOSITION_DISCARD;
1275 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1276 * the HEARTBEAT should clear the error counter of the
1277 * destination transport address to which the HEARTBEAT was
1278 * sent and mark the destination transport address as active if
1279 * it is not so marked.
1281 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1283 return SCTP_DISPOSITION_CONSUME;
1286 /* Helper function to send out an abort for the restart
1289 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1290 struct sctp_chunk *init,
1291 struct sctp_cmd_seq *commands)
1293 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1294 union sctp_addr_param *addrparm;
1295 struct sctp_errhdr *errhdr;
1296 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1297 struct sctp_endpoint *ep;
1298 struct sctp_packet *pkt;
1301 /* Build the error on the stack. We are way to malloc crazy
1302 * throughout the code today.
1304 errhdr = (struct sctp_errhdr *)buffer;
1305 addrparm = (union sctp_addr_param *)errhdr->variable;
1307 /* Copy into a parm format. */
1308 len = af->to_addr_param(ssa, addrparm);
1309 len += sizeof(*errhdr);
1311 errhdr->cause = SCTP_ERROR_RESTART;
1312 errhdr->length = htons(len);
1314 /* Assign to the control socket. */
1315 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1317 /* Association is NULL since this may be a restart attack and we
1318 * want to send back the attacker's vtag.
1320 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1324 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1326 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1328 /* Discard the rest of the inbound packet. */
1329 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1332 /* Even if there is no memory, treat as a failure so
1333 * the packet will get dropped.
1338 static bool list_has_sctp_addr(const struct list_head *list,
1339 union sctp_addr *ipaddr)
1341 struct sctp_transport *addr;
1343 list_for_each_entry(addr, list, transports) {
1344 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1350 /* A restart is occurring, check to make sure no new addresses
1351 * are being added as we may be under a takeover attack.
1353 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1354 const struct sctp_association *asoc,
1355 struct sctp_chunk *init,
1356 struct sctp_cmd_seq *commands)
1358 struct net *net = sock_net(new_asoc->base.sk);
1359 struct sctp_transport *new_addr;
1362 /* Implementor's Guide - Section 5.2.2
1364 * Before responding the endpoint MUST check to see if the
1365 * unexpected INIT adds new addresses to the association. If new
1366 * addresses are added to the association, the endpoint MUST respond
1370 /* Search through all current addresses and make sure
1371 * we aren't adding any new ones.
1373 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1375 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1376 &new_addr->ipaddr)) {
1377 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1384 /* Return success if all addresses were found. */
1388 /* Populate the verification/tie tags based on overlapping INIT
1391 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1393 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1394 const struct sctp_association *asoc)
1396 switch (asoc->state) {
1398 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1400 case SCTP_STATE_COOKIE_WAIT:
1401 new_asoc->c.my_vtag = asoc->c.my_vtag;
1402 new_asoc->c.my_ttag = asoc->c.my_vtag;
1403 new_asoc->c.peer_ttag = 0;
1406 case SCTP_STATE_COOKIE_ECHOED:
1407 new_asoc->c.my_vtag = asoc->c.my_vtag;
1408 new_asoc->c.my_ttag = asoc->c.my_vtag;
1409 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1412 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1413 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1416 new_asoc->c.my_ttag = asoc->c.my_vtag;
1417 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1421 /* Other parameters for the endpoint SHOULD be copied from the
1422 * existing parameters of the association (e.g. number of
1423 * outbound streams) into the INIT ACK and cookie.
1425 new_asoc->rwnd = asoc->rwnd;
1426 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1427 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1428 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1432 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1435 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1437 * Returns value representing action to be taken. These action values
1438 * correspond to Action/Description values in RFC 2960, Table 2.
1440 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1441 const struct sctp_association *asoc)
1443 /* In this case, the peer may have restarted. */
1444 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1445 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1446 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1447 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1450 /* Collision case B. */
1451 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1452 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1453 (0 == asoc->c.peer_vtag))) {
1457 /* Collision case D. */
1458 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1459 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1462 /* Collision case C. */
1463 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1464 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1465 (0 == new_asoc->c.my_ttag) &&
1466 (0 == new_asoc->c.peer_ttag))
1469 /* No match to any of the special cases; discard this packet. */
1473 /* Common helper routine for both duplicate and simulataneous INIT
1476 static enum sctp_disposition sctp_sf_do_unexpected_init(
1478 const struct sctp_endpoint *ep,
1479 const struct sctp_association *asoc,
1480 const union sctp_subtype type,
1482 struct sctp_cmd_seq *commands)
1484 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1485 struct sctp_unrecognized_param *unk_param;
1486 struct sctp_association *new_asoc;
1487 enum sctp_disposition retval;
1488 struct sctp_packet *packet;
1491 /* Update socket peer label if first association. */
1492 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
1494 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1497 * An endpoint MUST NOT bundle INIT, INIT ACK or
1498 * SHUTDOWN COMPLETE with any other chunks.
1501 * Furthermore, we require that the receiver of an INIT chunk MUST
1502 * enforce these rules by silently discarding an arriving packet
1503 * with an INIT chunk that is bundled with other chunks.
1505 if (!chunk->singleton)
1506 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1508 /* Make sure that the INIT chunk has a valid length. */
1509 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1510 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1512 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1515 if (chunk->sctp_hdr->vtag != 0)
1516 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1518 /* Grab the INIT header. */
1519 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1521 /* Tag the variable length parameters. */
1522 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1524 /* Verify the INIT chunk before processing it. */
1526 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1527 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1529 /* This chunk contains fatal error. It is to be discarded.
1530 * Send an ABORT, with causes if there is any.
1533 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1534 (__u8 *)(err_chunk->chunk_hdr) +
1535 sizeof(struct sctp_chunkhdr),
1536 ntohs(err_chunk->chunk_hdr->length) -
1537 sizeof(struct sctp_chunkhdr));
1540 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1541 SCTP_PACKET(packet));
1542 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1543 retval = SCTP_DISPOSITION_CONSUME;
1545 retval = SCTP_DISPOSITION_NOMEM;
1549 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1555 * Other parameters for the endpoint SHOULD be copied from the
1556 * existing parameters of the association (e.g. number of
1557 * outbound streams) into the INIT ACK and cookie.
1558 * FIXME: We are copying parameters from the endpoint not the
1561 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1565 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1566 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1569 /* In the outbound INIT ACK the endpoint MUST copy its current
1570 * Verification Tag and Peers Verification tag into a reserved
1571 * place (local tie-tag and per tie-tag) within the state cookie.
1573 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1574 (struct sctp_init_chunk *)chunk->chunk_hdr,
1578 /* Make sure no new addresses are being added during the
1579 * restart. Do not do this check for COOKIE-WAIT state,
1580 * since there are no peer addresses to check against.
1581 * Upon return an ABORT will have been sent if needed.
1583 if (!sctp_state(asoc, COOKIE_WAIT)) {
1584 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1586 retval = SCTP_DISPOSITION_CONSUME;
1591 sctp_tietags_populate(new_asoc, asoc);
1593 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1595 /* If there are errors need to be reported for unknown parameters,
1596 * make sure to reserve enough room in the INIT ACK for them.
1600 len = ntohs(err_chunk->chunk_hdr->length) -
1601 sizeof(struct sctp_chunkhdr);
1604 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1608 /* If there are errors need to be reported for unknown parameters,
1609 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1613 /* Get the "Unrecognized parameter" parameter(s) out of the
1614 * ERROR chunk generated by sctp_verify_init(). Since the
1615 * error cause code for "unknown parameter" and the
1616 * "Unrecognized parameter" type is the same, we can
1617 * construct the parameters in INIT ACK by copying the
1618 * ERROR causes over.
1620 unk_param = (struct sctp_unrecognized_param *)
1621 ((__u8 *)(err_chunk->chunk_hdr) +
1622 sizeof(struct sctp_chunkhdr));
1623 /* Replace the cause code with the "Unrecognized parameter"
1626 sctp_addto_chunk(repl, len, unk_param);
1629 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1630 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1633 * Note: After sending out INIT ACK with the State Cookie parameter,
1634 * "Z" MUST NOT allocate any resources for this new association.
1635 * Otherwise, "Z" will be vulnerable to resource attacks.
1637 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1638 retval = SCTP_DISPOSITION_CONSUME;
1643 retval = SCTP_DISPOSITION_NOMEM;
1646 sctp_association_free(new_asoc);
1649 sctp_chunk_free(err_chunk);
1654 * Handle simultaneous INIT.
1655 * This means we started an INIT and then we got an INIT request from
1658 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1659 * This usually indicates an initialization collision, i.e., each
1660 * endpoint is attempting, at about the same time, to establish an
1661 * association with the other endpoint.
1663 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1664 * endpoint MUST respond with an INIT ACK using the same parameters it
1665 * sent in its original INIT chunk (including its Verification Tag,
1666 * unchanged). These original parameters are combined with those from the
1667 * newly received INIT chunk. The endpoint shall also generate a State
1668 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1669 * INIT to calculate the State Cookie.
1671 * After that, the endpoint MUST NOT change its state, the T1-init
1672 * timer shall be left running and the corresponding TCB MUST NOT be
1673 * destroyed. The normal procedures for handling State Cookies when
1674 * a TCB exists will resolve the duplicate INITs to a single association.
1676 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1677 * its Tie-Tags with the Tag information of itself and its peer (see
1678 * section 5.2.2 for a description of the Tie-Tags).
1680 * Verification Tag: Not explicit, but an INIT can not have a valid
1681 * verification tag, so we skip the check.
1684 * (endpoint, asoc, chunk)
1687 * (asoc, reply_msg, msg_up, timers, counters)
1689 * The return value is the disposition of the chunk.
1691 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1693 const struct sctp_endpoint *ep,
1694 const struct sctp_association *asoc,
1695 const union sctp_subtype type,
1697 struct sctp_cmd_seq *commands)
1699 /* Call helper to do the real work for both simulataneous and
1700 * duplicate INIT chunk handling.
1702 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1706 * Handle duplicated INIT messages. These are usually delayed
1709 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1710 * COOKIE-ECHOED and COOKIE-WAIT
1712 * Unless otherwise stated, upon reception of an unexpected INIT for
1713 * this association, the endpoint shall generate an INIT ACK with a
1714 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1715 * current Verification Tag and peer's Verification Tag into a reserved
1716 * place within the state cookie. We shall refer to these locations as
1717 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1718 * containing this INIT ACK MUST carry a Verification Tag value equal to
1719 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1720 * MUST contain a new Initiation Tag (randomly generated see Section
1721 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1722 * existing parameters of the association (e.g. number of outbound
1723 * streams) into the INIT ACK and cookie.
1725 * After sending out the INIT ACK, the endpoint shall take no further
1726 * actions, i.e., the existing association, including its current state,
1727 * and the corresponding TCB MUST NOT be changed.
1729 * Note: Only when a TCB exists and the association is not in a COOKIE-
1730 * WAIT state are the Tie-Tags populated. For a normal association INIT
1731 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1732 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1733 * State Cookie are populated as specified in section 5.2.1.
1735 * Verification Tag: Not specified, but an INIT has no way of knowing
1736 * what the verification tag could be, so we ignore it.
1739 * (endpoint, asoc, chunk)
1742 * (asoc, reply_msg, msg_up, timers, counters)
1744 * The return value is the disposition of the chunk.
1746 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1748 const struct sctp_endpoint *ep,
1749 const struct sctp_association *asoc,
1750 const union sctp_subtype type,
1752 struct sctp_cmd_seq *commands)
1754 /* Call helper to do the real work for both simulataneous and
1755 * duplicate INIT chunk handling.
1757 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1762 * Unexpected INIT-ACK handler.
1765 * If an INIT ACK received by an endpoint in any state other than the
1766 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1767 * An unexpected INIT ACK usually indicates the processing of an old or
1768 * duplicated INIT chunk.
1770 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1772 const struct sctp_endpoint *ep,
1773 const struct sctp_association *asoc,
1774 const union sctp_subtype type,
1776 struct sctp_cmd_seq *commands)
1778 /* Per the above section, we'll discard the chunk if we have an
1779 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1781 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1782 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1784 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1787 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1790 * A) In this case, the peer may have restarted.
1792 static enum sctp_disposition sctp_sf_do_dupcook_a(
1794 const struct sctp_endpoint *ep,
1795 const struct sctp_association *asoc,
1796 struct sctp_chunk *chunk,
1797 struct sctp_cmd_seq *commands,
1798 struct sctp_association *new_asoc)
1800 struct sctp_init_chunk *peer_init;
1801 enum sctp_disposition disposition;
1802 struct sctp_ulpevent *ev;
1803 struct sctp_chunk *repl;
1804 struct sctp_chunk *err;
1806 /* new_asoc is a brand-new association, so these are not yet
1807 * side effects--it is safe to run them here.
1809 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1811 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1815 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1818 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1819 return SCTP_DISPOSITION_DISCARD;
1821 /* Make sure no new addresses are being added during the
1822 * restart. Though this is a pretty complicated attack
1823 * since you'd have to get inside the cookie.
1825 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1826 return SCTP_DISPOSITION_CONSUME;
1828 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1829 * the peer has restarted (Action A), it MUST NOT setup a new
1830 * association but instead resend the SHUTDOWN ACK and send an ERROR
1831 * chunk with a "Cookie Received while Shutting Down" error cause to
1834 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1835 disposition = __sctp_sf_do_9_2_reshutack(net, ep, asoc,
1836 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1838 if (SCTP_DISPOSITION_NOMEM == disposition)
1841 err = sctp_make_op_error(asoc, chunk,
1842 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1845 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1848 return SCTP_DISPOSITION_CONSUME;
1851 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1852 * data. Consider the optional choice of resending of this data.
1854 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1855 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1856 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1857 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1859 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1860 * and ASCONF-ACK cache.
1862 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1863 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1864 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1866 /* Update the content of current association. */
1867 if (sctp_assoc_update((struct sctp_association *)asoc, new_asoc)) {
1868 struct sctp_chunk *abort;
1870 abort = sctp_make_abort(asoc, NULL, sizeof(struct sctp_errhdr));
1872 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
1873 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
1875 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNABORTED));
1876 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1877 SCTP_PERR(SCTP_ERROR_RSRC_LOW));
1878 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1879 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1883 repl = sctp_make_cookie_ack(asoc, chunk);
1887 /* Report association restart to upper layer. */
1888 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1889 asoc->c.sinit_num_ostreams,
1890 asoc->c.sinit_max_instreams,
1895 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1896 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1897 sctp_state(asoc, SHUTDOWN_SENT)) &&
1898 (sctp_sstate(asoc->base.sk, CLOSING) ||
1899 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1900 /* If the socket has been closed by user, don't
1901 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1902 * bundled with COOKIE_ACK.
1904 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1905 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1906 SCTP_ST_CHUNK(0), repl,
1909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1910 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1911 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1913 return SCTP_DISPOSITION_CONSUME;
1916 sctp_chunk_free(repl);
1918 return SCTP_DISPOSITION_NOMEM;
1921 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1924 * B) In this case, both sides may be attempting to start an association
1925 * at about the same time but the peer endpoint started its INIT
1926 * after responding to the local endpoint's INIT
1928 /* This case represents an initialization collision. */
1929 static enum sctp_disposition sctp_sf_do_dupcook_b(
1931 const struct sctp_endpoint *ep,
1932 const struct sctp_association *asoc,
1933 struct sctp_chunk *chunk,
1934 struct sctp_cmd_seq *commands,
1935 struct sctp_association *new_asoc)
1937 struct sctp_init_chunk *peer_init;
1938 struct sctp_chunk *repl;
1940 /* new_asoc is a brand-new association, so these are not yet
1941 * side effects--it is safe to run them here.
1943 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1944 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1948 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1951 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1952 return SCTP_DISPOSITION_DISCARD;
1954 /* Update the content of current association. */
1955 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1956 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1957 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1958 if (asoc->state < SCTP_STATE_ESTABLISHED)
1959 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1960 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1962 repl = sctp_make_cookie_ack(new_asoc, chunk);
1966 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1968 /* RFC 2960 5.1 Normal Establishment of an Association
1970 * D) IMPLEMENTATION NOTE: An implementation may choose to
1971 * send the Communication Up notification to the SCTP user
1972 * upon reception of a valid COOKIE ECHO chunk.
1974 * Sadly, this needs to be implemented as a side-effect, because
1975 * we are not guaranteed to have set the association id of the real
1976 * association and so these notifications need to be delayed until
1977 * the association id is allocated.
1980 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1982 /* Sockets API Draft Section 5.3.1.6
1983 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1984 * delivers this notification to inform the application that of the
1985 * peers requested adaptation layer.
1987 * This also needs to be done as a side effect for the same reason as
1990 if (asoc->peer.adaptation_ind)
1991 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1993 if (!asoc->peer.auth_capable)
1994 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1996 return SCTP_DISPOSITION_CONSUME;
1999 return SCTP_DISPOSITION_NOMEM;
2002 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
2005 * C) In this case, the local endpoint's cookie has arrived late.
2006 * Before it arrived, the local endpoint sent an INIT and received an
2007 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
2008 * but a new tag of its own.
2010 /* This case represents an initialization collision. */
2011 static enum sctp_disposition sctp_sf_do_dupcook_c(
2013 const struct sctp_endpoint *ep,
2014 const struct sctp_association *asoc,
2015 struct sctp_chunk *chunk,
2016 struct sctp_cmd_seq *commands,
2017 struct sctp_association *new_asoc)
2019 /* The cookie should be silently discarded.
2020 * The endpoint SHOULD NOT change states and should leave
2021 * any timers running.
2023 return SCTP_DISPOSITION_DISCARD;
2026 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
2030 * D) When both local and remote tags match the endpoint should always
2031 * enter the ESTABLISHED state, if it has not already done so.
2033 /* This case represents an initialization collision. */
2034 static enum sctp_disposition sctp_sf_do_dupcook_d(
2036 const struct sctp_endpoint *ep,
2037 const struct sctp_association *asoc,
2038 struct sctp_chunk *chunk,
2039 struct sctp_cmd_seq *commands,
2040 struct sctp_association *new_asoc)
2042 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
2043 struct sctp_chunk *repl;
2045 /* Clarification from Implementor's Guide:
2046 * D) When both local and remote tags match the endpoint should
2047 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2048 * It should stop any cookie timer that may be running and send
2052 if (!sctp_auth_chunk_verify(net, chunk, asoc))
2053 return SCTP_DISPOSITION_DISCARD;
2055 /* Don't accidentally move back into established state. */
2056 if (asoc->state < SCTP_STATE_ESTABLISHED) {
2057 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2058 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2059 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2060 SCTP_STATE(SCTP_STATE_ESTABLISHED));
2061 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2062 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2065 /* RFC 2960 5.1 Normal Establishment of an Association
2067 * D) IMPLEMENTATION NOTE: An implementation may choose
2068 * to send the Communication Up notification to the
2069 * SCTP user upon reception of a valid COOKIE
2072 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2074 asoc->c.sinit_num_ostreams,
2075 asoc->c.sinit_max_instreams,
2080 /* Sockets API Draft Section 5.3.1.6
2081 * When a peer sends a Adaptation Layer Indication parameter,
2082 * SCTP delivers this notification to inform the application
2083 * that of the peers requested adaptation layer.
2085 if (asoc->peer.adaptation_ind) {
2086 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2093 if (!asoc->peer.auth_capable) {
2094 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2102 repl = sctp_make_cookie_ack(asoc, chunk);
2106 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2109 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2112 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2113 SCTP_ULPEVENT(ai_ev));
2115 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2116 SCTP_ULPEVENT(auth_ev));
2118 return SCTP_DISPOSITION_CONSUME;
2122 sctp_ulpevent_free(auth_ev);
2124 sctp_ulpevent_free(ai_ev);
2126 sctp_ulpevent_free(ev);
2127 return SCTP_DISPOSITION_NOMEM;
2131 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2132 * chunk was retransmitted and then delayed in the network.
2134 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2136 * Verification Tag: None. Do cookie validation.
2139 * (endpoint, asoc, chunk)
2142 * (asoc, reply_msg, msg_up, timers, counters)
2144 * The return value is the disposition of the chunk.
2146 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2148 const struct sctp_endpoint *ep,
2149 const struct sctp_association *asoc,
2150 const union sctp_subtype type,
2152 struct sctp_cmd_seq *commands)
2154 struct sctp_association *new_asoc;
2155 struct sctp_chunk *chunk = arg;
2156 enum sctp_disposition retval;
2157 struct sctp_chunk *err_chk_p;
2161 /* Make sure that the chunk has a valid length from the protocol
2162 * perspective. In this case check to make sure we have at least
2163 * enough for the chunk header. Cookie length verification is
2166 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr))) {
2167 if (!sctp_vtag_verify(chunk, asoc))
2169 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg, commands);
2172 /* "Decode" the chunk. We have no optional parameters so we
2173 * are in good shape.
2175 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2176 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2177 sizeof(struct sctp_chunkhdr)))
2180 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2181 * of a duplicate COOKIE ECHO match the Verification Tags of the
2182 * current association, consider the State Cookie valid even if
2183 * the lifespan is exceeded.
2185 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2189 * If the re-build failed, what is the proper error path
2192 * [We should abort the association. --piggy]
2195 /* FIXME: Several errors are possible. A bad cookie should
2196 * be silently discarded, but think about logging it too.
2199 case -SCTP_IERROR_NOMEM:
2202 case -SCTP_IERROR_STALE_COOKIE:
2203 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2205 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2206 case -SCTP_IERROR_BAD_SIG:
2208 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2212 /* Update socket peer label if first association. */
2213 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
2215 sctp_association_free(new_asoc);
2216 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2219 /* Set temp so that it won't be added into hashtable */
2222 /* Compare the tie_tag in cookie with the verification tag of
2223 * current association.
2225 action = sctp_tietags_compare(new_asoc, asoc);
2228 case 'A': /* Association restart. */
2229 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2233 case 'B': /* Collision case B. */
2234 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2238 case 'C': /* Collision case C. */
2239 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2243 case 'D': /* Collision case D. */
2244 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2248 default: /* Discard packet for all others. */
2249 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2253 /* Delete the tempory new association. */
2254 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2255 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2257 /* Restore association pointer to provide SCTP command interpeter
2258 * with a valid context in case it needs to manipulate
2260 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2261 SCTP_ASOC((struct sctp_association *)asoc));
2266 return SCTP_DISPOSITION_NOMEM;
2270 * Process an ABORT. (SHUTDOWN-PENDING state)
2272 * See sctp_sf_do_9_1_abort().
2274 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2276 const struct sctp_endpoint *ep,
2277 const struct sctp_association *asoc,
2278 const union sctp_subtype type,
2280 struct sctp_cmd_seq *commands)
2282 struct sctp_chunk *chunk = arg;
2284 if (!sctp_vtag_verify_either(chunk, asoc))
2285 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2287 /* Make sure that the ABORT chunk has a valid length.
2288 * Since this is an ABORT chunk, we have to discard it
2289 * because of the following text:
2290 * RFC 2960, Section 3.3.7
2291 * If an endpoint receives an ABORT with a format error or for an
2292 * association that doesn't exist, it MUST silently discard it.
2293 * Because the length is "invalid", we can't really discard just
2294 * as we do not know its true length. So, to be safe, discard the
2297 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2298 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2300 /* ADD-IP: Special case for ABORT chunks
2301 * F4) One special consideration is that ABORT Chunks arriving
2302 * destined to the IP address being deleted MUST be
2303 * ignored (see Section 5.3.1 for further details).
2305 if (SCTP_ADDR_DEL ==
2306 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2307 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2309 if (!sctp_err_chunk_valid(chunk))
2310 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2312 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2316 * Process an ABORT. (SHUTDOWN-SENT state)
2318 * See sctp_sf_do_9_1_abort().
2320 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2322 const struct sctp_endpoint *ep,
2323 const struct sctp_association *asoc,
2324 const union sctp_subtype type,
2326 struct sctp_cmd_seq *commands)
2328 struct sctp_chunk *chunk = arg;
2330 if (!sctp_vtag_verify_either(chunk, asoc))
2331 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2333 /* Make sure that the ABORT chunk has a valid length.
2334 * Since this is an ABORT chunk, we have to discard it
2335 * because of the following text:
2336 * RFC 2960, Section 3.3.7
2337 * If an endpoint receives an ABORT with a format error or for an
2338 * association that doesn't exist, it MUST silently discard it.
2339 * Because the length is "invalid", we can't really discard just
2340 * as we do not know its true length. So, to be safe, discard the
2343 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2344 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2346 /* ADD-IP: Special case for ABORT chunks
2347 * F4) One special consideration is that ABORT Chunks arriving
2348 * destined to the IP address being deleted MUST be
2349 * ignored (see Section 5.3.1 for further details).
2351 if (SCTP_ADDR_DEL ==
2352 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2353 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2355 if (!sctp_err_chunk_valid(chunk))
2356 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2358 /* Stop the T2-shutdown timer. */
2359 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2360 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2362 /* Stop the T5-shutdown guard timer. */
2363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2364 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2366 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2370 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2372 * See sctp_sf_do_9_1_abort().
2374 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2376 const struct sctp_endpoint *ep,
2377 const struct sctp_association *asoc,
2378 const union sctp_subtype type,
2380 struct sctp_cmd_seq *commands)
2382 /* The same T2 timer, so we should be able to use
2383 * common function with the SHUTDOWN-SENT state.
2385 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2389 * Handle an Error received in COOKIE_ECHOED state.
2391 * Only handle the error type of stale COOKIE Error, the other errors will
2395 * (endpoint, asoc, chunk)
2398 * (asoc, reply_msg, msg_up, timers, counters)
2400 * The return value is the disposition of the chunk.
2402 enum sctp_disposition sctp_sf_cookie_echoed_err(
2404 const struct sctp_endpoint *ep,
2405 const struct sctp_association *asoc,
2406 const union sctp_subtype type,
2408 struct sctp_cmd_seq *commands)
2410 struct sctp_chunk *chunk = arg;
2411 struct sctp_errhdr *err;
2413 if (!sctp_vtag_verify(chunk, asoc))
2414 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2416 /* Make sure that the ERROR chunk has a valid length.
2417 * The parameter walking depends on this as well.
2419 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2420 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2423 /* Process the error here */
2424 /* FUTURE FIXME: When PR-SCTP related and other optional
2425 * parms are emitted, this will have to change to handle multiple
2428 sctp_walk_errors(err, chunk->chunk_hdr) {
2429 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2430 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2434 /* It is possible to have malformed error causes, and that
2435 * will cause us to end the walk early. However, since
2436 * we are discarding the packet, there should be no adverse
2439 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2443 * Handle a Stale COOKIE Error
2445 * Section: 5.2.6 Handle Stale COOKIE Error
2446 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2447 * one of the following three alternatives.
2449 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2450 * Preservative parameter requesting an extension to the lifetime of
2451 * the State Cookie. When calculating the time extension, an
2452 * implementation SHOULD use the RTT information measured based on the
2453 * previous COOKIE ECHO / ERROR exchange, and should add no more
2454 * than 1 second beyond the measured RTT, due to long State Cookie
2455 * lifetimes making the endpoint more subject to a replay attack.
2457 * Verification Tag: Not explicit, but safe to ignore.
2460 * (endpoint, asoc, chunk)
2463 * (asoc, reply_msg, msg_up, timers, counters)
2465 * The return value is the disposition of the chunk.
2467 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2469 const struct sctp_endpoint *ep,
2470 const struct sctp_association *asoc,
2471 const union sctp_subtype type,
2473 struct sctp_cmd_seq *commands)
2475 int attempts = asoc->init_err_counter + 1;
2476 struct sctp_chunk *chunk = arg, *reply;
2477 struct sctp_cookie_preserve_param bht;
2478 struct sctp_bind_addr *bp;
2479 struct sctp_errhdr *err;
2482 if (attempts > asoc->max_init_attempts) {
2483 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2484 SCTP_ERROR(ETIMEDOUT));
2485 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2486 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2487 return SCTP_DISPOSITION_DELETE_TCB;
2490 err = (struct sctp_errhdr *)(chunk->skb->data);
2492 /* When calculating the time extension, an implementation
2493 * SHOULD use the RTT information measured based on the
2494 * previous COOKIE ECHO / ERROR exchange, and should add no
2495 * more than 1 second beyond the measured RTT, due to long
2496 * State Cookie lifetimes making the endpoint more subject to
2498 * Measure of Staleness's unit is usec. (1/1000000 sec)
2499 * Suggested Cookie Life-span Increment's unit is msec.
2501 * In general, if you use the suggested cookie life, the value
2502 * found in the field of measure of staleness should be doubled
2503 * to give ample time to retransmit the new cookie and thus
2504 * yield a higher probability of success on the reattempt.
2506 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2507 stale = (stale * 2) / 1000;
2509 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2510 bht.param_hdr.length = htons(sizeof(bht));
2511 bht.lifespan_increment = htonl(stale);
2513 /* Build that new INIT chunk. */
2514 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2515 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2519 sctp_addto_chunk(reply, sizeof(bht), &bht);
2521 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2522 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2524 /* Stop pending T3-rtx and heartbeat timers */
2525 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2526 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2528 /* Delete non-primary peer ip addresses since we are transitioning
2529 * back to the COOKIE-WAIT state
2531 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2533 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2536 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2537 SCTP_TRANSPORT(asoc->peer.primary_path));
2539 /* Cast away the const modifier, as we want to just
2540 * rerun it through as a sideffect.
2542 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2544 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2545 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2546 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2547 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2548 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2549 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2551 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2553 return SCTP_DISPOSITION_CONSUME;
2556 return SCTP_DISPOSITION_NOMEM;
2563 * After checking the Verification Tag, the receiving endpoint shall
2564 * remove the association from its record, and shall report the
2565 * termination to its upper layer.
2567 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2568 * B) Rules for packet carrying ABORT:
2570 * - The endpoint shall always fill in the Verification Tag field of the
2571 * outbound packet with the destination endpoint's tag value if it
2574 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2575 * MUST follow the procedure described in Section 8.4.
2577 * - The receiver MUST accept the packet if the Verification Tag
2578 * matches either its own tag, OR the tag of its peer. Otherwise, the
2579 * receiver MUST silently discard the packet and take no further
2583 * (endpoint, asoc, chunk)
2586 * (asoc, reply_msg, msg_up, timers, counters)
2588 * The return value is the disposition of the chunk.
2590 enum sctp_disposition sctp_sf_do_9_1_abort(
2592 const struct sctp_endpoint *ep,
2593 const struct sctp_association *asoc,
2594 const union sctp_subtype type,
2596 struct sctp_cmd_seq *commands)
2598 struct sctp_chunk *chunk = arg;
2600 if (!sctp_vtag_verify_either(chunk, asoc))
2601 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2603 /* Make sure that the ABORT chunk has a valid length.
2604 * Since this is an ABORT chunk, we have to discard it
2605 * because of the following text:
2606 * RFC 2960, Section 3.3.7
2607 * If an endpoint receives an ABORT with a format error or for an
2608 * association that doesn't exist, it MUST silently discard it.
2609 * Because the length is "invalid", we can't really discard just
2610 * as we do not know its true length. So, to be safe, discard the
2613 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2614 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2616 /* ADD-IP: Special case for ABORT chunks
2617 * F4) One special consideration is that ABORT Chunks arriving
2618 * destined to the IP address being deleted MUST be
2619 * ignored (see Section 5.3.1 for further details).
2621 if (SCTP_ADDR_DEL ==
2622 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2623 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2625 if (!sctp_err_chunk_valid(chunk))
2626 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2628 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2631 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2633 const struct sctp_endpoint *ep,
2634 const struct sctp_association *asoc,
2635 const union sctp_subtype type,
2637 struct sctp_cmd_seq *commands)
2639 __be16 error = SCTP_ERROR_NO_ERROR;
2640 struct sctp_chunk *chunk = arg;
2643 /* See if we have an error cause code in the chunk. */
2644 len = ntohs(chunk->chunk_hdr->length);
2645 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2646 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2648 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2649 /* ASSOC_FAILED will DELETE_TCB. */
2650 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2651 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2652 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2654 return SCTP_DISPOSITION_ABORT;
2658 * Process an ABORT. (COOKIE-WAIT state)
2660 * See sctp_sf_do_9_1_abort() above.
2662 enum sctp_disposition sctp_sf_cookie_wait_abort(
2664 const struct sctp_endpoint *ep,
2665 const struct sctp_association *asoc,
2666 const union sctp_subtype type,
2668 struct sctp_cmd_seq *commands)
2670 __be16 error = SCTP_ERROR_NO_ERROR;
2671 struct sctp_chunk *chunk = arg;
2674 if (!sctp_vtag_verify_either(chunk, asoc))
2675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2677 /* Make sure that the ABORT chunk has a valid length.
2678 * Since this is an ABORT chunk, we have to discard it
2679 * because of the following text:
2680 * RFC 2960, Section 3.3.7
2681 * If an endpoint receives an ABORT with a format error or for an
2682 * association that doesn't exist, it MUST silently discard it.
2683 * Because the length is "invalid", we can't really discard just
2684 * as we do not know its true length. So, to be safe, discard the
2687 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2688 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2690 /* See if we have an error cause code in the chunk. */
2691 len = ntohs(chunk->chunk_hdr->length);
2692 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2693 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2695 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2700 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2702 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2704 const struct sctp_endpoint *ep,
2705 const struct sctp_association *asoc,
2706 const union sctp_subtype type,
2708 struct sctp_cmd_seq *commands)
2710 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2712 (struct sctp_transport *)arg);
2716 * Process an ABORT. (COOKIE-ECHOED state)
2718 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2720 const struct sctp_endpoint *ep,
2721 const struct sctp_association *asoc,
2722 const union sctp_subtype type,
2724 struct sctp_cmd_seq *commands)
2726 /* There is a single T1 timer, so we should be able to use
2727 * common function with the COOKIE-WAIT state.
2729 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2733 * Stop T1 timer and abort association with "INIT failed".
2735 * This is common code called by several sctp_sf_*_abort() functions above.
2737 static enum sctp_disposition sctp_stop_t1_and_abort(
2739 struct sctp_cmd_seq *commands,
2740 __be16 error, int sk_err,
2741 const struct sctp_association *asoc,
2742 struct sctp_transport *transport)
2744 pr_debug("%s: ABORT received (INIT)\n", __func__);
2746 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2747 SCTP_STATE(SCTP_STATE_CLOSED));
2748 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2749 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2750 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2751 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2752 /* CMD_INIT_FAILED will DELETE_TCB. */
2753 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2756 return SCTP_DISPOSITION_ABORT;
2760 * sctp_sf_do_9_2_shut
2763 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2764 * - enter the SHUTDOWN-RECEIVED state,
2766 * - stop accepting new data from its SCTP user
2768 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2769 * that all its outstanding DATA chunks have been received by the
2772 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2773 * send a SHUTDOWN in response to a ULP request. And should discard
2774 * subsequent SHUTDOWN chunks.
2776 * If there are still outstanding DATA chunks left, the SHUTDOWN
2777 * receiver shall continue to follow normal data transmission
2778 * procedures defined in Section 6 until all outstanding DATA chunks
2779 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2780 * new data from its SCTP user.
2782 * Verification Tag: 8.5 Verification Tag [Normal verification]
2785 * (endpoint, asoc, chunk)
2788 * (asoc, reply_msg, msg_up, timers, counters)
2790 * The return value is the disposition of the chunk.
2792 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2794 const struct sctp_endpoint *ep,
2795 const struct sctp_association *asoc,
2796 const union sctp_subtype type,
2798 struct sctp_cmd_seq *commands)
2800 enum sctp_disposition disposition;
2801 struct sctp_chunk *chunk = arg;
2802 struct sctp_shutdownhdr *sdh;
2803 struct sctp_ulpevent *ev;
2806 if (!sctp_vtag_verify(chunk, asoc))
2807 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2809 /* Make sure that the SHUTDOWN chunk has a valid length. */
2810 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2811 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2814 /* Convert the elaborate header. */
2815 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2816 skb_pull(chunk->skb, sizeof(*sdh));
2817 chunk->subh.shutdown_hdr = sdh;
2818 ctsn = ntohl(sdh->cum_tsn_ack);
2820 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2821 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2822 asoc->ctsn_ack_point);
2824 return SCTP_DISPOSITION_DISCARD;
2827 /* If Cumulative TSN Ack beyond the max tsn currently
2828 * send, terminating the association and respond to the
2829 * sender with an ABORT.
2831 if (!TSN_lt(ctsn, asoc->next_tsn))
2832 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2834 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2835 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2836 * inform the application that it should cease sending data.
2838 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2840 disposition = SCTP_DISPOSITION_NOMEM;
2843 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2845 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2846 * - enter the SHUTDOWN-RECEIVED state,
2847 * - stop accepting new data from its SCTP user
2849 * [This is implicit in the new state.]
2851 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2852 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2853 disposition = SCTP_DISPOSITION_CONSUME;
2855 if (sctp_outq_is_empty(&asoc->outqueue)) {
2856 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2860 if (SCTP_DISPOSITION_NOMEM == disposition)
2863 /* - verify, by checking the Cumulative TSN Ack field of the
2864 * chunk, that all its outstanding DATA chunks have been
2865 * received by the SHUTDOWN sender.
2867 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2868 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2875 * sctp_sf_do_9_2_shut_ctsn
2877 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2878 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2879 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2880 * MUST be processed.
2882 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2884 const struct sctp_endpoint *ep,
2885 const struct sctp_association *asoc,
2886 const union sctp_subtype type,
2888 struct sctp_cmd_seq *commands)
2890 struct sctp_chunk *chunk = arg;
2891 struct sctp_shutdownhdr *sdh;
2894 if (!sctp_vtag_verify(chunk, asoc))
2895 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2897 /* Make sure that the SHUTDOWN chunk has a valid length. */
2898 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2899 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2902 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2903 ctsn = ntohl(sdh->cum_tsn_ack);
2905 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2906 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2907 asoc->ctsn_ack_point);
2909 return SCTP_DISPOSITION_DISCARD;
2912 /* If Cumulative TSN Ack beyond the max tsn currently
2913 * send, terminating the association and respond to the
2914 * sender with an ABORT.
2916 if (!TSN_lt(ctsn, asoc->next_tsn))
2917 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2919 /* verify, by checking the Cumulative TSN Ack field of the
2920 * chunk, that all its outstanding DATA chunks have been
2921 * received by the SHUTDOWN sender.
2923 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2924 SCTP_BE32(sdh->cum_tsn_ack));
2926 return SCTP_DISPOSITION_CONSUME;
2930 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2931 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2932 * transport addresses (either in the IP addresses or in the INIT chunk)
2933 * that belong to this association, it should discard the INIT chunk and
2934 * retransmit the SHUTDOWN ACK chunk.
2936 static enum sctp_disposition
2937 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2938 const struct sctp_association *asoc,
2939 const union sctp_subtype type, void *arg,
2940 struct sctp_cmd_seq *commands)
2942 struct sctp_chunk *chunk = arg;
2943 struct sctp_chunk *reply;
2945 /* Make sure that the chunk has a valid length */
2946 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2947 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2950 /* Since we are not going to really process this INIT, there
2951 * is no point in verifying chunk boundries. Just generate
2954 reply = sctp_make_shutdown_ack(asoc, chunk);
2958 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2959 * the T2-SHUTDOWN timer.
2961 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2963 /* and restart the T2-shutdown timer. */
2964 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2965 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2967 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2969 return SCTP_DISPOSITION_CONSUME;
2971 return SCTP_DISPOSITION_NOMEM;
2974 enum sctp_disposition
2975 sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2976 const struct sctp_association *asoc,
2977 const union sctp_subtype type, void *arg,
2978 struct sctp_cmd_seq *commands)
2980 struct sctp_chunk *chunk = arg;
2982 if (!chunk->singleton)
2983 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2985 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
2986 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2988 if (chunk->sctp_hdr->vtag != 0)
2989 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
2991 return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
2995 * sctp_sf_do_ecn_cwr
2997 * Section: Appendix A: Explicit Congestion Notification
3001 * RFC 2481 details a specific bit for a sender to send in the header of
3002 * its next outbound TCP segment to indicate to its peer that it has
3003 * reduced its congestion window. This is termed the CWR bit. For
3004 * SCTP the same indication is made by including the CWR chunk.
3005 * This chunk contains one data element, i.e. the TSN number that
3006 * was sent in the ECNE chunk. This element represents the lowest
3007 * TSN number in the datagram that was originally marked with the
3010 * Verification Tag: 8.5 Verification Tag [Normal verification]
3012 * (endpoint, asoc, chunk)
3015 * (asoc, reply_msg, msg_up, timers, counters)
3017 * The return value is the disposition of the chunk.
3019 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
3020 const struct sctp_endpoint *ep,
3021 const struct sctp_association *asoc,
3022 const union sctp_subtype type,
3024 struct sctp_cmd_seq *commands)
3026 struct sctp_chunk *chunk = arg;
3027 struct sctp_cwrhdr *cwr;
3030 if (!sctp_vtag_verify(chunk, asoc))
3031 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3033 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3034 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3037 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
3038 skb_pull(chunk->skb, sizeof(*cwr));
3040 lowest_tsn = ntohl(cwr->lowest_tsn);
3042 /* Does this CWR ack the last sent congestion notification? */
3043 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
3044 /* Stop sending ECNE. */
3045 sctp_add_cmd_sf(commands,
3047 SCTP_U32(lowest_tsn));
3049 return SCTP_DISPOSITION_CONSUME;
3055 * Section: Appendix A: Explicit Congestion Notification
3059 * RFC 2481 details a specific bit for a receiver to send back in its
3060 * TCP acknowledgements to notify the sender of the Congestion
3061 * Experienced (CE) bit having arrived from the network. For SCTP this
3062 * same indication is made by including the ECNE chunk. This chunk
3063 * contains one data element, i.e. the lowest TSN associated with the IP
3064 * datagram marked with the CE bit.....
3066 * Verification Tag: 8.5 Verification Tag [Normal verification]
3068 * (endpoint, asoc, chunk)
3071 * (asoc, reply_msg, msg_up, timers, counters)
3073 * The return value is the disposition of the chunk.
3075 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3076 const struct sctp_endpoint *ep,
3077 const struct sctp_association *asoc,
3078 const union sctp_subtype type,
3079 void *arg, struct sctp_cmd_seq *commands)
3081 struct sctp_chunk *chunk = arg;
3082 struct sctp_ecnehdr *ecne;
3084 if (!sctp_vtag_verify(chunk, asoc))
3085 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3087 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3088 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3091 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3092 skb_pull(chunk->skb, sizeof(*ecne));
3094 /* If this is a newer ECNE than the last CWR packet we sent out */
3095 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3096 SCTP_U32(ntohl(ecne->lowest_tsn)));
3098 return SCTP_DISPOSITION_CONSUME;
3102 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3104 * The SCTP endpoint MUST always acknowledge the reception of each valid
3107 * The guidelines on delayed acknowledgement algorithm specified in
3108 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3109 * acknowledgement SHOULD be generated for at least every second packet
3110 * (not every second DATA chunk) received, and SHOULD be generated within
3111 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3112 * situations it may be beneficial for an SCTP transmitter to be more
3113 * conservative than the algorithms detailed in this document allow.
3114 * However, an SCTP transmitter MUST NOT be more aggressive than the
3115 * following algorithms allow.
3117 * A SCTP receiver MUST NOT generate more than one SACK for every
3118 * incoming packet, other than to update the offered window as the
3119 * receiving application consumes new data.
3121 * Verification Tag: 8.5 Verification Tag [Normal verification]
3124 * (endpoint, asoc, chunk)
3127 * (asoc, reply_msg, msg_up, timers, counters)
3129 * The return value is the disposition of the chunk.
3131 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3132 const struct sctp_endpoint *ep,
3133 const struct sctp_association *asoc,
3134 const union sctp_subtype type,
3136 struct sctp_cmd_seq *commands)
3138 union sctp_arg force = SCTP_NOFORCE();
3139 struct sctp_chunk *chunk = arg;
3142 if (!sctp_vtag_verify(chunk, asoc)) {
3143 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3145 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3148 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3149 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3152 error = sctp_eat_data(asoc, chunk, commands);
3154 case SCTP_IERROR_NO_ERROR:
3156 case SCTP_IERROR_HIGH_TSN:
3157 case SCTP_IERROR_BAD_STREAM:
3158 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3159 goto discard_noforce;
3160 case SCTP_IERROR_DUP_TSN:
3161 case SCTP_IERROR_IGNORE_TSN:
3162 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3164 case SCTP_IERROR_NO_DATA:
3165 return SCTP_DISPOSITION_ABORT;
3166 case SCTP_IERROR_PROTO_VIOLATION:
3167 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3168 (u8 *)chunk->subh.data_hdr,
3169 sctp_datahdr_len(&asoc->stream));
3174 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3175 force = SCTP_FORCE();
3177 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3178 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3179 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3182 /* If this is the last chunk in a packet, we need to count it
3183 * toward sack generation. Note that we need to SACK every
3184 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3185 * THEM. We elect to NOT generate SACK's if the chunk fails
3186 * the verification tag test.
3188 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3190 * The SCTP endpoint MUST always acknowledge the reception of
3191 * each valid DATA chunk.
3193 * The guidelines on delayed acknowledgement algorithm
3194 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3195 * Specifically, an acknowledgement SHOULD be generated for at
3196 * least every second packet (not every second DATA chunk)
3197 * received, and SHOULD be generated within 200 ms of the
3198 * arrival of any unacknowledged DATA chunk. In some
3199 * situations it may be beneficial for an SCTP transmitter to
3200 * be more conservative than the algorithms detailed in this
3201 * document allow. However, an SCTP transmitter MUST NOT be
3202 * more aggressive than the following algorithms allow.
3204 if (chunk->end_of_packet)
3205 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3207 return SCTP_DISPOSITION_CONSUME;
3210 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3212 * When a packet arrives with duplicate DATA chunk(s) and with
3213 * no new DATA chunk(s), the endpoint MUST immediately send a
3214 * SACK with no delay. If a packet arrives with duplicate
3215 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3216 * MAY immediately send a SACK. Normally receipt of duplicate
3217 * DATA chunks will occur when the original SACK chunk was lost
3218 * and the peer's RTO has expired. The duplicate TSN number(s)
3219 * SHOULD be reported in the SACK as duplicate.
3221 /* In our case, we split the MAY SACK advice up whether or not
3222 * the last chunk is a duplicate.'
3224 if (chunk->end_of_packet)
3225 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3226 return SCTP_DISPOSITION_DISCARD;
3229 if (chunk->end_of_packet)
3230 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3232 return SCTP_DISPOSITION_DISCARD;
3236 * sctp_sf_eat_data_fast_4_4
3239 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3240 * DATA chunks without delay.
3242 * Verification Tag: 8.5 Verification Tag [Normal verification]
3244 * (endpoint, asoc, chunk)
3247 * (asoc, reply_msg, msg_up, timers, counters)
3249 * The return value is the disposition of the chunk.
3251 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3253 const struct sctp_endpoint *ep,
3254 const struct sctp_association *asoc,
3255 const union sctp_subtype type,
3257 struct sctp_cmd_seq *commands)
3259 struct sctp_chunk *chunk = arg;
3262 if (!sctp_vtag_verify(chunk, asoc)) {
3263 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3265 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3268 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3269 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3272 error = sctp_eat_data(asoc, chunk, commands);
3274 case SCTP_IERROR_NO_ERROR:
3275 case SCTP_IERROR_HIGH_TSN:
3276 case SCTP_IERROR_DUP_TSN:
3277 case SCTP_IERROR_IGNORE_TSN:
3278 case SCTP_IERROR_BAD_STREAM:
3280 case SCTP_IERROR_NO_DATA:
3281 return SCTP_DISPOSITION_ABORT;
3282 case SCTP_IERROR_PROTO_VIOLATION:
3283 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3284 (u8 *)chunk->subh.data_hdr,
3285 sctp_datahdr_len(&asoc->stream));
3290 /* Go a head and force a SACK, since we are shutting down. */
3292 /* Implementor's Guide.
3294 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3295 * respond to each received packet containing one or more DATA chunk(s)
3296 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3298 if (chunk->end_of_packet) {
3299 /* We must delay the chunk creation since the cumulative
3300 * TSN has not been updated yet.
3302 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3303 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3304 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3305 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3308 return SCTP_DISPOSITION_CONSUME;
3312 * Section: 6.2 Processing a Received SACK
3313 * D) Any time a SACK arrives, the endpoint performs the following:
3315 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3316 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3317 * increasing, a SACK whose Cumulative TSN Ack is less than the
3318 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3320 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3321 * of bytes still outstanding after processing the Cumulative TSN Ack
3322 * and the Gap Ack Blocks.
3324 * iii) If the SACK is missing a TSN that was previously
3325 * acknowledged via a Gap Ack Block (e.g., the data receiver
3326 * reneged on the data), then mark the corresponding DATA chunk
3327 * as available for retransmit: Mark it as missing for fast
3328 * retransmit as described in Section 7.2.4 and if no retransmit
3329 * timer is running for the destination address to which the DATA
3330 * chunk was originally transmitted, then T3-rtx is started for
3331 * that destination address.
3333 * Verification Tag: 8.5 Verification Tag [Normal verification]
3336 * (endpoint, asoc, chunk)
3339 * (asoc, reply_msg, msg_up, timers, counters)
3341 * The return value is the disposition of the chunk.
3343 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3344 const struct sctp_endpoint *ep,
3345 const struct sctp_association *asoc,
3346 const union sctp_subtype type,
3348 struct sctp_cmd_seq *commands)
3350 struct sctp_chunk *chunk = arg;
3351 struct sctp_sackhdr *sackh;
3354 trace_sctp_probe(ep, asoc, chunk);
3356 if (!sctp_vtag_verify(chunk, asoc))
3357 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3359 /* Make sure that the SACK chunk has a valid length. */
3360 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3361 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3364 /* Pull the SACK chunk from the data buffer */
3365 sackh = sctp_sm_pull_sack(chunk);
3366 /* Was this a bogus SACK? */
3368 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3369 chunk->subh.sack_hdr = sackh;
3370 ctsn = ntohl(sackh->cum_tsn_ack);
3372 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3373 * Ack Point, then drop the SACK. Since Cumulative TSN
3374 * Ack is monotonically increasing, a SACK whose
3375 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3376 * Point indicates an out-of-order SACK.
3378 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3379 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3380 asoc->ctsn_ack_point);
3382 return SCTP_DISPOSITION_DISCARD;
3385 /* If Cumulative TSN Ack beyond the max tsn currently
3386 * send, terminating the association and respond to the
3387 * sender with an ABORT.
3389 if (!TSN_lt(ctsn, asoc->next_tsn))
3390 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3392 /* Return this SACK for further processing. */
3393 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3395 /* Note: We do the rest of the work on the PROCESS_SACK
3398 return SCTP_DISPOSITION_CONSUME;
3402 * Generate an ABORT in response to a packet.
3404 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3406 * 8) The receiver should respond to the sender of the OOTB packet with
3407 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3408 * MUST fill in the Verification Tag field of the outbound packet
3409 * with the value found in the Verification Tag field of the OOTB
3410 * packet and set the T-bit in the Chunk Flags to indicate that the
3411 * Verification Tag is reflected. After sending this ABORT, the
3412 * receiver of the OOTB packet shall discard the OOTB packet and take
3413 * no further action.
3417 * The return value is the disposition of the chunk.
3419 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3421 const struct sctp_endpoint *ep,
3422 const struct sctp_association *asoc,
3423 const union sctp_subtype type,
3425 struct sctp_cmd_seq *commands)
3427 struct sctp_packet *packet = NULL;
3428 struct sctp_chunk *chunk = arg;
3429 struct sctp_chunk *abort;
3431 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3433 return SCTP_DISPOSITION_NOMEM;
3435 /* Make an ABORT. The T bit will be set if the asoc
3438 abort = sctp_make_abort(asoc, chunk, 0);
3440 sctp_ootb_pkt_free(packet);
3441 return SCTP_DISPOSITION_NOMEM;
3444 /* Reflect vtag if T-Bit is set */
3445 if (sctp_test_T_bit(abort))
3446 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3448 /* Set the skb to the belonging sock for accounting. */
3449 abort->skb->sk = ep->base.sk;
3451 sctp_packet_append_chunk(packet, abort);
3453 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3454 SCTP_PACKET(packet));
3456 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3458 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3459 return SCTP_DISPOSITION_CONSUME;
3463 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3464 * event as ULP notification for each cause included in the chunk.
3466 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3468 * The return value is the disposition of the chunk.
3470 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3471 const struct sctp_endpoint *ep,
3472 const struct sctp_association *asoc,
3473 const union sctp_subtype type,
3475 struct sctp_cmd_seq *commands)
3477 struct sctp_chunk *chunk = arg;
3478 struct sctp_errhdr *err;
3480 if (!sctp_vtag_verify(chunk, asoc))
3481 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3483 /* Make sure that the ERROR chunk has a valid length. */
3484 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3485 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3487 sctp_walk_errors(err, chunk->chunk_hdr);
3488 if ((void *)err != (void *)chunk->chunk_end)
3489 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3490 (void *)err, commands);
3492 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3495 return SCTP_DISPOSITION_CONSUME;
3499 * Process an inbound SHUTDOWN ACK.
3502 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3503 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3504 * peer, and remove all record of the association.
3506 * The return value is the disposition.
3508 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3509 const struct sctp_endpoint *ep,
3510 const struct sctp_association *asoc,
3511 const union sctp_subtype type,
3513 struct sctp_cmd_seq *commands)
3515 struct sctp_chunk *chunk = arg;
3516 struct sctp_chunk *reply;
3517 struct sctp_ulpevent *ev;
3519 if (!sctp_vtag_verify(chunk, asoc))
3520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3522 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3523 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3524 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3526 /* 10.2 H) SHUTDOWN COMPLETE notification
3528 * When SCTP completes the shutdown procedures (section 9.2) this
3529 * notification is passed to the upper layer.
3531 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3532 0, 0, 0, NULL, GFP_ATOMIC);
3536 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3537 reply = sctp_make_shutdown_complete(asoc, chunk);
3541 /* Do all the commands now (after allocation), so that we
3542 * have consistent state if memory allocation failes
3544 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3546 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3547 * stop the T2-shutdown timer,
3549 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3550 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3552 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3553 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3555 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3556 SCTP_STATE(SCTP_STATE_CLOSED));
3557 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3558 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3559 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3561 /* ...and remove all record of the association. */
3562 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3563 return SCTP_DISPOSITION_DELETE_TCB;
3566 sctp_ulpevent_free(ev);
3568 return SCTP_DISPOSITION_NOMEM;
3572 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3574 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3575 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3576 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3577 * packet must fill in the Verification Tag field of the outbound
3578 * packet with the Verification Tag received in the SHUTDOWN ACK and
3579 * set the T-bit in the Chunk Flags to indicate that the Verification
3582 * 8) The receiver should respond to the sender of the OOTB packet with
3583 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3584 * MUST fill in the Verification Tag field of the outbound packet
3585 * with the value found in the Verification Tag field of the OOTB
3586 * packet and set the T-bit in the Chunk Flags to indicate that the
3587 * Verification Tag is reflected. After sending this ABORT, the
3588 * receiver of the OOTB packet shall discard the OOTB packet and take
3589 * no further action.
3591 enum sctp_disposition sctp_sf_ootb(struct net *net,
3592 const struct sctp_endpoint *ep,
3593 const struct sctp_association *asoc,
3594 const union sctp_subtype type,
3595 void *arg, struct sctp_cmd_seq *commands)
3597 struct sctp_chunk *chunk = arg;
3598 struct sk_buff *skb = chunk->skb;
3599 struct sctp_chunkhdr *ch;
3600 struct sctp_errhdr *err;
3601 int ootb_cookie_ack = 0;
3602 int ootb_shut_ack = 0;
3605 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3607 if (asoc && !sctp_vtag_verify(chunk, asoc))
3610 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3612 /* Report violation if the chunk is less then minimal */
3613 if (ntohs(ch->length) < sizeof(*ch))
3614 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3617 /* Report violation if chunk len overflows */
3618 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3619 if (ch_end > skb_tail_pointer(skb))
3620 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3623 /* Now that we know we at least have a chunk header,
3624 * do things that are type appropriate.
3626 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3629 /* RFC 2960, Section 3.3.7
3630 * Moreover, under any circumstances, an endpoint that
3631 * receives an ABORT MUST NOT respond to that ABORT by
3632 * sending an ABORT of its own.
3634 if (SCTP_CID_ABORT == ch->type)
3635 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3637 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3638 * or a COOKIE ACK the SCTP Packet should be silently
3642 if (SCTP_CID_COOKIE_ACK == ch->type)
3643 ootb_cookie_ack = 1;
3645 if (SCTP_CID_ERROR == ch->type) {
3646 sctp_walk_errors(err, ch) {
3647 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3648 ootb_cookie_ack = 1;
3654 ch = (struct sctp_chunkhdr *)ch_end;
3655 } while (ch_end < skb_tail_pointer(skb));
3658 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3659 else if (ootb_cookie_ack)
3660 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3662 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3666 * Handle an "Out of the blue" SHUTDOWN ACK.
3668 * Section: 8.4 5, sctpimpguide 2.41.
3670 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3671 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3672 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3673 * packet must fill in the Verification Tag field of the outbound
3674 * packet with the Verification Tag received in the SHUTDOWN ACK and
3675 * set the T-bit in the Chunk Flags to indicate that the Verification
3679 * (endpoint, asoc, type, arg, commands)
3682 * (enum sctp_disposition)
3684 * The return value is the disposition of the chunk.
3686 static enum sctp_disposition sctp_sf_shut_8_4_5(
3688 const struct sctp_endpoint *ep,
3689 const struct sctp_association *asoc,
3690 const union sctp_subtype type,
3692 struct sctp_cmd_seq *commands)
3694 struct sctp_packet *packet = NULL;
3695 struct sctp_chunk *chunk = arg;
3696 struct sctp_chunk *shut;
3698 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3700 return SCTP_DISPOSITION_NOMEM;
3702 /* Make an SHUTDOWN_COMPLETE.
3703 * The T bit will be set if the asoc is NULL.
3705 shut = sctp_make_shutdown_complete(asoc, chunk);
3707 sctp_ootb_pkt_free(packet);
3708 return SCTP_DISPOSITION_NOMEM;
3711 /* Reflect vtag if T-Bit is set */
3712 if (sctp_test_T_bit(shut))
3713 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3715 /* Set the skb to the belonging sock for accounting. */
3716 shut->skb->sk = ep->base.sk;
3718 sctp_packet_append_chunk(packet, shut);
3720 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3721 SCTP_PACKET(packet));
3723 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3725 /* We need to discard the rest of the packet to prevent
3726 * potential bomming attacks from additional bundled chunks.
3727 * This is documented in SCTP Threats ID.
3729 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3733 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3735 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3736 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3737 * procedures in section 8.4 SHOULD be followed, in other words it
3738 * should be treated as an Out Of The Blue packet.
3739 * [This means that we do NOT check the Verification Tag on these
3743 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3744 const struct sctp_endpoint *ep,
3745 const struct sctp_association *asoc,
3746 const union sctp_subtype type,
3748 struct sctp_cmd_seq *commands)
3750 struct sctp_chunk *chunk = arg;
3752 if (!sctp_vtag_verify(chunk, asoc))
3755 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3756 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3760 /* Although we do have an association in this case, it corresponds
3761 * to a restarted association. So the packet is treated as an OOTB
3762 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3763 * called with a NULL association.
3765 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3767 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3770 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3771 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3772 const struct sctp_endpoint *ep,
3773 const struct sctp_association *asoc,
3774 const union sctp_subtype type,
3776 struct sctp_cmd_seq *commands)
3778 struct sctp_paramhdr *err_param = NULL;
3779 struct sctp_chunk *asconf_ack = NULL;
3780 struct sctp_chunk *chunk = arg;
3781 struct sctp_addiphdr *hdr;
3784 if (!sctp_vtag_verify(chunk, asoc)) {
3785 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3787 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3790 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3791 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3792 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3795 /* ADD-IP: Section 4.1.1
3796 * This chunk MUST be sent in an authenticated way by using
3797 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3798 * is received unauthenticated it MUST be silently discarded as
3799 * described in [I-D.ietf-tsvwg-sctp-auth].
3801 if (!net->sctp.addip_noauth && !chunk->auth)
3802 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3804 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3805 serial = ntohl(hdr->serial);
3807 /* Verify the ASCONF chunk before processing it. */
3808 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3809 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3810 (void *)err_param, commands);
3812 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3813 * the endpoint stored in a new association variable
3814 * 'Peer-Serial-Number'.
3816 if (serial == asoc->peer.addip_serial + 1) {
3817 /* If this is the first instance of ASCONF in the packet,
3818 * we can clean our old ASCONF-ACKs.
3820 if (!chunk->has_asconf)
3821 sctp_assoc_clean_asconf_ack_cache(asoc);
3823 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3824 * expected, process the ASCONF as described below and after
3825 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3826 * the response packet and cache a copy of it (in the event it
3827 * later needs to be retransmitted).
3829 * Essentially, do V1-V5.
3831 asconf_ack = sctp_process_asconf((struct sctp_association *)
3834 return SCTP_DISPOSITION_NOMEM;
3835 } else if (serial < asoc->peer.addip_serial + 1) {
3837 * If the value found in the Sequence Number is less than the
3838 * ('Peer- Sequence-Number' + 1), simply skip to the next
3839 * ASCONF, and include in the outbound response packet
3840 * any previously cached ASCONF-ACK response that was
3841 * sent and saved that matches the Sequence Number of the
3842 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3843 * Chunk exists. This will occur when an older ASCONF
3844 * arrives out of order. In such a case, the receiver
3845 * should skip the ASCONF Chunk and not include ASCONF-ACK
3846 * Chunk for that chunk.
3848 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3850 return SCTP_DISPOSITION_DISCARD;
3852 /* Reset the transport so that we select the correct one
3853 * this time around. This is to make sure that we don't
3854 * accidentally use a stale transport that's been removed.
3856 asconf_ack->transport = NULL;
3858 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3859 * it must be either a stale packet or from an attacker.
3861 return SCTP_DISPOSITION_DISCARD;
3864 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3865 * containing the ASCONF-ACK Chunks MUST be the source address of
3866 * the SCTP packet that held the ASCONF Chunks.
3868 * To do this properly, we'll set the destination address of the chunk
3869 * and at the transmit time, will try look up the transport to use.
3870 * Since ASCONFs may be bundled, the correct transport may not be
3871 * created until we process the entire packet, thus this workaround.
3873 asconf_ack->dest = chunk->source;
3874 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3875 if (asoc->new_transport) {
3876 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3877 ((struct sctp_association *)asoc)->new_transport = NULL;
3880 return SCTP_DISPOSITION_CONSUME;
3883 static enum sctp_disposition sctp_send_next_asconf(
3885 const struct sctp_endpoint *ep,
3886 struct sctp_association *asoc,
3887 const union sctp_subtype type,
3888 struct sctp_cmd_seq *commands)
3890 struct sctp_chunk *asconf;
3891 struct list_head *entry;
3893 if (list_empty(&asoc->addip_chunk_list))
3894 return SCTP_DISPOSITION_CONSUME;
3896 entry = asoc->addip_chunk_list.next;
3897 asconf = list_entry(entry, struct sctp_chunk, list);
3899 list_del_init(entry);
3900 sctp_chunk_hold(asconf);
3901 asoc->addip_last_asconf = asconf;
3903 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3907 * ADDIP Section 4.3 General rules for address manipulation
3908 * When building TLV parameters for the ASCONF Chunk that will add or
3909 * delete IP addresses the D0 to D13 rules should be applied:
3911 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3912 const struct sctp_endpoint *ep,
3913 const struct sctp_association *asoc,
3914 const union sctp_subtype type,
3916 struct sctp_cmd_seq *commands)
3918 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3919 struct sctp_paramhdr *err_param = NULL;
3920 struct sctp_chunk *asconf_ack = arg;
3921 struct sctp_addiphdr *addip_hdr;
3922 __u32 sent_serial, rcvd_serial;
3923 struct sctp_chunk *abort;
3925 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3926 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3928 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3931 /* Make sure that the ADDIP chunk has a valid length. */
3932 if (!sctp_chunk_length_valid(asconf_ack,
3933 sizeof(struct sctp_addip_chunk)))
3934 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3937 /* ADD-IP, Section 4.1.2:
3938 * This chunk MUST be sent in an authenticated way by using
3939 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3940 * is received unauthenticated it MUST be silently discarded as
3941 * described in [I-D.ietf-tsvwg-sctp-auth].
3943 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3944 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3946 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3947 rcvd_serial = ntohl(addip_hdr->serial);
3949 /* Verify the ASCONF-ACK chunk before processing it. */
3950 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3951 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3952 (void *)err_param, commands);
3955 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3956 sent_serial = ntohl(addip_hdr->serial);
3958 sent_serial = asoc->addip_serial - 1;
3961 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3962 * equal to the next serial number to be used but no ASCONF chunk is
3963 * outstanding the endpoint MUST ABORT the association. Note that a
3964 * sequence number is greater than if it is no more than 2^^31-1
3965 * larger than the current sequence number (using serial arithmetic).
3967 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3968 !(asoc->addip_last_asconf)) {
3969 abort = sctp_make_abort(asoc, asconf_ack,
3970 sizeof(struct sctp_errhdr));
3972 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3976 /* We are going to ABORT, so we might as well stop
3977 * processing the rest of the chunks in the packet.
3979 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3980 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3981 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3982 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3983 SCTP_ERROR(ECONNABORTED));
3984 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3985 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3986 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3987 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3988 return SCTP_DISPOSITION_ABORT;
3991 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3992 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3993 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3995 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3997 return sctp_send_next_asconf(net, ep,
3998 (struct sctp_association *)asoc,
4001 abort = sctp_make_abort(asoc, asconf_ack,
4002 sizeof(struct sctp_errhdr));
4004 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
4005 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4008 /* We are going to ABORT, so we might as well stop
4009 * processing the rest of the chunks in the packet.
4011 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4012 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4013 SCTP_ERROR(ECONNABORTED));
4014 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4015 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4016 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4017 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4018 return SCTP_DISPOSITION_ABORT;
4021 return SCTP_DISPOSITION_DISCARD;
4024 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
4025 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
4026 const struct sctp_endpoint *ep,
4027 const struct sctp_association *asoc,
4028 const union sctp_subtype type,
4030 struct sctp_cmd_seq *commands)
4032 struct sctp_paramhdr *err_param = NULL;
4033 struct sctp_chunk *chunk = arg;
4034 struct sctp_reconf_chunk *hdr;
4035 union sctp_params param;
4037 if (!sctp_vtag_verify(chunk, asoc)) {
4038 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4040 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4043 /* Make sure that the RECONF chunk has a valid length. */
4044 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
4045 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4048 if (!sctp_verify_reconf(asoc, chunk, &err_param))
4049 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4050 (void *)err_param, commands);
4052 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
4053 sctp_walk_params(param, hdr, params) {
4054 struct sctp_chunk *reply = NULL;
4055 struct sctp_ulpevent *ev = NULL;
4057 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4058 reply = sctp_process_strreset_outreq(
4059 (struct sctp_association *)asoc, param, &ev);
4060 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4061 reply = sctp_process_strreset_inreq(
4062 (struct sctp_association *)asoc, param, &ev);
4063 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4064 reply = sctp_process_strreset_tsnreq(
4065 (struct sctp_association *)asoc, param, &ev);
4066 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4067 reply = sctp_process_strreset_addstrm_out(
4068 (struct sctp_association *)asoc, param, &ev);
4069 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4070 reply = sctp_process_strreset_addstrm_in(
4071 (struct sctp_association *)asoc, param, &ev);
4072 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4073 reply = sctp_process_strreset_resp(
4074 (struct sctp_association *)asoc, param, &ev);
4077 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4081 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4085 return SCTP_DISPOSITION_CONSUME;
4089 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4091 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4092 * its cumulative TSN point to the value carried in the FORWARD TSN
4093 * chunk, and then MUST further advance its cumulative TSN point locally
4095 * After the above processing, the data receiver MUST stop reporting any
4096 * missing TSNs earlier than or equal to the new cumulative TSN point.
4098 * Verification Tag: 8.5 Verification Tag [Normal verification]
4100 * The return value is the disposition of the chunk.
4102 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4103 const struct sctp_endpoint *ep,
4104 const struct sctp_association *asoc,
4105 const union sctp_subtype type,
4107 struct sctp_cmd_seq *commands)
4109 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4110 struct sctp_chunk *chunk = arg;
4114 if (!sctp_vtag_verify(chunk, asoc)) {
4115 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4117 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4120 if (!asoc->peer.prsctp_capable)
4121 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4123 /* Make sure that the FORWARD_TSN chunk has valid length. */
4124 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4125 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4128 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4129 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4130 len = ntohs(chunk->chunk_hdr->length);
4131 len -= sizeof(struct sctp_chunkhdr);
4132 skb_pull(chunk->skb, len);
4134 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4135 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4137 /* The TSN is too high--silently discard the chunk and count on it
4138 * getting retransmitted later.
4140 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4141 goto discard_noforce;
4143 if (!asoc->stream.si->validate_ftsn(chunk))
4144 goto discard_noforce;
4146 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4147 if (len > sctp_ftsnhdr_len(&asoc->stream))
4148 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4151 /* Count this as receiving DATA. */
4152 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4153 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4154 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4157 /* FIXME: For now send a SACK, but DATA processing may
4160 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4162 return SCTP_DISPOSITION_CONSUME;
4165 return SCTP_DISPOSITION_DISCARD;
4168 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4170 const struct sctp_endpoint *ep,
4171 const struct sctp_association *asoc,
4172 const union sctp_subtype type,
4174 struct sctp_cmd_seq *commands)
4176 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4177 struct sctp_chunk *chunk = arg;
4181 if (!sctp_vtag_verify(chunk, asoc)) {
4182 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4184 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4187 if (!asoc->peer.prsctp_capable)
4188 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4190 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4191 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4192 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4195 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4196 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4197 len = ntohs(chunk->chunk_hdr->length);
4198 len -= sizeof(struct sctp_chunkhdr);
4199 skb_pull(chunk->skb, len);
4201 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4202 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4204 /* The TSN is too high--silently discard the chunk and count on it
4205 * getting retransmitted later.
4207 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4210 if (!asoc->stream.si->validate_ftsn(chunk))
4213 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4214 if (len > sctp_ftsnhdr_len(&asoc->stream))
4215 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4218 /* Go a head and force a SACK, since we are shutting down. */
4220 /* Implementor's Guide.
4222 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4223 * respond to each received packet containing one or more DATA chunk(s)
4224 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4226 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4227 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4228 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4229 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4231 return SCTP_DISPOSITION_CONSUME;
4235 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4237 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4238 * Identifier field. If this algorithm was not specified by the
4239 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4240 * during association setup, the AUTH chunk and all chunks after it MUST
4241 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4242 * defined in Section 4.1.
4244 * If an endpoint with no shared key receives a Shared Key Identifier
4245 * other than 0, it MUST silently discard all authenticated chunks. If
4246 * the endpoint has at least one endpoint pair shared key for the peer,
4247 * it MUST use the key specified by the Shared Key Identifier if a
4248 * key has been configured for that Shared Key Identifier. If no
4249 * endpoint pair shared key has been configured for that Shared Key
4250 * Identifier, all authenticated chunks MUST be silently discarded.
4252 * Verification Tag: 8.5 Verification Tag [Normal verification]
4254 * The return value is the disposition of the chunk.
4256 static enum sctp_ierror sctp_sf_authenticate(
4257 const struct sctp_association *asoc,
4258 struct sctp_chunk *chunk)
4260 struct sctp_shared_key *sh_key = NULL;
4261 struct sctp_authhdr *auth_hdr;
4262 __u8 *save_digest, *digest;
4263 struct sctp_hmac *hmac;
4264 unsigned int sig_len;
4267 /* Pull in the auth header, so we can do some more verification */
4268 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4269 chunk->subh.auth_hdr = auth_hdr;
4270 skb_pull(chunk->skb, sizeof(*auth_hdr));
4272 /* Make sure that we support the HMAC algorithm from the auth
4275 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4276 return SCTP_IERROR_AUTH_BAD_HMAC;
4278 /* Make sure that the provided shared key identifier has been
4281 key_id = ntohs(auth_hdr->shkey_id);
4282 if (key_id != asoc->active_key_id) {
4283 sh_key = sctp_auth_get_shkey(asoc, key_id);
4285 return SCTP_IERROR_AUTH_BAD_KEYID;
4288 /* Make sure that the length of the signature matches what
4291 sig_len = ntohs(chunk->chunk_hdr->length) -
4292 sizeof(struct sctp_auth_chunk);
4293 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4294 if (sig_len != hmac->hmac_len)
4295 return SCTP_IERROR_PROTO_VIOLATION;
4297 /* Now that we've done validation checks, we can compute and
4298 * verify the hmac. The steps involved are:
4299 * 1. Save the digest from the chunk.
4300 * 2. Zero out the digest in the chunk.
4301 * 3. Compute the new digest
4302 * 4. Compare saved and new digests.
4304 digest = auth_hdr->hmac;
4305 skb_pull(chunk->skb, sig_len);
4307 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4311 memset(digest, 0, sig_len);
4313 sctp_auth_calculate_hmac(asoc, chunk->skb,
4314 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4315 sh_key, GFP_ATOMIC);
4317 /* Discard the packet if the digests do not match */
4318 if (memcmp(save_digest, digest, sig_len)) {
4320 return SCTP_IERROR_BAD_SIG;
4326 return SCTP_IERROR_NO_ERROR;
4328 return SCTP_IERROR_NOMEM;
4331 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4332 const struct sctp_endpoint *ep,
4333 const struct sctp_association *asoc,
4334 const union sctp_subtype type,
4335 void *arg, struct sctp_cmd_seq *commands)
4337 struct sctp_chunk *chunk = arg;
4338 struct sctp_authhdr *auth_hdr;
4339 struct sctp_chunk *err_chunk;
4340 enum sctp_ierror error;
4342 /* Make sure that the peer has AUTH capable */
4343 if (!asoc->peer.auth_capable)
4344 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4346 if (!sctp_vtag_verify(chunk, asoc)) {
4347 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4349 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4352 /* Make sure that the AUTH chunk has valid length. */
4353 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4354 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4357 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4358 error = sctp_sf_authenticate(asoc, chunk);
4360 case SCTP_IERROR_AUTH_BAD_HMAC:
4361 /* Generate the ERROR chunk and discard the rest
4364 err_chunk = sctp_make_op_error(asoc, chunk,
4365 SCTP_ERROR_UNSUP_HMAC,
4369 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4370 SCTP_CHUNK(err_chunk));
4373 case SCTP_IERROR_AUTH_BAD_KEYID:
4374 case SCTP_IERROR_BAD_SIG:
4375 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4377 case SCTP_IERROR_PROTO_VIOLATION:
4378 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4381 case SCTP_IERROR_NOMEM:
4382 return SCTP_DISPOSITION_NOMEM;
4384 default: /* Prevent gcc warnings */
4388 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4389 struct sctp_ulpevent *ev;
4391 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4392 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4397 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4401 return SCTP_DISPOSITION_CONSUME;
4405 * Process an unknown chunk.
4407 * Section: 3.2. Also, 2.1 in the implementor's guide.
4409 * Chunk Types are encoded such that the highest-order two bits specify
4410 * the action that must be taken if the processing endpoint does not
4411 * recognize the Chunk Type.
4413 * 00 - Stop processing this SCTP packet and discard it, do not process
4414 * any further chunks within it.
4416 * 01 - Stop processing this SCTP packet and discard it, do not process
4417 * any further chunks within it, and report the unrecognized
4418 * chunk in an 'Unrecognized Chunk Type'.
4420 * 10 - Skip this chunk and continue processing.
4422 * 11 - Skip this chunk and continue processing, but report in an ERROR
4423 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4425 * The return value is the disposition of the chunk.
4427 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4428 const struct sctp_endpoint *ep,
4429 const struct sctp_association *asoc,
4430 const union sctp_subtype type,
4432 struct sctp_cmd_seq *commands)
4434 struct sctp_chunk *unk_chunk = arg;
4435 struct sctp_chunk *err_chunk;
4436 struct sctp_chunkhdr *hdr;
4438 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4440 if (!sctp_vtag_verify(unk_chunk, asoc))
4441 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4443 /* Make sure that the chunk has a valid length.
4444 * Since we don't know the chunk type, we use a general
4445 * chunkhdr structure to make a comparison.
4447 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4448 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4451 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4452 case SCTP_CID_ACTION_DISCARD:
4453 /* Discard the packet. */
4454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4455 case SCTP_CID_ACTION_DISCARD_ERR:
4456 /* Generate an ERROR chunk as response. */
4457 hdr = unk_chunk->chunk_hdr;
4458 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4459 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4460 SCTP_PAD4(ntohs(hdr->length)),
4463 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4464 SCTP_CHUNK(err_chunk));
4467 /* Discard the packet. */
4468 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4469 return SCTP_DISPOSITION_CONSUME;
4470 case SCTP_CID_ACTION_SKIP:
4471 /* Skip the chunk. */
4472 return SCTP_DISPOSITION_DISCARD;
4473 case SCTP_CID_ACTION_SKIP_ERR:
4474 /* Generate an ERROR chunk as response. */
4475 hdr = unk_chunk->chunk_hdr;
4476 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4477 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4478 SCTP_PAD4(ntohs(hdr->length)),
4481 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4482 SCTP_CHUNK(err_chunk));
4484 /* Skip the chunk. */
4485 return SCTP_DISPOSITION_CONSUME;
4490 return SCTP_DISPOSITION_DISCARD;
4494 * Discard the chunk.
4496 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4497 * [Too numerous to mention...]
4498 * Verification Tag: No verification needed.
4500 * (endpoint, asoc, chunk)
4503 * (asoc, reply_msg, msg_up, timers, counters)
4505 * The return value is the disposition of the chunk.
4507 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4508 const struct sctp_endpoint *ep,
4509 const struct sctp_association *asoc,
4510 const union sctp_subtype type,
4512 struct sctp_cmd_seq *commands)
4514 struct sctp_chunk *chunk = arg;
4516 if (asoc && !sctp_vtag_verify(chunk, asoc))
4517 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4519 /* Make sure that the chunk has a valid length.
4520 * Since we don't know the chunk type, we use a general
4521 * chunkhdr structure to make a comparison.
4523 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4524 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4527 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4529 return SCTP_DISPOSITION_DISCARD;
4533 * Discard the whole packet.
4537 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4538 * silently discard the OOTB packet and take no further action.
4540 * Verification Tag: No verification necessary
4543 * (endpoint, asoc, chunk)
4546 * (asoc, reply_msg, msg_up, timers, counters)
4548 * The return value is the disposition of the chunk.
4550 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4551 const struct sctp_endpoint *ep,
4552 const struct sctp_association *asoc,
4553 const union sctp_subtype type,
4554 void *arg, struct sctp_cmd_seq *commands)
4556 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4557 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4559 return SCTP_DISPOSITION_CONSUME;
4564 * The other end is violating protocol.
4566 * Section: Not specified
4567 * Verification Tag: Not specified
4569 * (endpoint, asoc, chunk)
4572 * (asoc, reply_msg, msg_up, timers, counters)
4574 * We simply tag the chunk as a violation. The state machine will log
4575 * the violation and continue.
4577 enum sctp_disposition sctp_sf_violation(struct net *net,
4578 const struct sctp_endpoint *ep,
4579 const struct sctp_association *asoc,
4580 const union sctp_subtype type,
4582 struct sctp_cmd_seq *commands)
4584 struct sctp_chunk *chunk = arg;
4586 if (!sctp_vtag_verify(chunk, asoc))
4587 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4589 /* Make sure that the chunk has a valid length. */
4590 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4591 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4594 return SCTP_DISPOSITION_VIOLATION;
4598 * Common function to handle a protocol violation.
4600 static enum sctp_disposition sctp_sf_abort_violation(
4602 const struct sctp_endpoint *ep,
4603 const struct sctp_association *asoc,
4605 struct sctp_cmd_seq *commands,
4606 const __u8 *payload,
4607 const size_t paylen)
4609 struct sctp_packet *packet = NULL;
4610 struct sctp_chunk *chunk = arg;
4611 struct sctp_chunk *abort = NULL;
4613 /* SCTP-AUTH, Section 6.3:
4614 * It should be noted that if the receiver wants to tear
4615 * down an association in an authenticated way only, the
4616 * handling of malformed packets should not result in
4617 * tearing down the association.
4619 * This means that if we only want to abort associations
4620 * in an authenticated way (i.e AUTH+ABORT), then we
4621 * can't destroy this association just because the packet
4624 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4627 /* Make the abort chunk. */
4628 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4633 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4634 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4635 !asoc->peer.i.init_tag) {
4636 struct sctp_initack_chunk *initack;
4638 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4639 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4640 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4642 unsigned int inittag;
4644 inittag = ntohl(initack->init_hdr.init_tag);
4645 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4650 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4651 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4653 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4654 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4655 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4656 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4657 SCTP_ERROR(ECONNREFUSED));
4658 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4659 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4661 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4662 SCTP_ERROR(ECONNABORTED));
4663 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4664 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4665 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4668 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4673 if (sctp_test_T_bit(abort))
4674 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4676 abort->skb->sk = ep->base.sk;
4678 sctp_packet_append_chunk(packet, abort);
4680 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4681 SCTP_PACKET(packet));
4683 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4686 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4689 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4690 return SCTP_DISPOSITION_ABORT;
4693 sctp_chunk_free(abort);
4695 return SCTP_DISPOSITION_NOMEM;
4699 * Handle a protocol violation when the chunk length is invalid.
4700 * "Invalid" length is identified as smaller than the minimal length a
4701 * given chunk can be. For example, a SACK chunk has invalid length
4702 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4704 * We inform the other end by sending an ABORT with a Protocol Violation
4707 * Section: Not specified
4708 * Verification Tag: Nothing to do
4710 * (endpoint, asoc, chunk)
4713 * (reply_msg, msg_up, counters)
4715 * Generate an ABORT chunk and terminate the association.
4717 static enum sctp_disposition sctp_sf_violation_chunklen(
4719 const struct sctp_endpoint *ep,
4720 const struct sctp_association *asoc,
4721 const union sctp_subtype type,
4723 struct sctp_cmd_seq *commands)
4725 static const char err_str[] = "The following chunk had invalid length:";
4727 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4732 * Handle a protocol violation when the parameter length is invalid.
4733 * If the length is smaller than the minimum length of a given parameter,
4734 * or accumulated length in multi parameters exceeds the end of the chunk,
4735 * the length is considered as invalid.
4737 static enum sctp_disposition sctp_sf_violation_paramlen(
4739 const struct sctp_endpoint *ep,
4740 const struct sctp_association *asoc,
4741 const union sctp_subtype type,
4742 void *arg, void *ext,
4743 struct sctp_cmd_seq *commands)
4745 struct sctp_paramhdr *param = ext;
4746 struct sctp_chunk *abort = NULL;
4747 struct sctp_chunk *chunk = arg;
4749 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4752 /* Make the abort chunk. */
4753 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4757 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4758 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4760 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4761 SCTP_ERROR(ECONNABORTED));
4762 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4763 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4764 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4765 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4768 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4769 return SCTP_DISPOSITION_ABORT;
4771 return SCTP_DISPOSITION_NOMEM;
4774 /* Handle a protocol violation when the peer trying to advance the
4775 * cumulative tsn ack to a point beyond the max tsn currently sent.
4777 * We inform the other end by sending an ABORT with a Protocol Violation
4780 static enum sctp_disposition sctp_sf_violation_ctsn(
4782 const struct sctp_endpoint *ep,
4783 const struct sctp_association *asoc,
4784 const union sctp_subtype type,
4786 struct sctp_cmd_seq *commands)
4788 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4790 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4794 /* Handle protocol violation of an invalid chunk bundling. For example,
4795 * when we have an association and we receive bundled INIT-ACK, or
4796 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4797 * statement from the specs. Additionally, there might be an attacker
4798 * on the path and we may not want to continue this communication.
4800 static enum sctp_disposition sctp_sf_violation_chunk(
4802 const struct sctp_endpoint *ep,
4803 const struct sctp_association *asoc,
4804 const union sctp_subtype type,
4806 struct sctp_cmd_seq *commands)
4808 static const char err_str[] = "The following chunk violates protocol:";
4811 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4813 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4816 /***************************************************************************
4817 * These are the state functions for handling primitive (Section 10) events.
4818 ***************************************************************************/
4820 * sctp_sf_do_prm_asoc
4822 * Section: 10.1 ULP-to-SCTP
4825 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4826 * outbound stream count)
4827 * -> association id [,destination transport addr list] [,outbound stream
4830 * This primitive allows the upper layer to initiate an association to a
4831 * specific peer endpoint.
4833 * The peer endpoint shall be specified by one of the transport addresses
4834 * which defines the endpoint (see Section 1.4). If the local SCTP
4835 * instance has not been initialized, the ASSOCIATE is considered an
4837 * [This is not relevant for the kernel implementation since we do all
4838 * initialization at boot time. It we hadn't initialized we wouldn't
4839 * get anywhere near this code.]
4841 * An association id, which is a local handle to the SCTP association,
4842 * will be returned on successful establishment of the association. If
4843 * SCTP is not able to open an SCTP association with the peer endpoint,
4844 * an error is returned.
4845 * [In the kernel implementation, the struct sctp_association needs to
4846 * be created BEFORE causing this primitive to run.]
4848 * Other association parameters may be returned, including the
4849 * complete destination transport addresses of the peer as well as the
4850 * outbound stream count of the local endpoint. One of the transport
4851 * address from the returned destination addresses will be selected by
4852 * the local endpoint as default primary path for sending SCTP packets
4853 * to this peer. The returned "destination transport addr list" can
4854 * be used by the ULP to change the default primary path or to force
4855 * sending a packet to a specific transport address. [All of this
4856 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4859 * Mandatory attributes:
4861 * o local SCTP instance name - obtained from the INITIALIZE operation.
4862 * [This is the argument asoc.]
4863 * o destination transport addr - specified as one of the transport
4864 * addresses of the peer endpoint with which the association is to be
4866 * [This is asoc->peer.active_path.]
4867 * o outbound stream count - the number of outbound streams the ULP
4868 * would like to open towards this peer endpoint.
4869 * [BUG: This is not currently implemented.]
4870 * Optional attributes:
4874 * The return value is a disposition.
4876 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4877 const struct sctp_endpoint *ep,
4878 const struct sctp_association *asoc,
4879 const union sctp_subtype type,
4881 struct sctp_cmd_seq *commands)
4883 struct sctp_association *my_asoc;
4884 struct sctp_chunk *repl;
4886 /* The comment below says that we enter COOKIE-WAIT AFTER
4887 * sending the INIT, but that doesn't actually work in our
4890 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4891 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4893 /* RFC 2960 5.1 Normal Establishment of an Association
4895 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4896 * must provide its Verification Tag (Tag_A) in the Initiate
4897 * Tag field. Tag_A SHOULD be a random number in the range of
4898 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4901 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4905 /* Choose transport for INIT. */
4906 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4909 /* Cast away the const modifier, as we want to just
4910 * rerun it through as a sideffect.
4912 my_asoc = (struct sctp_association *)asoc;
4913 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4915 /* After sending the INIT, "A" starts the T1-init timer and
4916 * enters the COOKIE-WAIT state.
4918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4919 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4920 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4921 return SCTP_DISPOSITION_CONSUME;
4924 return SCTP_DISPOSITION_NOMEM;
4928 * Process the SEND primitive.
4930 * Section: 10.1 ULP-to-SCTP
4933 * Format: SEND(association id, buffer address, byte count [,context]
4934 * [,stream id] [,life time] [,destination transport address]
4935 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4938 * This is the main method to send user data via SCTP.
4940 * Mandatory attributes:
4942 * o association id - local handle to the SCTP association
4944 * o buffer address - the location where the user message to be
4945 * transmitted is stored;
4947 * o byte count - The size of the user data in number of bytes;
4949 * Optional attributes:
4951 * o context - an optional 32 bit integer that will be carried in the
4952 * sending failure notification to the ULP if the transportation of
4953 * this User Message fails.
4955 * o stream id - to indicate which stream to send the data on. If not
4956 * specified, stream 0 will be used.
4958 * o life time - specifies the life time of the user data. The user data
4959 * will not be sent by SCTP after the life time expires. This
4960 * parameter can be used to avoid efforts to transmit stale
4961 * user messages. SCTP notifies the ULP if the data cannot be
4962 * initiated to transport (i.e. sent to the destination via SCTP's
4963 * send primitive) within the life time variable. However, the
4964 * user data will be transmitted if SCTP has attempted to transmit a
4965 * chunk before the life time expired.
4967 * o destination transport address - specified as one of the destination
4968 * transport addresses of the peer endpoint to which this packet
4969 * should be sent. Whenever possible, SCTP should use this destination
4970 * transport address for sending the packets, instead of the current
4973 * o unorder flag - this flag, if present, indicates that the user
4974 * would like the data delivered in an unordered fashion to the peer
4975 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4978 * o no-bundle flag - instructs SCTP not to bundle this user data with
4979 * other outbound DATA chunks. SCTP MAY still bundle even when
4980 * this flag is present, when faced with network congestion.
4982 * o payload protocol-id - A 32 bit unsigned integer that is to be
4983 * passed to the peer indicating the type of payload protocol data
4984 * being transmitted. This value is passed as opaque data by SCTP.
4986 * The return value is the disposition.
4988 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4989 const struct sctp_endpoint *ep,
4990 const struct sctp_association *asoc,
4991 const union sctp_subtype type,
4993 struct sctp_cmd_seq *commands)
4995 struct sctp_datamsg *msg = arg;
4997 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4998 return SCTP_DISPOSITION_CONSUME;
5002 * Process the SHUTDOWN primitive.
5007 * Format: SHUTDOWN(association id)
5010 * Gracefully closes an association. Any locally queued user data
5011 * will be delivered to the peer. The association will be terminated only
5012 * after the peer acknowledges all the SCTP packets sent. A success code
5013 * will be returned on successful termination of the association. If
5014 * attempting to terminate the association results in a failure, an error
5015 * code shall be returned.
5017 * Mandatory attributes:
5019 * o association id - local handle to the SCTP association
5021 * Optional attributes:
5025 * The return value is the disposition.
5027 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
5029 const struct sctp_endpoint *ep,
5030 const struct sctp_association *asoc,
5031 const union sctp_subtype type,
5033 struct sctp_cmd_seq *commands)
5035 enum sctp_disposition disposition;
5037 /* From 9.2 Shutdown of an Association
5038 * Upon receipt of the SHUTDOWN primitive from its upper
5039 * layer, the endpoint enters SHUTDOWN-PENDING state and
5040 * remains there until all outstanding data has been
5041 * acknowledged by its peer. The endpoint accepts no new data
5042 * from its upper layer, but retransmits data to the far end
5043 * if necessary to fill gaps.
5045 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5046 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5048 disposition = SCTP_DISPOSITION_CONSUME;
5049 if (sctp_outq_is_empty(&asoc->outqueue)) {
5050 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5058 * Process the ABORT primitive.
5063 * Format: Abort(association id [, cause code])
5066 * Ungracefully closes an association. Any locally queued user data
5067 * will be discarded and an ABORT chunk is sent to the peer. A success code
5068 * will be returned on successful abortion of the association. If
5069 * attempting to abort the association results in a failure, an error
5070 * code shall be returned.
5072 * Mandatory attributes:
5074 * o association id - local handle to the SCTP association
5076 * Optional attributes:
5078 * o cause code - reason of the abort to be passed to the peer
5082 * The return value is the disposition.
5084 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5086 const struct sctp_endpoint *ep,
5087 const struct sctp_association *asoc,
5088 const union sctp_subtype type,
5090 struct sctp_cmd_seq *commands)
5092 /* From 9.1 Abort of an Association
5093 * Upon receipt of the ABORT primitive from its upper
5094 * layer, the endpoint enters CLOSED state and
5095 * discard all outstanding data has been
5096 * acknowledged by its peer. The endpoint accepts no new data
5097 * from its upper layer, but retransmits data to the far end
5098 * if necessary to fill gaps.
5100 struct sctp_chunk *abort = arg;
5103 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5105 /* Even if we can't send the ABORT due to low memory delete the
5106 * TCB. This is a departure from our typical NOMEM handling.
5109 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5110 SCTP_ERROR(ECONNABORTED));
5111 /* Delete the established association. */
5112 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5113 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5115 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5116 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5118 return SCTP_DISPOSITION_ABORT;
5121 /* We tried an illegal operation on an association which is closed. */
5122 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5123 const struct sctp_endpoint *ep,
5124 const struct sctp_association *asoc,
5125 const union sctp_subtype type,
5127 struct sctp_cmd_seq *commands)
5129 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5130 return SCTP_DISPOSITION_CONSUME;
5133 /* We tried an illegal operation on an association which is shutting
5136 enum sctp_disposition sctp_sf_error_shutdown(
5138 const struct sctp_endpoint *ep,
5139 const struct sctp_association *asoc,
5140 const union sctp_subtype type,
5142 struct sctp_cmd_seq *commands)
5144 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5145 SCTP_ERROR(-ESHUTDOWN));
5146 return SCTP_DISPOSITION_CONSUME;
5150 * sctp_cookie_wait_prm_shutdown
5152 * Section: 4 Note: 2
5157 * The RFC does not explicitly address this issue, but is the route through the
5158 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5163 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5165 const struct sctp_endpoint *ep,
5166 const struct sctp_association *asoc,
5167 const union sctp_subtype type,
5169 struct sctp_cmd_seq *commands)
5171 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5172 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5174 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5175 SCTP_STATE(SCTP_STATE_CLOSED));
5177 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5179 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5181 return SCTP_DISPOSITION_DELETE_TCB;
5185 * sctp_cookie_echoed_prm_shutdown
5187 * Section: 4 Note: 2
5192 * The RFC does not explcitly address this issue, but is the route through the
5193 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5198 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5200 const struct sctp_endpoint *ep,
5201 const struct sctp_association *asoc,
5202 const union sctp_subtype type,
5204 struct sctp_cmd_seq *commands)
5206 /* There is a single T1 timer, so we should be able to use
5207 * common function with the COOKIE-WAIT state.
5209 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5213 * sctp_sf_cookie_wait_prm_abort
5215 * Section: 4 Note: 2
5220 * The RFC does not explicitly address this issue, but is the route through the
5221 * state table when someone issues an abort while in COOKIE_WAIT state.
5226 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5228 const struct sctp_endpoint *ep,
5229 const struct sctp_association *asoc,
5230 const union sctp_subtype type,
5232 struct sctp_cmd_seq *commands)
5234 struct sctp_chunk *abort = arg;
5236 /* Stop T1-init timer */
5237 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5238 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5241 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5243 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5244 SCTP_STATE(SCTP_STATE_CLOSED));
5246 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5248 /* Even if we can't send the ABORT due to low memory delete the
5249 * TCB. This is a departure from our typical NOMEM handling.
5252 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5253 SCTP_ERROR(ECONNREFUSED));
5254 /* Delete the established association. */
5255 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5256 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5258 return SCTP_DISPOSITION_ABORT;
5262 * sctp_sf_cookie_echoed_prm_abort
5264 * Section: 4 Note: 3
5269 * The RFC does not explcitly address this issue, but is the route through the
5270 * state table when someone issues an abort while in COOKIE_ECHOED state.
5275 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5277 const struct sctp_endpoint *ep,
5278 const struct sctp_association *asoc,
5279 const union sctp_subtype type,
5281 struct sctp_cmd_seq *commands)
5283 /* There is a single T1 timer, so we should be able to use
5284 * common function with the COOKIE-WAIT state.
5286 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5290 * sctp_sf_shutdown_pending_prm_abort
5295 * The RFC does not explicitly address this issue, but is the route through the
5296 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5301 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5303 const struct sctp_endpoint *ep,
5304 const struct sctp_association *asoc,
5305 const union sctp_subtype type,
5307 struct sctp_cmd_seq *commands)
5309 /* Stop the T5-shutdown guard timer. */
5310 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5311 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5313 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5317 * sctp_sf_shutdown_sent_prm_abort
5322 * The RFC does not explicitly address this issue, but is the route through the
5323 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5328 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5330 const struct sctp_endpoint *ep,
5331 const struct sctp_association *asoc,
5332 const union sctp_subtype type,
5334 struct sctp_cmd_seq *commands)
5336 /* Stop the T2-shutdown timer. */
5337 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5338 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5340 /* Stop the T5-shutdown guard timer. */
5341 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5342 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5344 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5348 * sctp_sf_cookie_echoed_prm_abort
5353 * The RFC does not explcitly address this issue, but is the route through the
5354 * state table when someone issues an abort while in COOKIE_ECHOED state.
5359 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5361 const struct sctp_endpoint *ep,
5362 const struct sctp_association *asoc,
5363 const union sctp_subtype type,
5365 struct sctp_cmd_seq *commands)
5367 /* The same T2 timer, so we should be able to use
5368 * common function with the SHUTDOWN-SENT state.
5370 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5374 * Process the REQUESTHEARTBEAT primitive
5377 * J) Request Heartbeat
5379 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5383 * Instructs the local endpoint to perform a HeartBeat on the specified
5384 * destination transport address of the given association. The returned
5385 * result should indicate whether the transmission of the HEARTBEAT
5386 * chunk to the destination address is successful.
5388 * Mandatory attributes:
5390 * o association id - local handle to the SCTP association
5392 * o destination transport address - the transport address of the
5393 * association on which a heartbeat should be issued.
5395 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5397 const struct sctp_endpoint *ep,
5398 const struct sctp_association *asoc,
5399 const union sctp_subtype type,
5401 struct sctp_cmd_seq *commands)
5403 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5404 (struct sctp_transport *)arg, commands))
5405 return SCTP_DISPOSITION_NOMEM;
5408 * RFC 2960 (bis), section 8.3
5410 * D) Request an on-demand HEARTBEAT on a specific destination
5411 * transport address of a given association.
5413 * The endpoint should increment the respective error counter of
5414 * the destination transport address each time a HEARTBEAT is sent
5415 * to that address and not acknowledged within one RTO.
5418 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5419 SCTP_TRANSPORT(arg));
5420 return SCTP_DISPOSITION_CONSUME;
5424 * ADDIP Section 4.1 ASCONF Chunk Procedures
5425 * When an endpoint has an ASCONF signaled change to be sent to the
5426 * remote endpoint it should do A1 to A9
5428 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5429 const struct sctp_endpoint *ep,
5430 const struct sctp_association *asoc,
5431 const union sctp_subtype type,
5433 struct sctp_cmd_seq *commands)
5435 struct sctp_chunk *chunk = arg;
5437 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5438 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5439 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5440 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5441 return SCTP_DISPOSITION_CONSUME;
5444 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5445 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5446 const struct sctp_endpoint *ep,
5447 const struct sctp_association *asoc,
5448 const union sctp_subtype type,
5450 struct sctp_cmd_seq *commands)
5452 struct sctp_chunk *chunk = arg;
5454 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5455 return SCTP_DISPOSITION_CONSUME;
5459 * Ignore the primitive event
5461 * The return value is the disposition of the primitive.
5463 enum sctp_disposition sctp_sf_ignore_primitive(
5465 const struct sctp_endpoint *ep,
5466 const struct sctp_association *asoc,
5467 const union sctp_subtype type,
5469 struct sctp_cmd_seq *commands)
5471 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5474 return SCTP_DISPOSITION_DISCARD;
5477 /***************************************************************************
5478 * These are the state functions for the OTHER events.
5479 ***************************************************************************/
5482 * When the SCTP stack has no more user data to send or retransmit, this
5483 * notification is given to the user. Also, at the time when a user app
5484 * subscribes to this event, if there is no data to be sent or
5485 * retransmit, the stack will immediately send up this notification.
5487 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5489 const struct sctp_endpoint *ep,
5490 const struct sctp_association *asoc,
5491 const union sctp_subtype type,
5493 struct sctp_cmd_seq *commands)
5495 struct sctp_ulpevent *event;
5497 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5499 return SCTP_DISPOSITION_NOMEM;
5501 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5503 return SCTP_DISPOSITION_CONSUME;
5507 * Start the shutdown negotiation.
5510 * Once all its outstanding data has been acknowledged, the endpoint
5511 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5512 * TSN Ack field the last sequential TSN it has received from the peer.
5513 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5514 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5515 * with the updated last sequential TSN received from its peer.
5517 * The return value is the disposition.
5519 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5521 const struct sctp_endpoint *ep,
5522 const struct sctp_association *asoc,
5523 const union sctp_subtype type,
5525 struct sctp_cmd_seq *commands)
5527 struct sctp_chunk *reply;
5529 /* Once all its outstanding data has been acknowledged, the
5530 * endpoint shall send a SHUTDOWN chunk to its peer including
5531 * in the Cumulative TSN Ack field the last sequential TSN it
5532 * has received from the peer.
5534 reply = sctp_make_shutdown(asoc, arg);
5538 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5539 * T2-shutdown timer.
5541 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5543 /* It shall then start the T2-shutdown timer */
5544 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5545 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5547 /* RFC 4960 Section 9.2
5548 * The sender of the SHUTDOWN MAY also start an overall guard timer
5549 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5551 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5552 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5554 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5555 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5556 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5558 /* and enter the SHUTDOWN-SENT state. */
5559 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5560 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5562 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5564 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5567 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5569 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5571 return SCTP_DISPOSITION_CONSUME;
5574 return SCTP_DISPOSITION_NOMEM;
5578 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5582 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5583 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5584 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5585 * endpoint must re-send the SHUTDOWN ACK.
5587 * The return value is the disposition.
5589 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5591 const struct sctp_endpoint *ep,
5592 const struct sctp_association *asoc,
5593 const union sctp_subtype type,
5595 struct sctp_cmd_seq *commands)
5597 struct sctp_chunk *chunk = arg;
5598 struct sctp_chunk *reply;
5600 /* There are 2 ways of getting here:
5601 * 1) called in response to a SHUTDOWN chunk
5602 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5604 * For the case (2), the arg parameter is set to NULL. We need
5605 * to check that we have a chunk before accessing it's fields.
5608 if (!sctp_vtag_verify(chunk, asoc))
5609 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5612 /* Make sure that the SHUTDOWN chunk has a valid length. */
5613 if (!sctp_chunk_length_valid(
5614 chunk, sizeof(struct sctp_shutdown_chunk)))
5615 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5619 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5620 * shall send a SHUTDOWN ACK ...
5622 reply = sctp_make_shutdown_ack(asoc, chunk);
5626 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5627 * the T2-shutdown timer.
5629 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5631 /* and start/restart a T2-shutdown timer of its own, */
5632 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5633 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5635 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5636 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5637 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5639 /* Enter the SHUTDOWN-ACK-SENT state. */
5640 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5641 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5643 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5645 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5648 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5650 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5652 return SCTP_DISPOSITION_CONSUME;
5655 return SCTP_DISPOSITION_NOMEM;
5659 * Ignore the event defined as other
5661 * The return value is the disposition of the event.
5663 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5664 const struct sctp_endpoint *ep,
5665 const struct sctp_association *asoc,
5666 const union sctp_subtype type,
5668 struct sctp_cmd_seq *commands)
5670 pr_debug("%s: the event other type:%d is ignored\n",
5671 __func__, type.other);
5673 return SCTP_DISPOSITION_DISCARD;
5676 /************************************************************
5677 * These are the state functions for handling timeout events.
5678 ************************************************************/
5683 * Section: 6.3.3 Handle T3-rtx Expiration
5685 * Whenever the retransmission timer T3-rtx expires for a destination
5686 * address, do the following:
5689 * The return value is the disposition of the chunk.
5691 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5692 const struct sctp_endpoint *ep,
5693 const struct sctp_association *asoc,
5694 const union sctp_subtype type,
5696 struct sctp_cmd_seq *commands)
5698 struct sctp_transport *transport = arg;
5700 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5702 if (asoc->overall_error_count >= asoc->max_retrans) {
5703 if (asoc->peer.zero_window_announced &&
5704 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5706 * We are here likely because the receiver had its rwnd
5707 * closed for a while and we have not been able to
5708 * transmit the locally queued data within the maximum
5709 * retransmission attempts limit. Start the T5
5710 * shutdown guard timer to give the receiver one last
5711 * chance and some additional time to recover before
5714 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5715 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5717 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5718 SCTP_ERROR(ETIMEDOUT));
5719 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5720 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5721 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5722 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5723 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5724 return SCTP_DISPOSITION_DELETE_TCB;
5728 /* E1) For the destination address for which the timer
5729 * expires, adjust its ssthresh with rules defined in Section
5730 * 7.2.3 and set the cwnd <- MTU.
5733 /* E2) For the destination address for which the timer
5734 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5735 * maximum value discussed in rule C7 above (RTO.max) may be
5736 * used to provide an upper bound to this doubling operation.
5739 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5740 * outstanding DATA chunks for the address for which the
5741 * T3-rtx has expired will fit into a single packet, subject
5742 * to the MTU constraint for the path corresponding to the
5743 * destination transport address to which the retransmission
5744 * is being sent (this may be different from the address for
5745 * which the timer expires [see Section 6.4]). Call this
5746 * value K. Bundle and retransmit those K DATA chunks in a
5747 * single packet to the destination endpoint.
5749 * Note: Any DATA chunks that were sent to the address for
5750 * which the T3-rtx timer expired but did not fit in one MTU
5751 * (rule E3 above), should be marked for retransmission and
5752 * sent as soon as cwnd allows (normally when a SACK arrives).
5755 /* Do some failure management (Section 8.2). */
5756 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5758 /* NB: Rules E4 and F1 are implicit in R1. */
5759 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5761 return SCTP_DISPOSITION_CONSUME;
5765 * Generate delayed SACK on timeout
5767 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5769 * The guidelines on delayed acknowledgement algorithm specified in
5770 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5771 * acknowledgement SHOULD be generated for at least every second packet
5772 * (not every second DATA chunk) received, and SHOULD be generated
5773 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5774 * some situations it may be beneficial for an SCTP transmitter to be
5775 * more conservative than the algorithms detailed in this document
5776 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5777 * the following algorithms allow.
5779 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5780 const struct sctp_endpoint *ep,
5781 const struct sctp_association *asoc,
5782 const union sctp_subtype type,
5784 struct sctp_cmd_seq *commands)
5786 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5787 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5788 return SCTP_DISPOSITION_CONSUME;
5792 * sctp_sf_t1_init_timer_expire
5794 * Section: 4 Note: 2
5799 * RFC 2960 Section 4 Notes
5800 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5801 * and re-start the T1-init timer without changing state. This MUST
5802 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5803 * endpoint MUST abort the initialization process and report the
5804 * error to SCTP user.
5810 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5812 const struct sctp_endpoint *ep,
5813 const struct sctp_association *asoc,
5814 const union sctp_subtype type,
5816 struct sctp_cmd_seq *commands)
5818 int attempts = asoc->init_err_counter + 1;
5819 struct sctp_chunk *repl = NULL;
5820 struct sctp_bind_addr *bp;
5822 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5824 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5826 if (attempts <= asoc->max_init_attempts) {
5827 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5828 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5830 return SCTP_DISPOSITION_NOMEM;
5832 /* Choose transport for INIT. */
5833 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5836 /* Issue a sideeffect to do the needed accounting. */
5837 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5838 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5842 pr_debug("%s: giving up on INIT, attempts:%d "
5843 "max_init_attempts:%d\n", __func__, attempts,
5844 asoc->max_init_attempts);
5846 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5847 SCTP_ERROR(ETIMEDOUT));
5848 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5849 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5850 return SCTP_DISPOSITION_DELETE_TCB;
5853 return SCTP_DISPOSITION_CONSUME;
5857 * sctp_sf_t1_cookie_timer_expire
5859 * Section: 4 Note: 2
5864 * RFC 2960 Section 4 Notes
5865 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5866 * COOKIE ECHO and re-start the T1-cookie timer without changing
5867 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5868 * After that, the endpoint MUST abort the initialization process and
5869 * report the error to SCTP user.
5875 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5877 const struct sctp_endpoint *ep,
5878 const struct sctp_association *asoc,
5879 const union sctp_subtype type,
5881 struct sctp_cmd_seq *commands)
5883 int attempts = asoc->init_err_counter + 1;
5884 struct sctp_chunk *repl = NULL;
5886 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5888 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5890 if (attempts <= asoc->max_init_attempts) {
5891 repl = sctp_make_cookie_echo(asoc, NULL);
5893 return SCTP_DISPOSITION_NOMEM;
5895 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5897 /* Issue a sideeffect to do the needed accounting. */
5898 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5899 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5901 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5903 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5904 SCTP_ERROR(ETIMEDOUT));
5905 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5906 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5907 return SCTP_DISPOSITION_DELETE_TCB;
5910 return SCTP_DISPOSITION_CONSUME;
5913 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5914 * with the updated last sequential TSN received from its peer.
5916 * An endpoint should limit the number of retransmissions of the
5917 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5918 * If this threshold is exceeded the endpoint should destroy the TCB and
5919 * MUST report the peer endpoint unreachable to the upper layer (and
5920 * thus the association enters the CLOSED state). The reception of any
5921 * packet from its peer (i.e. as the peer sends all of its queued DATA
5922 * chunks) should clear the endpoint's retransmission count and restart
5923 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5924 * all of its queued DATA chunks that have not yet been sent.
5926 enum sctp_disposition sctp_sf_t2_timer_expire(
5928 const struct sctp_endpoint *ep,
5929 const struct sctp_association *asoc,
5930 const union sctp_subtype type,
5932 struct sctp_cmd_seq *commands)
5934 struct sctp_chunk *reply = NULL;
5936 pr_debug("%s: timer T2 expired\n", __func__);
5938 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5940 ((struct sctp_association *)asoc)->shutdown_retries++;
5942 if (asoc->overall_error_count >= asoc->max_retrans) {
5943 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5944 SCTP_ERROR(ETIMEDOUT));
5945 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5946 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5947 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5948 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5949 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5950 return SCTP_DISPOSITION_DELETE_TCB;
5953 switch (asoc->state) {
5954 case SCTP_STATE_SHUTDOWN_SENT:
5955 reply = sctp_make_shutdown(asoc, NULL);
5958 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5959 reply = sctp_make_shutdown_ack(asoc, NULL);
5970 /* Do some failure management (Section 8.2).
5971 * If we remove the transport an SHUTDOWN was last sent to, don't
5972 * do failure management.
5974 if (asoc->shutdown_last_sent_to)
5975 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5976 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5978 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5979 * the T2-shutdown timer.
5981 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5983 /* Restart the T2-shutdown timer. */
5984 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5985 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5986 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5987 return SCTP_DISPOSITION_CONSUME;
5990 return SCTP_DISPOSITION_NOMEM;
5994 * ADDIP Section 4.1 ASCONF CHunk Procedures
5995 * If the T4 RTO timer expires the endpoint should do B1 to B5
5997 enum sctp_disposition sctp_sf_t4_timer_expire(
5999 const struct sctp_endpoint *ep,
6000 const struct sctp_association *asoc,
6001 const union sctp_subtype type,
6003 struct sctp_cmd_seq *commands)
6005 struct sctp_chunk *chunk = asoc->addip_last_asconf;
6006 struct sctp_transport *transport = chunk->transport;
6008 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
6010 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
6011 * detection on the appropriate destination address as defined in
6012 * RFC2960 [5] section 8.1 and 8.2.
6015 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6016 SCTP_TRANSPORT(transport));
6018 /* Reconfig T4 timer and transport. */
6019 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
6021 /* ADDIP 4.1 B2) Increment the association error counters and perform
6022 * endpoint failure detection on the association as defined in
6023 * RFC2960 [5] section 8.1 and 8.2.
6024 * association error counter is incremented in SCTP_CMD_STRIKE.
6026 if (asoc->overall_error_count >= asoc->max_retrans) {
6027 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
6028 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6029 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6030 SCTP_ERROR(ETIMEDOUT));
6031 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6032 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6033 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6034 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6035 return SCTP_DISPOSITION_ABORT;
6038 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
6039 * the ASCONF chunk was sent by doubling the RTO timer value.
6040 * This is done in SCTP_CMD_STRIKE.
6043 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
6044 * choose an alternate destination address (please refer to RFC2960
6045 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
6046 * chunk, it MUST be the same (including its serial number) as the last
6049 sctp_chunk_hold(asoc->addip_last_asconf);
6050 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6051 SCTP_CHUNK(asoc->addip_last_asconf));
6053 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
6054 * destination is selected, then the RTO used will be that of the new
6055 * destination address.
6057 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6058 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6060 return SCTP_DISPOSITION_CONSUME;
6063 /* sctpimpguide-05 Section 2.12.2
6064 * The sender of the SHUTDOWN MAY also start an overall guard timer
6065 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6066 * At the expiration of this timer the sender SHOULD abort the association
6067 * by sending an ABORT chunk.
6069 enum sctp_disposition sctp_sf_t5_timer_expire(
6071 const struct sctp_endpoint *ep,
6072 const struct sctp_association *asoc,
6073 const union sctp_subtype type,
6075 struct sctp_cmd_seq *commands)
6077 struct sctp_chunk *reply = NULL;
6079 pr_debug("%s: timer T5 expired\n", __func__);
6081 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6083 reply = sctp_make_abort(asoc, NULL, 0);
6087 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6088 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6089 SCTP_ERROR(ETIMEDOUT));
6090 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6091 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6093 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6094 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6096 return SCTP_DISPOSITION_DELETE_TCB;
6098 return SCTP_DISPOSITION_NOMEM;
6101 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6102 * the association is automatically closed by starting the shutdown process.
6103 * The work that needs to be done is same as when SHUTDOWN is initiated by
6104 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6106 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6108 const struct sctp_endpoint *ep,
6109 const struct sctp_association *asoc,
6110 const union sctp_subtype type,
6112 struct sctp_cmd_seq *commands)
6114 enum sctp_disposition disposition;
6116 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6118 /* From 9.2 Shutdown of an Association
6119 * Upon receipt of the SHUTDOWN primitive from its upper
6120 * layer, the endpoint enters SHUTDOWN-PENDING state and
6121 * remains there until all outstanding data has been
6122 * acknowledged by its peer. The endpoint accepts no new data
6123 * from its upper layer, but retransmits data to the far end
6124 * if necessary to fill gaps.
6126 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6127 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6129 disposition = SCTP_DISPOSITION_CONSUME;
6130 if (sctp_outq_is_empty(&asoc->outqueue)) {
6131 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6138 /*****************************************************************************
6139 * These are sa state functions which could apply to all types of events.
6140 ****************************************************************************/
6143 * This table entry is not implemented.
6146 * (endpoint, asoc, chunk)
6148 * The return value is the disposition of the chunk.
6150 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6151 const struct sctp_endpoint *ep,
6152 const struct sctp_association *asoc,
6153 const union sctp_subtype type,
6154 void *arg, struct sctp_cmd_seq *commands)
6156 return SCTP_DISPOSITION_NOT_IMPL;
6160 * This table entry represents a bug.
6163 * (endpoint, asoc, chunk)
6165 * The return value is the disposition of the chunk.
6167 enum sctp_disposition sctp_sf_bug(struct net *net,
6168 const struct sctp_endpoint *ep,
6169 const struct sctp_association *asoc,
6170 const union sctp_subtype type,
6171 void *arg, struct sctp_cmd_seq *commands)
6173 return SCTP_DISPOSITION_BUG;
6177 * This table entry represents the firing of a timer in the wrong state.
6178 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6179 * when the association is in the wrong state. This event should
6180 * be ignored, so as to prevent any rearming of the timer.
6183 * (endpoint, asoc, chunk)
6185 * The return value is the disposition of the chunk.
6187 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6188 const struct sctp_endpoint *ep,
6189 const struct sctp_association *asoc,
6190 const union sctp_subtype type,
6192 struct sctp_cmd_seq *commands)
6194 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6196 return SCTP_DISPOSITION_CONSUME;
6199 /********************************************************************
6200 * 2nd Level Abstractions
6201 ********************************************************************/
6203 /* Pull the SACK chunk based on the SACK header. */
6204 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6206 struct sctp_sackhdr *sack;
6211 /* Protect ourselves from reading too far into
6212 * the skb from a bogus sender.
6214 sack = (struct sctp_sackhdr *) chunk->skb->data;
6216 num_blocks = ntohs(sack->num_gap_ack_blocks);
6217 num_dup_tsns = ntohs(sack->num_dup_tsns);
6218 len = sizeof(struct sctp_sackhdr);
6219 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6220 if (len > chunk->skb->len)
6223 skb_pull(chunk->skb, len);
6228 /* Create an ABORT packet to be sent as a response, with the specified
6231 static struct sctp_packet *sctp_abort_pkt_new(
6233 const struct sctp_endpoint *ep,
6234 const struct sctp_association *asoc,
6235 struct sctp_chunk *chunk,
6236 const void *payload, size_t paylen)
6238 struct sctp_packet *packet;
6239 struct sctp_chunk *abort;
6241 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6245 * The T bit will be set if the asoc is NULL.
6247 abort = sctp_make_abort(asoc, chunk, paylen);
6249 sctp_ootb_pkt_free(packet);
6253 /* Reflect vtag if T-Bit is set */
6254 if (sctp_test_T_bit(abort))
6255 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6257 /* Add specified error causes, i.e., payload, to the
6260 sctp_addto_chunk(abort, paylen, payload);
6262 /* Set the skb to the belonging sock for accounting. */
6263 abort->skb->sk = ep->base.sk;
6265 sctp_packet_append_chunk(packet, abort);
6272 /* Allocate a packet for responding in the OOTB conditions. */
6273 static struct sctp_packet *sctp_ootb_pkt_new(
6275 const struct sctp_association *asoc,
6276 const struct sctp_chunk *chunk)
6278 struct sctp_transport *transport;
6279 struct sctp_packet *packet;
6283 /* Get the source and destination port from the inbound packet. */
6284 sport = ntohs(chunk->sctp_hdr->dest);
6285 dport = ntohs(chunk->sctp_hdr->source);
6287 /* The V-tag is going to be the same as the inbound packet if no
6288 * association exists, otherwise, use the peer's vtag.
6291 /* Special case the INIT-ACK as there is no peer's vtag
6294 switch (chunk->chunk_hdr->type) {
6296 case SCTP_CID_INIT_ACK:
6298 struct sctp_initack_chunk *initack;
6300 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6301 vtag = ntohl(initack->init_hdr.init_tag);
6305 vtag = asoc->peer.i.init_tag;
6309 /* Special case the INIT and stale COOKIE_ECHO as there is no
6312 switch (chunk->chunk_hdr->type) {
6315 struct sctp_init_chunk *init;
6317 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6318 vtag = ntohl(init->init_hdr.init_tag);
6322 vtag = ntohl(chunk->sctp_hdr->vtag);
6327 /* Make a transport for the bucket, Eliza... */
6328 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6332 /* Cache a route for the transport with the chunk's destination as
6333 * the source address.
6335 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6336 sctp_sk(net->sctp.ctl_sock));
6338 packet = &transport->packet;
6339 sctp_packet_init(packet, transport, sport, dport);
6340 sctp_packet_config(packet, vtag, 0);
6348 /* Free the packet allocated earlier for responding in the OOTB condition. */
6349 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6351 sctp_transport_free(packet->transport);
6354 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6355 static void sctp_send_stale_cookie_err(struct net *net,
6356 const struct sctp_endpoint *ep,
6357 const struct sctp_association *asoc,
6358 const struct sctp_chunk *chunk,
6359 struct sctp_cmd_seq *commands,
6360 struct sctp_chunk *err_chunk)
6362 struct sctp_packet *packet;
6365 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6367 struct sctp_signed_cookie *cookie;
6369 /* Override the OOTB vtag from the cookie. */
6370 cookie = chunk->subh.cookie_hdr;
6371 packet->vtag = cookie->c.peer_vtag;
6373 /* Set the skb to the belonging sock for accounting. */
6374 err_chunk->skb->sk = ep->base.sk;
6375 sctp_packet_append_chunk(packet, err_chunk);
6376 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6377 SCTP_PACKET(packet));
6378 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6380 sctp_chunk_free (err_chunk);
6385 /* Process a data chunk */
6386 static int sctp_eat_data(const struct sctp_association *asoc,
6387 struct sctp_chunk *chunk,
6388 struct sctp_cmd_seq *commands)
6390 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6391 struct sock *sk = asoc->base.sk;
6392 struct net *net = sock_net(sk);
6393 struct sctp_datahdr *data_hdr;
6394 struct sctp_chunk *err;
6395 enum sctp_verb deliver;
6400 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6401 chunk->subh.data_hdr = data_hdr;
6402 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6404 tsn = ntohl(data_hdr->tsn);
6405 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6407 /* ASSERT: Now skb->data is really the user data. */
6409 /* Process ECN based congestion.
6411 * Since the chunk structure is reused for all chunks within
6412 * a packet, we use ecn_ce_done to track if we've already
6413 * done CE processing for this packet.
6415 * We need to do ECN processing even if we plan to discard the
6419 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6420 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6421 chunk->ecn_ce_done = 1;
6423 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6424 /* Do real work as sideffect. */
6425 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6430 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6432 /* The TSN is too high--silently discard the chunk and
6433 * count on it getting retransmitted later.
6436 chunk->asoc->stats.outofseqtsns++;
6437 return SCTP_IERROR_HIGH_TSN;
6438 } else if (tmp > 0) {
6439 /* This is a duplicate. Record it. */
6440 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6441 return SCTP_IERROR_DUP_TSN;
6444 /* This is a new TSN. */
6446 /* Discard if there is no room in the receive window.
6447 * Actually, allow a little bit of overflow (up to a MTU).
6449 datalen = ntohs(chunk->chunk_hdr->length);
6450 datalen -= sctp_datachk_len(&asoc->stream);
6452 deliver = SCTP_CMD_CHUNK_ULP;
6454 /* Think about partial delivery. */
6455 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6457 /* Even if we don't accept this chunk there is
6460 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6463 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6464 * seems a bit troublesome in that frag_point varies based on
6465 * PMTU. In cases, such as loopback, this might be a rather
6468 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6469 (datalen > asoc->rwnd + asoc->frag_point))) {
6471 /* If this is the next TSN, consider reneging to make
6472 * room. Note: Playing nice with a confused sender. A
6473 * malicious sender can still eat up all our buffer
6474 * space and in the future we may want to detect and
6475 * do more drastic reneging.
6477 if (sctp_tsnmap_has_gap(map) &&
6478 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6479 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6480 deliver = SCTP_CMD_RENEGE;
6482 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6483 __func__, tsn, datalen, asoc->rwnd);
6485 return SCTP_IERROR_IGNORE_TSN;
6490 * Also try to renege to limit our memory usage in the event that
6491 * we are under memory pressure
6492 * If we can't renege, don't worry about it, the sk_rmem_schedule
6493 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6494 * memory usage too much
6496 if (*sk->sk_prot_creator->memory_pressure) {
6497 if (sctp_tsnmap_has_gap(map) &&
6498 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6499 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6501 deliver = SCTP_CMD_RENEGE;
6506 * Section 3.3.10.9 No User Data (9)
6510 * No User Data: This error cause is returned to the originator of a
6511 * DATA chunk if a received DATA chunk has no user data.
6513 if (unlikely(0 == datalen)) {
6514 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6516 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6519 /* We are going to ABORT, so we might as well stop
6520 * processing the rest of the chunks in the packet.
6522 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6523 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6524 SCTP_ERROR(ECONNABORTED));
6525 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6526 SCTP_PERR(SCTP_ERROR_NO_DATA));
6527 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6528 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6529 return SCTP_IERROR_NO_DATA;
6532 chunk->data_accepted = 1;
6534 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6535 * if we renege and the chunk arrives again.
6537 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6538 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6540 chunk->asoc->stats.iuodchunks++;
6542 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6544 chunk->asoc->stats.iodchunks++;
6547 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6549 * If an endpoint receive a DATA chunk with an invalid stream
6550 * identifier, it shall acknowledge the reception of the DATA chunk
6551 * following the normal procedure, immediately send an ERROR chunk
6552 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6553 * and discard the DATA chunk.
6555 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6556 /* Mark tsn as received even though we drop it */
6557 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6559 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6561 sizeof(data_hdr->stream),
6564 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6566 return SCTP_IERROR_BAD_STREAM;
6569 /* Check to see if the SSN is possible for this TSN.
6570 * The biggest gap we can record is 4K wide. Since SSNs wrap
6571 * at an unsigned short, there is no way that an SSN can
6572 * wrap and for a valid TSN. We can simply check if the current
6573 * SSN is smaller then the next expected one. If it is, it wrapped
6576 if (!asoc->stream.si->validate_data(chunk))
6577 return SCTP_IERROR_PROTO_VIOLATION;
6579 /* Send the data up to the user. Note: Schedule the
6580 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6581 * chunk needs the updated rwnd.
6583 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6585 return SCTP_IERROR_NO_ERROR;