1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
8 * This is part of the SCTP Linux Kernel Implementation.
10 * These are the state functions for the state machine.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <linux-sctp@vger.kernel.org>
32 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/ipv6.h>
52 #include <linux/net.h>
53 #include <linux/inet.h>
54 #include <linux/slab.h>
56 #include <net/inet_ecn.h>
57 #include <linux/skbuff.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
60 #include <net/sctp/structs.h>
62 static struct sctp_packet *sctp_abort_pkt_new(
64 const struct sctp_endpoint *ep,
65 const struct sctp_association *asoc,
66 struct sctp_chunk *chunk,
67 const void *payload, size_t paylen);
68 static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 struct sctp_cmd_seq *commands);
71 static struct sctp_packet *sctp_ootb_pkt_new(
73 const struct sctp_association *asoc,
74 const struct sctp_chunk *chunk);
75 static void sctp_send_stale_cookie_err(struct net *net,
76 const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 struct sctp_cmd_seq *commands,
80 struct sctp_chunk *err_chunk);
81 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
83 const struct sctp_endpoint *ep,
84 const struct sctp_association *asoc,
85 const union sctp_subtype type,
87 struct sctp_cmd_seq *commands);
88 static enum sctp_disposition sctp_sf_shut_8_4_5(
90 const struct sctp_endpoint *ep,
91 const struct sctp_association *asoc,
92 const union sctp_subtype type,
94 struct sctp_cmd_seq *commands);
95 static enum sctp_disposition sctp_sf_tabort_8_4_8(
97 const struct sctp_endpoint *ep,
98 const struct sctp_association *asoc,
99 const union sctp_subtype type,
101 struct sctp_cmd_seq *commands);
102 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
104 static enum sctp_disposition sctp_stop_t1_and_abort(
106 struct sctp_cmd_seq *commands,
107 __be16 error, int sk_err,
108 const struct sctp_association *asoc,
109 struct sctp_transport *transport);
111 static enum sctp_disposition sctp_sf_abort_violation(
113 const struct sctp_endpoint *ep,
114 const struct sctp_association *asoc,
116 struct sctp_cmd_seq *commands,
118 const size_t paylen);
120 static enum sctp_disposition sctp_sf_violation_chunklen(
122 const struct sctp_endpoint *ep,
123 const struct sctp_association *asoc,
124 const union sctp_subtype type,
126 struct sctp_cmd_seq *commands);
128 static enum sctp_disposition sctp_sf_violation_paramlen(
130 const struct sctp_endpoint *ep,
131 const struct sctp_association *asoc,
132 const union sctp_subtype type,
133 void *arg, void *ext,
134 struct sctp_cmd_seq *commands);
136 static enum sctp_disposition sctp_sf_violation_ctsn(
138 const struct sctp_endpoint *ep,
139 const struct sctp_association *asoc,
140 const union sctp_subtype type,
142 struct sctp_cmd_seq *commands);
144 static enum sctp_disposition sctp_sf_violation_chunk(
146 const struct sctp_endpoint *ep,
147 const struct sctp_association *asoc,
148 const union sctp_subtype type,
150 struct sctp_cmd_seq *commands);
152 static enum sctp_ierror sctp_sf_authenticate(
153 const struct sctp_association *asoc,
154 struct sctp_chunk *chunk);
156 static enum sctp_disposition __sctp_sf_do_9_1_abort(
158 const struct sctp_endpoint *ep,
159 const struct sctp_association *asoc,
160 const union sctp_subtype type,
162 struct sctp_cmd_seq *commands);
164 static enum sctp_disposition
165 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
166 const struct sctp_association *asoc,
167 const union sctp_subtype type, void *arg,
168 struct sctp_cmd_seq *commands);
170 /* Small helper function that checks if the chunk length
171 * is of the appropriate length. The 'required_length' argument
172 * is set to be the size of a specific chunk we are testing.
173 * Return Values: true = Valid length
174 * false = Invalid length
177 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
178 __u16 required_length)
180 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
182 /* Previously already marked? */
183 if (unlikely(chunk->pdiscard))
185 if (unlikely(chunk_length < required_length))
191 /* Check for format error in an ABORT chunk */
192 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
194 struct sctp_errhdr *err;
196 sctp_walk_errors(err, chunk->chunk_hdr);
198 return (void *)err == (void *)chunk->chunk_end;
201 /**********************************************************
202 * These are the state functions for handling chunk events.
203 **********************************************************/
206 * Process the final SHUTDOWN COMPLETE.
208 * Section: 4 (C) (diagram), 9.2
209 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
210 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
211 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
212 * should stop the T2-shutdown timer and remove all knowledge of the
213 * association (and thus the association enters the CLOSED state).
215 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
216 * C) Rules for packet carrying SHUTDOWN COMPLETE:
218 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
219 * if the Verification Tag field of the packet matches its own tag and
220 * the T bit is not set
222 * it is set to its peer's tag and the T bit is set in the Chunk
224 * Otherwise, the receiver MUST silently discard the packet
225 * and take no further action. An endpoint MUST ignore the
226 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
229 * (endpoint, asoc, chunk)
232 * (asoc, reply_msg, msg_up, timers, counters)
234 * The return value is the disposition of the chunk.
236 enum sctp_disposition sctp_sf_do_4_C(struct net *net,
237 const struct sctp_endpoint *ep,
238 const struct sctp_association *asoc,
239 const union sctp_subtype type,
240 void *arg, struct sctp_cmd_seq *commands)
242 struct sctp_chunk *chunk = arg;
243 struct sctp_ulpevent *ev;
245 if (!sctp_vtag_verify_either(chunk, asoc))
246 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
248 /* RFC 2960 6.10 Bundling
250 * An endpoint MUST NOT bundle INIT, INIT ACK or
251 * SHUTDOWN COMPLETE with any other chunks.
253 if (!chunk->singleton)
254 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
256 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
257 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
258 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
261 /* RFC 2960 10.2 SCTP-to-ULP
263 * H) SHUTDOWN COMPLETE notification
265 * When SCTP completes the shutdown procedures (section 9.2) this
266 * notification is passed to the upper layer.
268 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
269 0, 0, 0, NULL, GFP_ATOMIC);
271 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
274 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
275 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
276 * not the chunk should be discarded. If the endpoint is in
277 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
278 * T2-shutdown timer and remove all knowledge of the
279 * association (and thus the association enters the CLOSED
282 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
283 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
285 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
286 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
288 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
289 SCTP_STATE(SCTP_STATE_CLOSED));
291 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
292 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
294 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
296 return SCTP_DISPOSITION_DELETE_TCB;
300 * Respond to a normal INIT chunk.
301 * We are the side that is being asked for an association.
303 * Section: 5.1 Normal Establishment of an Association, B
304 * B) "Z" shall respond immediately with an INIT ACK chunk. The
305 * destination IP address of the INIT ACK MUST be set to the source
306 * IP address of the INIT to which this INIT ACK is responding. In
307 * the response, besides filling in other parameters, "Z" must set the
308 * Verification Tag field to Tag_A, and also provide its own
309 * Verification Tag (Tag_Z) in the Initiate Tag field.
311 * Verification Tag: Must be 0.
314 * (endpoint, asoc, chunk)
317 * (asoc, reply_msg, msg_up, timers, counters)
319 * The return value is the disposition of the chunk.
321 enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
322 const struct sctp_endpoint *ep,
323 const struct sctp_association *asoc,
324 const union sctp_subtype type,
326 struct sctp_cmd_seq *commands)
328 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
329 struct sctp_unrecognized_param *unk_param;
330 struct sctp_association *new_asoc;
331 struct sctp_packet *packet;
335 * An endpoint MUST NOT bundle INIT, INIT ACK or
336 * SHUTDOWN COMPLETE with any other chunks.
339 * Furthermore, we require that the receiver of an INIT chunk MUST
340 * enforce these rules by silently discarding an arriving packet
341 * with an INIT chunk that is bundled with other chunks.
343 if (!chunk->singleton)
344 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
346 /* Make sure that the INIT chunk has a valid length.
347 * Normally, this would cause an ABORT with a Protocol Violation
348 * error, but since we don't have an association, we'll
349 * just discard the packet.
351 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
352 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
354 /* If the packet is an OOTB packet which is temporarily on the
355 * control endpoint, respond with an ABORT.
357 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
358 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
359 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
362 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
365 if (chunk->sctp_hdr->vtag != 0)
366 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
368 /* If the INIT is coming toward a closing socket, we'll send back
369 * and ABORT. Essentially, this catches the race of INIT being
370 * backloged to the socket at the same time as the user isses close().
371 * Since the socket and all its associations are going away, we
372 * can treat this OOTB
374 if (sctp_sstate(ep->base.sk, CLOSING))
375 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
377 /* Verify the INIT chunk before processing it. */
379 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
380 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
382 /* This chunk contains fatal error. It is to be discarded.
383 * Send an ABORT, with causes if there is any.
386 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
387 (__u8 *)(err_chunk->chunk_hdr) +
388 sizeof(struct sctp_chunkhdr),
389 ntohs(err_chunk->chunk_hdr->length) -
390 sizeof(struct sctp_chunkhdr));
392 sctp_chunk_free(err_chunk);
395 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
396 SCTP_PACKET(packet));
397 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
398 return SCTP_DISPOSITION_CONSUME;
400 return SCTP_DISPOSITION_NOMEM;
403 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
408 /* Grab the INIT header. */
409 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
411 /* Tag the variable length parameters. */
412 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
414 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
418 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
419 sctp_scope(sctp_source(chunk)),
423 /* The call, sctp_process_init(), can fail on memory allocation. */
424 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
425 (struct sctp_init_chunk *)chunk->chunk_hdr,
429 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
431 /* If there are errors need to be reported for unknown parameters,
432 * make sure to reserve enough room in the INIT ACK for them.
436 len = ntohs(err_chunk->chunk_hdr->length) -
437 sizeof(struct sctp_chunkhdr);
439 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
443 /* If there are errors need to be reported for unknown parameters,
444 * include them in the outgoing INIT ACK as "Unrecognized parameter"
448 /* Get the "Unrecognized parameter" parameter(s) out of the
449 * ERROR chunk generated by sctp_verify_init(). Since the
450 * error cause code for "unknown parameter" and the
451 * "Unrecognized parameter" type is the same, we can
452 * construct the parameters in INIT ACK by copying the
455 unk_param = (struct sctp_unrecognized_param *)
456 ((__u8 *)(err_chunk->chunk_hdr) +
457 sizeof(struct sctp_chunkhdr));
458 /* Replace the cause code with the "Unrecognized parameter"
461 sctp_addto_chunk(repl, len, unk_param);
462 sctp_chunk_free(err_chunk);
465 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
467 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
470 * Note: After sending out INIT ACK with the State Cookie parameter,
471 * "Z" MUST NOT allocate any resources, nor keep any states for the
472 * new association. Otherwise, "Z" will be vulnerable to resource
475 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
477 return SCTP_DISPOSITION_DELETE_TCB;
480 sctp_association_free(new_asoc);
483 sctp_chunk_free(err_chunk);
484 return SCTP_DISPOSITION_NOMEM;
488 * Respond to a normal INIT ACK chunk.
489 * We are the side that is initiating the association.
491 * Section: 5.1 Normal Establishment of an Association, C
492 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
493 * timer and leave COOKIE-WAIT state. "A" shall then send the State
494 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
495 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
497 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
498 * DATA chunks, but it MUST be the first chunk in the packet and
499 * until the COOKIE ACK is returned the sender MUST NOT send any
500 * other packets to the peer.
502 * Verification Tag: 3.3.3
503 * If the value of the Initiate Tag in a received INIT ACK chunk is
504 * found to be 0, the receiver MUST treat it as an error and close the
505 * association by transmitting an ABORT.
508 * (endpoint, asoc, chunk)
511 * (asoc, reply_msg, msg_up, timers, counters)
513 * The return value is the disposition of the chunk.
515 enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
516 const struct sctp_endpoint *ep,
517 const struct sctp_association *asoc,
518 const union sctp_subtype type,
520 struct sctp_cmd_seq *commands)
522 struct sctp_init_chunk *initchunk;
523 struct sctp_chunk *chunk = arg;
524 struct sctp_chunk *err_chunk;
525 struct sctp_packet *packet;
527 if (!sctp_vtag_verify(chunk, asoc))
528 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
531 * An endpoint MUST NOT bundle INIT, INIT ACK or
532 * SHUTDOWN COMPLETE with any other chunks.
534 if (!chunk->singleton)
535 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
537 /* Make sure that the INIT-ACK chunk has a valid length */
538 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
539 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
541 /* Grab the INIT header. */
542 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
544 /* Verify the INIT chunk before processing it. */
546 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
547 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
550 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
552 /* This chunk contains fatal error. It is to be discarded.
553 * Send an ABORT, with causes. If there are no causes,
554 * then there wasn't enough memory. Just terminate
558 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
559 (__u8 *)(err_chunk->chunk_hdr) +
560 sizeof(struct sctp_chunkhdr),
561 ntohs(err_chunk->chunk_hdr->length) -
562 sizeof(struct sctp_chunkhdr));
564 sctp_chunk_free(err_chunk);
567 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
568 SCTP_PACKET(packet));
569 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
570 error = SCTP_ERROR_INV_PARAM;
574 /* SCTP-AUTH, Section 6.3:
575 * It should be noted that if the receiver wants to tear
576 * down an association in an authenticated way only, the
577 * handling of malformed packets should not result in
578 * tearing down the association.
580 * This means that if we only want to abort associations
581 * in an authenticated way (i.e AUTH+ABORT), then we
582 * can't destroy this association just because the packet
585 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
586 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
588 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
589 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
590 asoc, chunk->transport);
593 /* Tag the variable length parameters. Note that we never
594 * convert the parameters in an INIT chunk.
596 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
598 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
600 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
601 SCTP_PEER_INIT(initchunk));
603 /* Reset init error count upon receipt of INIT-ACK. */
604 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
606 /* 5.1 C) "A" shall stop the T1-init timer and leave
607 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
608 * timer, and enter the COOKIE-ECHOED state.
610 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
611 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
612 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
613 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
614 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
615 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
617 /* SCTP-AUTH: genereate the assocition shared keys so that
618 * we can potentially signe the COOKIE-ECHO.
620 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
622 /* 5.1 C) "A" shall then send the State Cookie received in the
623 * INIT ACK chunk in a COOKIE ECHO chunk, ...
625 /* If there is any errors to report, send the ERROR chunk generated
626 * for unknown parameters as well.
628 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
629 SCTP_CHUNK(err_chunk));
631 return SCTP_DISPOSITION_CONSUME;
634 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
635 const struct sctp_association *asoc)
637 struct sctp_chunk auth;
639 if (!chunk->auth_chunk)
642 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
643 * is supposed to be authenticated and we have to do delayed
644 * authentication. We've just recreated the association using
645 * the information in the cookie and now it's much easier to
646 * do the authentication.
649 /* Make sure that we and the peer are AUTH capable */
650 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
653 /* set-up our fake chunk so that we can process it */
654 auth.skb = chunk->auth_chunk;
655 auth.asoc = chunk->asoc;
656 auth.sctp_hdr = chunk->sctp_hdr;
657 auth.chunk_hdr = (struct sctp_chunkhdr *)
658 skb_push(chunk->auth_chunk,
659 sizeof(struct sctp_chunkhdr));
660 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
661 auth.transport = chunk->transport;
663 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
667 * Respond to a normal COOKIE ECHO chunk.
668 * We are the side that is being asked for an association.
670 * Section: 5.1 Normal Establishment of an Association, D
671 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
672 * with a COOKIE ACK chunk after building a TCB and moving to
673 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
674 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
675 * chunk MUST be the first chunk in the packet.
677 * IMPLEMENTATION NOTE: An implementation may choose to send the
678 * Communication Up notification to the SCTP user upon reception
679 * of a valid COOKIE ECHO chunk.
681 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
682 * D) Rules for packet carrying a COOKIE ECHO
684 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
685 * Initial Tag received in the INIT ACK.
687 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
690 * (endpoint, asoc, chunk)
693 * (asoc, reply_msg, msg_up, timers, counters)
695 * The return value is the disposition of the chunk.
697 enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
698 const struct sctp_endpoint *ep,
699 const struct sctp_association *asoc,
700 const union sctp_subtype type,
702 struct sctp_cmd_seq *commands)
704 struct sctp_ulpevent *ev, *ai_ev = NULL;
705 struct sctp_association *new_asoc;
706 struct sctp_init_chunk *peer_init;
707 struct sctp_chunk *chunk = arg;
708 struct sctp_chunk *err_chk_p;
709 struct sctp_chunk *repl;
713 if (asoc && !sctp_vtag_verify(chunk, asoc))
714 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
716 /* If the packet is an OOTB packet which is temporarily on the
717 * control endpoint, respond with an ABORT.
719 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
720 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
721 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
724 /* Make sure that the COOKIE_ECHO chunk has a valid length.
725 * In this case, we check that we have enough for at least a
726 * chunk header. More detailed verification is done
727 * in sctp_unpack_cookie().
729 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
730 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
733 /* If the endpoint is not listening or if the number of associations
734 * on the TCP-style socket exceed the max backlog, respond with an
738 if (!sctp_sstate(sk, LISTENING) ||
739 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
740 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
742 /* "Decode" the chunk. We have no optional parameters so we
745 chunk->subh.cookie_hdr =
746 (struct sctp_signed_cookie *)chunk->skb->data;
747 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
748 sizeof(struct sctp_chunkhdr)))
751 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
752 * "Z" will reply with a COOKIE ACK chunk after building a TCB
753 * and moving to the ESTABLISHED state.
755 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
759 * If the re-build failed, what is the proper error path
762 * [We should abort the association. --piggy]
765 /* FIXME: Several errors are possible. A bad cookie should
766 * be silently discarded, but think about logging it too.
769 case -SCTP_IERROR_NOMEM:
772 case -SCTP_IERROR_STALE_COOKIE:
773 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
775 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
777 case -SCTP_IERROR_BAD_SIG:
779 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
784 /* Delay state machine commands until later.
786 * Re-build the bind address for the association is done in
787 * the sctp_unpack_cookie() already.
789 /* This is a brand-new association, so these are not yet side
790 * effects--it is safe to run them here.
792 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
794 if (!sctp_process_init(new_asoc, chunk,
795 &chunk->subh.cookie_hdr->c.peer_addr,
796 peer_init, GFP_ATOMIC))
799 /* SCTP-AUTH: Now that we've populate required fields in
800 * sctp_process_init, set up the assocaition shared keys as
801 * necessary so that we can potentially authenticate the ACK
803 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
807 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
808 sctp_association_free(new_asoc);
809 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
812 repl = sctp_make_cookie_ack(new_asoc, chunk);
816 /* RFC 2960 5.1 Normal Establishment of an Association
818 * D) IMPLEMENTATION NOTE: An implementation may choose to
819 * send the Communication Up notification to the SCTP user
820 * upon reception of a valid COOKIE ECHO chunk.
822 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
823 new_asoc->c.sinit_num_ostreams,
824 new_asoc->c.sinit_max_instreams,
829 /* Sockets API Draft Section 5.3.1.6
830 * When a peer sends a Adaptation Layer Indication parameter , SCTP
831 * delivers this notification to inform the application that of the
832 * peers requested adaptation layer.
834 if (new_asoc->peer.adaptation_ind) {
835 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
841 /* Add all the state machine commands now since we've created
842 * everything. This way we don't introduce memory corruptions
843 * during side-effect processing and correclty count established
846 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
847 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
848 SCTP_STATE(SCTP_STATE_ESTABLISHED));
849 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
850 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
851 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
853 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
854 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
855 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
857 /* This will send the COOKIE ACK */
858 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
860 /* Queue the ASSOC_CHANGE event */
861 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
863 /* Send up the Adaptation Layer Indication event */
865 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
866 SCTP_ULPEVENT(ai_ev));
868 return SCTP_DISPOSITION_CONSUME;
871 sctp_ulpevent_free(ev);
873 sctp_chunk_free(repl);
875 sctp_association_free(new_asoc);
877 return SCTP_DISPOSITION_NOMEM;
881 * Respond to a normal COOKIE ACK chunk.
882 * We are the side that is asking for an association.
884 * RFC 2960 5.1 Normal Establishment of an Association
886 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
887 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
888 * timer. It may also notify its ULP about the successful
889 * establishment of the association with a Communication Up
890 * notification (see Section 10).
894 * (endpoint, asoc, chunk)
897 * (asoc, reply_msg, msg_up, timers, counters)
899 * The return value is the disposition of the chunk.
901 enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
902 const struct sctp_endpoint *ep,
903 const struct sctp_association *asoc,
904 const union sctp_subtype type,
906 struct sctp_cmd_seq *commands)
908 struct sctp_chunk *chunk = arg;
909 struct sctp_ulpevent *ev;
911 if (!sctp_vtag_verify(chunk, asoc))
912 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
914 /* Verify that the chunk length for the COOKIE-ACK is OK.
915 * If we don't do this, any bundled chunks may be junked.
917 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
918 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
921 /* Reset init error count upon receipt of COOKIE-ACK,
922 * to avoid problems with the managemement of this
923 * counter in stale cookie situations when a transition back
924 * from the COOKIE-ECHOED state to the COOKIE-WAIT
925 * state is performed.
927 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
929 /* RFC 2960 5.1 Normal Establishment of an Association
931 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
932 * from the COOKIE-ECHOED state to the ESTABLISHED state,
933 * stopping the T1-cookie timer.
935 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
936 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
937 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
938 SCTP_STATE(SCTP_STATE_ESTABLISHED));
939 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
940 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
941 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
942 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
943 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
944 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
946 /* It may also notify its ULP about the successful
947 * establishment of the association with a Communication Up
948 * notification (see Section 10).
950 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
951 0, asoc->c.sinit_num_ostreams,
952 asoc->c.sinit_max_instreams,
958 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
960 /* Sockets API Draft Section 5.3.1.6
961 * When a peer sends a Adaptation Layer Indication parameter , SCTP
962 * delivers this notification to inform the application that of the
963 * peers requested adaptation layer.
965 if (asoc->peer.adaptation_ind) {
966 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
970 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
974 return SCTP_DISPOSITION_CONSUME;
976 return SCTP_DISPOSITION_NOMEM;
979 /* Generate and sendout a heartbeat packet. */
980 static enum sctp_disposition sctp_sf_heartbeat(
981 const struct sctp_endpoint *ep,
982 const struct sctp_association *asoc,
983 const union sctp_subtype type,
985 struct sctp_cmd_seq *commands)
987 struct sctp_transport *transport = (struct sctp_transport *) arg;
988 struct sctp_chunk *reply;
990 /* Send a heartbeat to our peer. */
991 reply = sctp_make_heartbeat(asoc, transport);
993 return SCTP_DISPOSITION_NOMEM;
995 /* Set rto_pending indicating that an RTT measurement
996 * is started with this heartbeat chunk.
998 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
999 SCTP_TRANSPORT(transport));
1001 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1002 return SCTP_DISPOSITION_CONSUME;
1005 /* Generate a HEARTBEAT packet on the given transport. */
1006 enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1007 const struct sctp_endpoint *ep,
1008 const struct sctp_association *asoc,
1009 const union sctp_subtype type,
1011 struct sctp_cmd_seq *commands)
1013 struct sctp_transport *transport = (struct sctp_transport *) arg;
1015 if (asoc->overall_error_count >= asoc->max_retrans) {
1016 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1017 SCTP_ERROR(ETIMEDOUT));
1018 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1019 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1020 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1021 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1022 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1023 return SCTP_DISPOSITION_DELETE_TCB;
1027 * The Sender-specific Heartbeat Info field should normally include
1028 * information about the sender's current time when this HEARTBEAT
1029 * chunk is sent and the destination transport address to which this
1030 * HEARTBEAT is sent (see Section 8.3).
1033 if (transport->param_flags & SPP_HB_ENABLE) {
1034 if (SCTP_DISPOSITION_NOMEM ==
1035 sctp_sf_heartbeat(ep, asoc, type, arg,
1037 return SCTP_DISPOSITION_NOMEM;
1039 /* Set transport error counter and association error counter
1040 * when sending heartbeat.
1042 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1043 SCTP_TRANSPORT(transport));
1045 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1046 SCTP_TRANSPORT(transport));
1047 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1048 SCTP_TRANSPORT(transport));
1050 return SCTP_DISPOSITION_CONSUME;
1053 /* resend asoc strreset_chunk. */
1054 enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1055 const struct sctp_endpoint *ep,
1056 const struct sctp_association *asoc,
1057 const union sctp_subtype type,
1059 struct sctp_cmd_seq *commands)
1061 struct sctp_transport *transport = arg;
1063 if (asoc->overall_error_count >= asoc->max_retrans) {
1064 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1065 SCTP_ERROR(ETIMEDOUT));
1066 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1067 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1068 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1069 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1070 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1071 return SCTP_DISPOSITION_DELETE_TCB;
1074 sctp_chunk_hold(asoc->strreset_chunk);
1075 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1076 SCTP_CHUNK(asoc->strreset_chunk));
1077 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1079 return SCTP_DISPOSITION_CONSUME;
1083 * Process an heartbeat request.
1085 * Section: 8.3 Path Heartbeat
1086 * The receiver of the HEARTBEAT should immediately respond with a
1087 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1088 * from the received HEARTBEAT chunk.
1090 * Verification Tag: 8.5 Verification Tag [Normal verification]
1091 * When receiving an SCTP packet, the endpoint MUST ensure that the
1092 * value in the Verification Tag field of the received SCTP packet
1093 * matches its own Tag. If the received Verification Tag value does not
1094 * match the receiver's own tag value, the receiver shall silently
1095 * discard the packet and shall not process it any further except for
1096 * those cases listed in Section 8.5.1 below.
1099 * (endpoint, asoc, chunk)
1102 * (asoc, reply_msg, msg_up, timers, counters)
1104 * The return value is the disposition of the chunk.
1106 enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1107 const struct sctp_endpoint *ep,
1108 const struct sctp_association *asoc,
1109 const union sctp_subtype type,
1110 void *arg, struct sctp_cmd_seq *commands)
1112 struct sctp_paramhdr *param_hdr;
1113 struct sctp_chunk *chunk = arg;
1114 struct sctp_chunk *reply;
1117 if (!sctp_vtag_verify(chunk, asoc))
1118 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1120 /* Make sure that the HEARTBEAT chunk has a valid length. */
1121 if (!sctp_chunk_length_valid(chunk,
1122 sizeof(struct sctp_heartbeat_chunk)))
1123 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1126 /* 8.3 The receiver of the HEARTBEAT should immediately
1127 * respond with a HEARTBEAT ACK that contains the Heartbeat
1128 * Information field copied from the received HEARTBEAT chunk.
1130 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1131 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1132 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1134 if (ntohs(param_hdr->length) > paylen)
1135 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1136 param_hdr, commands);
1138 if (!pskb_pull(chunk->skb, paylen))
1141 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1145 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1146 return SCTP_DISPOSITION_CONSUME;
1149 return SCTP_DISPOSITION_NOMEM;
1153 * Process the returning HEARTBEAT ACK.
1155 * Section: 8.3 Path Heartbeat
1156 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1157 * should clear the error counter of the destination transport
1158 * address to which the HEARTBEAT was sent, and mark the destination
1159 * transport address as active if it is not so marked. The endpoint may
1160 * optionally report to the upper layer when an inactive destination
1161 * address is marked as active due to the reception of the latest
1162 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1163 * clear the association overall error count as well (as defined
1166 * The receiver of the HEARTBEAT ACK should also perform an RTT
1167 * measurement for that destination transport address using the time
1168 * value carried in the HEARTBEAT ACK chunk.
1170 * Verification Tag: 8.5 Verification Tag [Normal verification]
1173 * (endpoint, asoc, chunk)
1176 * (asoc, reply_msg, msg_up, timers, counters)
1178 * The return value is the disposition of the chunk.
1180 enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1181 const struct sctp_endpoint *ep,
1182 const struct sctp_association *asoc,
1183 const union sctp_subtype type,
1185 struct sctp_cmd_seq *commands)
1187 struct sctp_sender_hb_info *hbinfo;
1188 struct sctp_chunk *chunk = arg;
1189 struct sctp_transport *link;
1190 unsigned long max_interval;
1191 union sctp_addr from_addr;
1193 if (!sctp_vtag_verify(chunk, asoc))
1194 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1196 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1197 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1199 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1202 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1203 /* Make sure that the length of the parameter is what we expect */
1204 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1205 return SCTP_DISPOSITION_DISCARD;
1207 from_addr = hbinfo->daddr;
1208 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1210 /* This should never happen, but lets log it if so. */
1211 if (unlikely(!link)) {
1212 if (from_addr.sa.sa_family == AF_INET6) {
1213 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1216 &from_addr.v6.sin6_addr);
1218 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1221 &from_addr.v4.sin_addr.s_addr);
1223 return SCTP_DISPOSITION_DISCARD;
1226 /* Validate the 64-bit random nonce. */
1227 if (hbinfo->hb_nonce != link->hb_nonce)
1228 return SCTP_DISPOSITION_DISCARD;
1230 max_interval = link->hbinterval + link->rto;
1232 /* Check if the timestamp looks valid. */
1233 if (time_after(hbinfo->sent_at, jiffies) ||
1234 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1235 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1236 "for transport:%p\n", __func__, link);
1238 return SCTP_DISPOSITION_DISCARD;
1241 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1242 * the HEARTBEAT should clear the error counter of the
1243 * destination transport address to which the HEARTBEAT was
1244 * sent and mark the destination transport address as active if
1245 * it is not so marked.
1247 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1249 return SCTP_DISPOSITION_CONSUME;
1252 /* Helper function to send out an abort for the restart
1255 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1256 struct sctp_chunk *init,
1257 struct sctp_cmd_seq *commands)
1259 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1260 union sctp_addr_param *addrparm;
1261 struct sctp_errhdr *errhdr;
1262 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1263 struct sctp_endpoint *ep;
1264 struct sctp_packet *pkt;
1267 /* Build the error on the stack. We are way to malloc crazy
1268 * throughout the code today.
1270 errhdr = (struct sctp_errhdr *)buffer;
1271 addrparm = (union sctp_addr_param *)errhdr->variable;
1273 /* Copy into a parm format. */
1274 len = af->to_addr_param(ssa, addrparm);
1275 len += sizeof(*errhdr);
1277 errhdr->cause = SCTP_ERROR_RESTART;
1278 errhdr->length = htons(len);
1280 /* Assign to the control socket. */
1281 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1283 /* Association is NULL since this may be a restart attack and we
1284 * want to send back the attacker's vtag.
1286 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1290 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1292 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1294 /* Discard the rest of the inbound packet. */
1295 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1298 /* Even if there is no memory, treat as a failure so
1299 * the packet will get dropped.
1304 static bool list_has_sctp_addr(const struct list_head *list,
1305 union sctp_addr *ipaddr)
1307 struct sctp_transport *addr;
1309 list_for_each_entry(addr, list, transports) {
1310 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1316 /* A restart is occurring, check to make sure no new addresses
1317 * are being added as we may be under a takeover attack.
1319 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1320 const struct sctp_association *asoc,
1321 struct sctp_chunk *init,
1322 struct sctp_cmd_seq *commands)
1324 struct net *net = sock_net(new_asoc->base.sk);
1325 struct sctp_transport *new_addr;
1328 /* Implementor's Guide - Section 5.2.2
1330 * Before responding the endpoint MUST check to see if the
1331 * unexpected INIT adds new addresses to the association. If new
1332 * addresses are added to the association, the endpoint MUST respond
1336 /* Search through all current addresses and make sure
1337 * we aren't adding any new ones.
1339 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1341 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1342 &new_addr->ipaddr)) {
1343 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1350 /* Return success if all addresses were found. */
1354 /* Populate the verification/tie tags based on overlapping INIT
1357 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1359 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1360 const struct sctp_association *asoc)
1362 switch (asoc->state) {
1364 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1366 case SCTP_STATE_COOKIE_WAIT:
1367 new_asoc->c.my_vtag = asoc->c.my_vtag;
1368 new_asoc->c.my_ttag = asoc->c.my_vtag;
1369 new_asoc->c.peer_ttag = 0;
1372 case SCTP_STATE_COOKIE_ECHOED:
1373 new_asoc->c.my_vtag = asoc->c.my_vtag;
1374 new_asoc->c.my_ttag = asoc->c.my_vtag;
1375 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1378 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1379 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1382 new_asoc->c.my_ttag = asoc->c.my_vtag;
1383 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1387 /* Other parameters for the endpoint SHOULD be copied from the
1388 * existing parameters of the association (e.g. number of
1389 * outbound streams) into the INIT ACK and cookie.
1391 new_asoc->rwnd = asoc->rwnd;
1392 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1393 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1394 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1398 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1401 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1403 * Returns value representing action to be taken. These action values
1404 * correspond to Action/Description values in RFC 2960, Table 2.
1406 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1407 const struct sctp_association *asoc)
1409 /* In this case, the peer may have restarted. */
1410 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1411 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1412 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1413 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1416 /* Collision case B. */
1417 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1418 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1419 (0 == asoc->c.peer_vtag))) {
1423 /* Collision case D. */
1424 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1425 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1428 /* Collision case C. */
1429 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1430 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1431 (0 == new_asoc->c.my_ttag) &&
1432 (0 == new_asoc->c.peer_ttag))
1435 /* No match to any of the special cases; discard this packet. */
1439 /* Common helper routine for both duplicate and simulataneous INIT
1442 static enum sctp_disposition sctp_sf_do_unexpected_init(
1444 const struct sctp_endpoint *ep,
1445 const struct sctp_association *asoc,
1446 const union sctp_subtype type,
1448 struct sctp_cmd_seq *commands)
1450 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1451 struct sctp_unrecognized_param *unk_param;
1452 struct sctp_association *new_asoc;
1453 enum sctp_disposition retval;
1454 struct sctp_packet *packet;
1458 * An endpoint MUST NOT bundle INIT, INIT ACK or
1459 * SHUTDOWN COMPLETE with any other chunks.
1462 * Furthermore, we require that the receiver of an INIT chunk MUST
1463 * enforce these rules by silently discarding an arriving packet
1464 * with an INIT chunk that is bundled with other chunks.
1466 if (!chunk->singleton)
1467 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1469 /* Make sure that the INIT chunk has a valid length. */
1470 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1471 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1473 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1476 if (chunk->sctp_hdr->vtag != 0)
1477 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1479 /* Grab the INIT header. */
1480 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1482 /* Tag the variable length parameters. */
1483 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1485 /* Verify the INIT chunk before processing it. */
1487 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1488 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1490 /* This chunk contains fatal error. It is to be discarded.
1491 * Send an ABORT, with causes if there is any.
1494 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1495 (__u8 *)(err_chunk->chunk_hdr) +
1496 sizeof(struct sctp_chunkhdr),
1497 ntohs(err_chunk->chunk_hdr->length) -
1498 sizeof(struct sctp_chunkhdr));
1501 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1502 SCTP_PACKET(packet));
1503 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1504 retval = SCTP_DISPOSITION_CONSUME;
1506 retval = SCTP_DISPOSITION_NOMEM;
1510 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1516 * Other parameters for the endpoint SHOULD be copied from the
1517 * existing parameters of the association (e.g. number of
1518 * outbound streams) into the INIT ACK and cookie.
1519 * FIXME: We are copying parameters from the endpoint not the
1522 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1526 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1527 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1530 /* In the outbound INIT ACK the endpoint MUST copy its current
1531 * Verification Tag and Peers Verification tag into a reserved
1532 * place (local tie-tag and per tie-tag) within the state cookie.
1534 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1535 (struct sctp_init_chunk *)chunk->chunk_hdr,
1539 /* Make sure no new addresses are being added during the
1540 * restart. Do not do this check for COOKIE-WAIT state,
1541 * since there are no peer addresses to check against.
1542 * Upon return an ABORT will have been sent if needed.
1544 if (!sctp_state(asoc, COOKIE_WAIT)) {
1545 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1547 retval = SCTP_DISPOSITION_CONSUME;
1552 sctp_tietags_populate(new_asoc, asoc);
1554 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1556 /* If there are errors need to be reported for unknown parameters,
1557 * make sure to reserve enough room in the INIT ACK for them.
1561 len = ntohs(err_chunk->chunk_hdr->length) -
1562 sizeof(struct sctp_chunkhdr);
1565 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1569 /* If there are errors need to be reported for unknown parameters,
1570 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1574 /* Get the "Unrecognized parameter" parameter(s) out of the
1575 * ERROR chunk generated by sctp_verify_init(). Since the
1576 * error cause code for "unknown parameter" and the
1577 * "Unrecognized parameter" type is the same, we can
1578 * construct the parameters in INIT ACK by copying the
1579 * ERROR causes over.
1581 unk_param = (struct sctp_unrecognized_param *)
1582 ((__u8 *)(err_chunk->chunk_hdr) +
1583 sizeof(struct sctp_chunkhdr));
1584 /* Replace the cause code with the "Unrecognized parameter"
1587 sctp_addto_chunk(repl, len, unk_param);
1590 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1591 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1594 * Note: After sending out INIT ACK with the State Cookie parameter,
1595 * "Z" MUST NOT allocate any resources for this new association.
1596 * Otherwise, "Z" will be vulnerable to resource attacks.
1598 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1599 retval = SCTP_DISPOSITION_CONSUME;
1604 retval = SCTP_DISPOSITION_NOMEM;
1607 sctp_association_free(new_asoc);
1610 sctp_chunk_free(err_chunk);
1615 * Handle simultaneous INIT.
1616 * This means we started an INIT and then we got an INIT request from
1619 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1620 * This usually indicates an initialization collision, i.e., each
1621 * endpoint is attempting, at about the same time, to establish an
1622 * association with the other endpoint.
1624 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1625 * endpoint MUST respond with an INIT ACK using the same parameters it
1626 * sent in its original INIT chunk (including its Verification Tag,
1627 * unchanged). These original parameters are combined with those from the
1628 * newly received INIT chunk. The endpoint shall also generate a State
1629 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1630 * INIT to calculate the State Cookie.
1632 * After that, the endpoint MUST NOT change its state, the T1-init
1633 * timer shall be left running and the corresponding TCB MUST NOT be
1634 * destroyed. The normal procedures for handling State Cookies when
1635 * a TCB exists will resolve the duplicate INITs to a single association.
1637 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1638 * its Tie-Tags with the Tag information of itself and its peer (see
1639 * section 5.2.2 for a description of the Tie-Tags).
1641 * Verification Tag: Not explicit, but an INIT can not have a valid
1642 * verification tag, so we skip the check.
1645 * (endpoint, asoc, chunk)
1648 * (asoc, reply_msg, msg_up, timers, counters)
1650 * The return value is the disposition of the chunk.
1652 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1654 const struct sctp_endpoint *ep,
1655 const struct sctp_association *asoc,
1656 const union sctp_subtype type,
1658 struct sctp_cmd_seq *commands)
1660 /* Call helper to do the real work for both simulataneous and
1661 * duplicate INIT chunk handling.
1663 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1667 * Handle duplicated INIT messages. These are usually delayed
1670 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1671 * COOKIE-ECHOED and COOKIE-WAIT
1673 * Unless otherwise stated, upon reception of an unexpected INIT for
1674 * this association, the endpoint shall generate an INIT ACK with a
1675 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1676 * current Verification Tag and peer's Verification Tag into a reserved
1677 * place within the state cookie. We shall refer to these locations as
1678 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1679 * containing this INIT ACK MUST carry a Verification Tag value equal to
1680 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1681 * MUST contain a new Initiation Tag (randomly generated see Section
1682 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1683 * existing parameters of the association (e.g. number of outbound
1684 * streams) into the INIT ACK and cookie.
1686 * After sending out the INIT ACK, the endpoint shall take no further
1687 * actions, i.e., the existing association, including its current state,
1688 * and the corresponding TCB MUST NOT be changed.
1690 * Note: Only when a TCB exists and the association is not in a COOKIE-
1691 * WAIT state are the Tie-Tags populated. For a normal association INIT
1692 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1693 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1694 * State Cookie are populated as specified in section 5.2.1.
1696 * Verification Tag: Not specified, but an INIT has no way of knowing
1697 * what the verification tag could be, so we ignore it.
1700 * (endpoint, asoc, chunk)
1703 * (asoc, reply_msg, msg_up, timers, counters)
1705 * The return value is the disposition of the chunk.
1707 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1709 const struct sctp_endpoint *ep,
1710 const struct sctp_association *asoc,
1711 const union sctp_subtype type,
1713 struct sctp_cmd_seq *commands)
1715 /* Call helper to do the real work for both simulataneous and
1716 * duplicate INIT chunk handling.
1718 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1723 * Unexpected INIT-ACK handler.
1726 * If an INIT ACK received by an endpoint in any state other than the
1727 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1728 * An unexpected INIT ACK usually indicates the processing of an old or
1729 * duplicated INIT chunk.
1731 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1733 const struct sctp_endpoint *ep,
1734 const struct sctp_association *asoc,
1735 const union sctp_subtype type,
1737 struct sctp_cmd_seq *commands)
1739 /* Per the above section, we'll discard the chunk if we have an
1740 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1742 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1743 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1745 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1748 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1751 * A) In this case, the peer may have restarted.
1753 static enum sctp_disposition sctp_sf_do_dupcook_a(
1755 const struct sctp_endpoint *ep,
1756 const struct sctp_association *asoc,
1757 struct sctp_chunk *chunk,
1758 struct sctp_cmd_seq *commands,
1759 struct sctp_association *new_asoc)
1761 struct sctp_init_chunk *peer_init;
1762 enum sctp_disposition disposition;
1763 struct sctp_ulpevent *ev;
1764 struct sctp_chunk *repl;
1765 struct sctp_chunk *err;
1767 /* new_asoc is a brand-new association, so these are not yet
1768 * side effects--it is safe to run them here.
1770 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1772 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1776 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1777 return SCTP_DISPOSITION_DISCARD;
1779 /* Make sure no new addresses are being added during the
1780 * restart. Though this is a pretty complicated attack
1781 * since you'd have to get inside the cookie.
1783 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1784 return SCTP_DISPOSITION_CONSUME;
1786 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1787 * the peer has restarted (Action A), it MUST NOT setup a new
1788 * association but instead resend the SHUTDOWN ACK and send an ERROR
1789 * chunk with a "Cookie Received while Shutting Down" error cause to
1792 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1793 disposition = __sctp_sf_do_9_2_reshutack(net, ep, asoc,
1794 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1796 if (SCTP_DISPOSITION_NOMEM == disposition)
1799 err = sctp_make_op_error(asoc, chunk,
1800 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1803 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1806 return SCTP_DISPOSITION_CONSUME;
1809 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1810 * data. Consider the optional choice of resending of this data.
1812 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1813 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1814 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1815 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1817 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1818 * and ASCONF-ACK cache.
1820 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1821 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1822 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1824 repl = sctp_make_cookie_ack(new_asoc, chunk);
1828 /* Report association restart to upper layer. */
1829 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1830 new_asoc->c.sinit_num_ostreams,
1831 new_asoc->c.sinit_max_instreams,
1836 /* Update the content of current association. */
1837 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1838 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1839 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1840 sctp_state(asoc, SHUTDOWN_SENT)) &&
1841 (sctp_sstate(asoc->base.sk, CLOSING) ||
1842 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1843 /* If the socket has been closed by user, don't
1844 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1845 * bundled with COOKIE_ACK.
1847 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1848 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1849 SCTP_ST_CHUNK(0), NULL,
1852 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1853 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1854 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1856 return SCTP_DISPOSITION_CONSUME;
1859 sctp_chunk_free(repl);
1861 return SCTP_DISPOSITION_NOMEM;
1864 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1867 * B) In this case, both sides may be attempting to start an association
1868 * at about the same time but the peer endpoint started its INIT
1869 * after responding to the local endpoint's INIT
1871 /* This case represents an initialization collision. */
1872 static enum sctp_disposition sctp_sf_do_dupcook_b(
1874 const struct sctp_endpoint *ep,
1875 const struct sctp_association *asoc,
1876 struct sctp_chunk *chunk,
1877 struct sctp_cmd_seq *commands,
1878 struct sctp_association *new_asoc)
1880 struct sctp_init_chunk *peer_init;
1881 struct sctp_chunk *repl;
1883 /* new_asoc is a brand-new association, so these are not yet
1884 * side effects--it is safe to run them here.
1886 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1887 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1891 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1892 return SCTP_DISPOSITION_DISCARD;
1894 /* Update the content of current association. */
1895 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1896 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1897 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1898 if (asoc->state < SCTP_STATE_ESTABLISHED)
1899 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1900 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1902 repl = sctp_make_cookie_ack(new_asoc, chunk);
1906 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1908 /* RFC 2960 5.1 Normal Establishment of an Association
1910 * D) IMPLEMENTATION NOTE: An implementation may choose to
1911 * send the Communication Up notification to the SCTP user
1912 * upon reception of a valid COOKIE ECHO chunk.
1914 * Sadly, this needs to be implemented as a side-effect, because
1915 * we are not guaranteed to have set the association id of the real
1916 * association and so these notifications need to be delayed until
1917 * the association id is allocated.
1920 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1922 /* Sockets API Draft Section 5.3.1.6
1923 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1924 * delivers this notification to inform the application that of the
1925 * peers requested adaptation layer.
1927 * This also needs to be done as a side effect for the same reason as
1930 if (asoc->peer.adaptation_ind)
1931 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1933 return SCTP_DISPOSITION_CONSUME;
1936 return SCTP_DISPOSITION_NOMEM;
1939 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1942 * C) In this case, the local endpoint's cookie has arrived late.
1943 * Before it arrived, the local endpoint sent an INIT and received an
1944 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1945 * but a new tag of its own.
1947 /* This case represents an initialization collision. */
1948 static enum sctp_disposition sctp_sf_do_dupcook_c(
1950 const struct sctp_endpoint *ep,
1951 const struct sctp_association *asoc,
1952 struct sctp_chunk *chunk,
1953 struct sctp_cmd_seq *commands,
1954 struct sctp_association *new_asoc)
1956 /* The cookie should be silently discarded.
1957 * The endpoint SHOULD NOT change states and should leave
1958 * any timers running.
1960 return SCTP_DISPOSITION_DISCARD;
1963 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1967 * D) When both local and remote tags match the endpoint should always
1968 * enter the ESTABLISHED state, if it has not already done so.
1970 /* This case represents an initialization collision. */
1971 static enum sctp_disposition sctp_sf_do_dupcook_d(
1973 const struct sctp_endpoint *ep,
1974 const struct sctp_association *asoc,
1975 struct sctp_chunk *chunk,
1976 struct sctp_cmd_seq *commands,
1977 struct sctp_association *new_asoc)
1979 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1980 struct sctp_chunk *repl;
1982 /* Clarification from Implementor's Guide:
1983 * D) When both local and remote tags match the endpoint should
1984 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1985 * It should stop any cookie timer that may be running and send
1989 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1990 return SCTP_DISPOSITION_DISCARD;
1992 /* Don't accidentally move back into established state. */
1993 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1994 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1995 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1996 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1997 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1998 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1999 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2002 /* RFC 2960 5.1 Normal Establishment of an Association
2004 * D) IMPLEMENTATION NOTE: An implementation may choose
2005 * to send the Communication Up notification to the
2006 * SCTP user upon reception of a valid COOKIE
2009 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2011 asoc->c.sinit_num_ostreams,
2012 asoc->c.sinit_max_instreams,
2017 /* Sockets API Draft Section 5.3.1.6
2018 * When a peer sends a Adaptation Layer Indication parameter,
2019 * SCTP delivers this notification to inform the application
2020 * that of the peers requested adaptation layer.
2022 if (asoc->peer.adaptation_ind) {
2023 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2031 repl = sctp_make_cookie_ack(asoc, chunk);
2035 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2038 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2041 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2042 SCTP_ULPEVENT(ai_ev));
2044 return SCTP_DISPOSITION_CONSUME;
2048 sctp_ulpevent_free(ai_ev);
2050 sctp_ulpevent_free(ev);
2051 return SCTP_DISPOSITION_NOMEM;
2055 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2056 * chunk was retransmitted and then delayed in the network.
2058 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2060 * Verification Tag: None. Do cookie validation.
2063 * (endpoint, asoc, chunk)
2066 * (asoc, reply_msg, msg_up, timers, counters)
2068 * The return value is the disposition of the chunk.
2070 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2072 const struct sctp_endpoint *ep,
2073 const struct sctp_association *asoc,
2074 const union sctp_subtype type,
2076 struct sctp_cmd_seq *commands)
2078 struct sctp_association *new_asoc;
2079 struct sctp_chunk *chunk = arg;
2080 enum sctp_disposition retval;
2081 struct sctp_chunk *err_chk_p;
2085 /* Make sure that the chunk has a valid length from the protocol
2086 * perspective. In this case check to make sure we have at least
2087 * enough for the chunk header. Cookie length verification is
2090 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr))) {
2091 if (!sctp_vtag_verify(chunk, asoc))
2093 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg, commands);
2096 /* "Decode" the chunk. We have no optional parameters so we
2097 * are in good shape.
2099 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2100 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2101 sizeof(struct sctp_chunkhdr)))
2104 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2105 * of a duplicate COOKIE ECHO match the Verification Tags of the
2106 * current association, consider the State Cookie valid even if
2107 * the lifespan is exceeded.
2109 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2113 * If the re-build failed, what is the proper error path
2116 * [We should abort the association. --piggy]
2119 /* FIXME: Several errors are possible. A bad cookie should
2120 * be silently discarded, but think about logging it too.
2123 case -SCTP_IERROR_NOMEM:
2126 case -SCTP_IERROR_STALE_COOKIE:
2127 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2129 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2130 case -SCTP_IERROR_BAD_SIG:
2132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2136 /* Set temp so that it won't be added into hashtable */
2139 /* Compare the tie_tag in cookie with the verification tag of
2140 * current association.
2142 action = sctp_tietags_compare(new_asoc, asoc);
2145 case 'A': /* Association restart. */
2146 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2150 case 'B': /* Collision case B. */
2151 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2155 case 'C': /* Collision case C. */
2156 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2160 case 'D': /* Collision case D. */
2161 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2165 default: /* Discard packet for all others. */
2166 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2170 /* Delete the tempory new association. */
2171 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2172 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2174 /* Restore association pointer to provide SCTP command interpeter
2175 * with a valid context in case it needs to manipulate
2177 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2178 SCTP_ASOC((struct sctp_association *)asoc));
2183 return SCTP_DISPOSITION_NOMEM;
2187 * Process an ABORT. (SHUTDOWN-PENDING state)
2189 * See sctp_sf_do_9_1_abort().
2191 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2193 const struct sctp_endpoint *ep,
2194 const struct sctp_association *asoc,
2195 const union sctp_subtype type,
2197 struct sctp_cmd_seq *commands)
2199 struct sctp_chunk *chunk = arg;
2201 if (!sctp_vtag_verify_either(chunk, asoc))
2202 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2204 /* Make sure that the ABORT chunk has a valid length.
2205 * Since this is an ABORT chunk, we have to discard it
2206 * because of the following text:
2207 * RFC 2960, Section 3.3.7
2208 * If an endpoint receives an ABORT with a format error or for an
2209 * association that doesn't exist, it MUST silently discard it.
2210 * Because the length is "invalid", we can't really discard just
2211 * as we do not know its true length. So, to be safe, discard the
2214 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2215 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2217 /* ADD-IP: Special case for ABORT chunks
2218 * F4) One special consideration is that ABORT Chunks arriving
2219 * destined to the IP address being deleted MUST be
2220 * ignored (see Section 5.3.1 for further details).
2222 if (SCTP_ADDR_DEL ==
2223 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2224 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2226 if (!sctp_err_chunk_valid(chunk))
2227 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2229 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2233 * Process an ABORT. (SHUTDOWN-SENT state)
2235 * See sctp_sf_do_9_1_abort().
2237 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2239 const struct sctp_endpoint *ep,
2240 const struct sctp_association *asoc,
2241 const union sctp_subtype type,
2243 struct sctp_cmd_seq *commands)
2245 struct sctp_chunk *chunk = arg;
2247 if (!sctp_vtag_verify_either(chunk, asoc))
2248 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2250 /* Make sure that the ABORT chunk has a valid length.
2251 * Since this is an ABORT chunk, we have to discard it
2252 * because of the following text:
2253 * RFC 2960, Section 3.3.7
2254 * If an endpoint receives an ABORT with a format error or for an
2255 * association that doesn't exist, it MUST silently discard it.
2256 * Because the length is "invalid", we can't really discard just
2257 * as we do not know its true length. So, to be safe, discard the
2260 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2261 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2263 /* ADD-IP: Special case for ABORT chunks
2264 * F4) One special consideration is that ABORT Chunks arriving
2265 * destined to the IP address being deleted MUST be
2266 * ignored (see Section 5.3.1 for further details).
2268 if (SCTP_ADDR_DEL ==
2269 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2270 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2272 if (!sctp_err_chunk_valid(chunk))
2273 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2275 /* Stop the T2-shutdown timer. */
2276 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2277 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2279 /* Stop the T5-shutdown guard timer. */
2280 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2281 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2283 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2287 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2289 * See sctp_sf_do_9_1_abort().
2291 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2293 const struct sctp_endpoint *ep,
2294 const struct sctp_association *asoc,
2295 const union sctp_subtype type,
2297 struct sctp_cmd_seq *commands)
2299 /* The same T2 timer, so we should be able to use
2300 * common function with the SHUTDOWN-SENT state.
2302 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2306 * Handle an Error received in COOKIE_ECHOED state.
2308 * Only handle the error type of stale COOKIE Error, the other errors will
2312 * (endpoint, asoc, chunk)
2315 * (asoc, reply_msg, msg_up, timers, counters)
2317 * The return value is the disposition of the chunk.
2319 enum sctp_disposition sctp_sf_cookie_echoed_err(
2321 const struct sctp_endpoint *ep,
2322 const struct sctp_association *asoc,
2323 const union sctp_subtype type,
2325 struct sctp_cmd_seq *commands)
2327 struct sctp_chunk *chunk = arg;
2328 struct sctp_errhdr *err;
2330 if (!sctp_vtag_verify(chunk, asoc))
2331 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2333 /* Make sure that the ERROR chunk has a valid length.
2334 * The parameter walking depends on this as well.
2336 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2337 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2340 /* Process the error here */
2341 /* FUTURE FIXME: When PR-SCTP related and other optional
2342 * parms are emitted, this will have to change to handle multiple
2345 sctp_walk_errors(err, chunk->chunk_hdr) {
2346 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2347 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2351 /* It is possible to have malformed error causes, and that
2352 * will cause us to end the walk early. However, since
2353 * we are discarding the packet, there should be no adverse
2356 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2360 * Handle a Stale COOKIE Error
2362 * Section: 5.2.6 Handle Stale COOKIE Error
2363 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2364 * one of the following three alternatives.
2366 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2367 * Preservative parameter requesting an extension to the lifetime of
2368 * the State Cookie. When calculating the time extension, an
2369 * implementation SHOULD use the RTT information measured based on the
2370 * previous COOKIE ECHO / ERROR exchange, and should add no more
2371 * than 1 second beyond the measured RTT, due to long State Cookie
2372 * lifetimes making the endpoint more subject to a replay attack.
2374 * Verification Tag: Not explicit, but safe to ignore.
2377 * (endpoint, asoc, chunk)
2380 * (asoc, reply_msg, msg_up, timers, counters)
2382 * The return value is the disposition of the chunk.
2384 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2386 const struct sctp_endpoint *ep,
2387 const struct sctp_association *asoc,
2388 const union sctp_subtype type,
2390 struct sctp_cmd_seq *commands)
2392 int attempts = asoc->init_err_counter + 1;
2393 struct sctp_chunk *chunk = arg, *reply;
2394 struct sctp_cookie_preserve_param bht;
2395 struct sctp_bind_addr *bp;
2396 struct sctp_errhdr *err;
2399 if (attempts > asoc->max_init_attempts) {
2400 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2401 SCTP_ERROR(ETIMEDOUT));
2402 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2403 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2404 return SCTP_DISPOSITION_DELETE_TCB;
2407 err = (struct sctp_errhdr *)(chunk->skb->data);
2409 /* When calculating the time extension, an implementation
2410 * SHOULD use the RTT information measured based on the
2411 * previous COOKIE ECHO / ERROR exchange, and should add no
2412 * more than 1 second beyond the measured RTT, due to long
2413 * State Cookie lifetimes making the endpoint more subject to
2415 * Measure of Staleness's unit is usec. (1/1000000 sec)
2416 * Suggested Cookie Life-span Increment's unit is msec.
2418 * In general, if you use the suggested cookie life, the value
2419 * found in the field of measure of staleness should be doubled
2420 * to give ample time to retransmit the new cookie and thus
2421 * yield a higher probability of success on the reattempt.
2423 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2424 stale = (stale * 2) / 1000;
2426 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2427 bht.param_hdr.length = htons(sizeof(bht));
2428 bht.lifespan_increment = htonl(stale);
2430 /* Build that new INIT chunk. */
2431 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2432 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2436 sctp_addto_chunk(reply, sizeof(bht), &bht);
2438 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2439 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2441 /* Stop pending T3-rtx and heartbeat timers */
2442 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2443 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2445 /* Delete non-primary peer ip addresses since we are transitioning
2446 * back to the COOKIE-WAIT state
2448 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2450 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2453 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2454 SCTP_TRANSPORT(asoc->peer.primary_path));
2456 /* Cast away the const modifier, as we want to just
2457 * rerun it through as a sideffect.
2459 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2461 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2462 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2463 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2464 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2465 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2466 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2468 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2470 return SCTP_DISPOSITION_CONSUME;
2473 return SCTP_DISPOSITION_NOMEM;
2480 * After checking the Verification Tag, the receiving endpoint shall
2481 * remove the association from its record, and shall report the
2482 * termination to its upper layer.
2484 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2485 * B) Rules for packet carrying ABORT:
2487 * - The endpoint shall always fill in the Verification Tag field of the
2488 * outbound packet with the destination endpoint's tag value if it
2491 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2492 * MUST follow the procedure described in Section 8.4.
2494 * - The receiver MUST accept the packet if the Verification Tag
2495 * matches either its own tag, OR the tag of its peer. Otherwise, the
2496 * receiver MUST silently discard the packet and take no further
2500 * (endpoint, asoc, chunk)
2503 * (asoc, reply_msg, msg_up, timers, counters)
2505 * The return value is the disposition of the chunk.
2507 enum sctp_disposition sctp_sf_do_9_1_abort(
2509 const struct sctp_endpoint *ep,
2510 const struct sctp_association *asoc,
2511 const union sctp_subtype type,
2513 struct sctp_cmd_seq *commands)
2515 struct sctp_chunk *chunk = arg;
2517 if (!sctp_vtag_verify_either(chunk, asoc))
2518 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2520 /* Make sure that the ABORT chunk has a valid length.
2521 * Since this is an ABORT chunk, we have to discard it
2522 * because of the following text:
2523 * RFC 2960, Section 3.3.7
2524 * If an endpoint receives an ABORT with a format error or for an
2525 * association that doesn't exist, it MUST silently discard it.
2526 * Because the length is "invalid", we can't really discard just
2527 * as we do not know its true length. So, to be safe, discard the
2530 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2531 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2533 /* ADD-IP: Special case for ABORT chunks
2534 * F4) One special consideration is that ABORT Chunks arriving
2535 * destined to the IP address being deleted MUST be
2536 * ignored (see Section 5.3.1 for further details).
2538 if (SCTP_ADDR_DEL ==
2539 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2540 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2542 if (!sctp_err_chunk_valid(chunk))
2543 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2545 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2548 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2550 const struct sctp_endpoint *ep,
2551 const struct sctp_association *asoc,
2552 const union sctp_subtype type,
2554 struct sctp_cmd_seq *commands)
2556 __be16 error = SCTP_ERROR_NO_ERROR;
2557 struct sctp_chunk *chunk = arg;
2560 /* See if we have an error cause code in the chunk. */
2561 len = ntohs(chunk->chunk_hdr->length);
2562 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2563 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2565 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2566 /* ASSOC_FAILED will DELETE_TCB. */
2567 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2568 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2569 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2571 return SCTP_DISPOSITION_ABORT;
2575 * Process an ABORT. (COOKIE-WAIT state)
2577 * See sctp_sf_do_9_1_abort() above.
2579 enum sctp_disposition sctp_sf_cookie_wait_abort(
2581 const struct sctp_endpoint *ep,
2582 const struct sctp_association *asoc,
2583 const union sctp_subtype type,
2585 struct sctp_cmd_seq *commands)
2587 __be16 error = SCTP_ERROR_NO_ERROR;
2588 struct sctp_chunk *chunk = arg;
2591 if (!sctp_vtag_verify_either(chunk, asoc))
2592 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2594 /* Make sure that the ABORT chunk has a valid length.
2595 * Since this is an ABORT chunk, we have to discard it
2596 * because of the following text:
2597 * RFC 2960, Section 3.3.7
2598 * If an endpoint receives an ABORT with a format error or for an
2599 * association that doesn't exist, it MUST silently discard it.
2600 * Because the length is "invalid", we can't really discard just
2601 * as we do not know its true length. So, to be safe, discard the
2604 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2605 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2607 /* See if we have an error cause code in the chunk. */
2608 len = ntohs(chunk->chunk_hdr->length);
2609 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2610 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2612 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2617 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2619 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2621 const struct sctp_endpoint *ep,
2622 const struct sctp_association *asoc,
2623 const union sctp_subtype type,
2625 struct sctp_cmd_seq *commands)
2627 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2629 (struct sctp_transport *)arg);
2633 * Process an ABORT. (COOKIE-ECHOED state)
2635 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2637 const struct sctp_endpoint *ep,
2638 const struct sctp_association *asoc,
2639 const union sctp_subtype type,
2641 struct sctp_cmd_seq *commands)
2643 /* There is a single T1 timer, so we should be able to use
2644 * common function with the COOKIE-WAIT state.
2646 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2650 * Stop T1 timer and abort association with "INIT failed".
2652 * This is common code called by several sctp_sf_*_abort() functions above.
2654 static enum sctp_disposition sctp_stop_t1_and_abort(
2656 struct sctp_cmd_seq *commands,
2657 __be16 error, int sk_err,
2658 const struct sctp_association *asoc,
2659 struct sctp_transport *transport)
2661 pr_debug("%s: ABORT received (INIT)\n", __func__);
2663 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2664 SCTP_STATE(SCTP_STATE_CLOSED));
2665 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2666 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2667 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2668 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2669 /* CMD_INIT_FAILED will DELETE_TCB. */
2670 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2673 return SCTP_DISPOSITION_ABORT;
2677 * sctp_sf_do_9_2_shut
2680 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2681 * - enter the SHUTDOWN-RECEIVED state,
2683 * - stop accepting new data from its SCTP user
2685 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2686 * that all its outstanding DATA chunks have been received by the
2689 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2690 * send a SHUTDOWN in response to a ULP request. And should discard
2691 * subsequent SHUTDOWN chunks.
2693 * If there are still outstanding DATA chunks left, the SHUTDOWN
2694 * receiver shall continue to follow normal data transmission
2695 * procedures defined in Section 6 until all outstanding DATA chunks
2696 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2697 * new data from its SCTP user.
2699 * Verification Tag: 8.5 Verification Tag [Normal verification]
2702 * (endpoint, asoc, chunk)
2705 * (asoc, reply_msg, msg_up, timers, counters)
2707 * The return value is the disposition of the chunk.
2709 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2711 const struct sctp_endpoint *ep,
2712 const struct sctp_association *asoc,
2713 const union sctp_subtype type,
2715 struct sctp_cmd_seq *commands)
2717 enum sctp_disposition disposition;
2718 struct sctp_chunk *chunk = arg;
2719 struct sctp_shutdownhdr *sdh;
2720 struct sctp_ulpevent *ev;
2723 if (!sctp_vtag_verify(chunk, asoc))
2724 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2726 /* Make sure that the SHUTDOWN chunk has a valid length. */
2727 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2728 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2731 /* Convert the elaborate header. */
2732 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2733 skb_pull(chunk->skb, sizeof(*sdh));
2734 chunk->subh.shutdown_hdr = sdh;
2735 ctsn = ntohl(sdh->cum_tsn_ack);
2737 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2738 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2739 asoc->ctsn_ack_point);
2741 return SCTP_DISPOSITION_DISCARD;
2744 /* If Cumulative TSN Ack beyond the max tsn currently
2745 * send, terminating the association and respond to the
2746 * sender with an ABORT.
2748 if (!TSN_lt(ctsn, asoc->next_tsn))
2749 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2751 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2752 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2753 * inform the application that it should cease sending data.
2755 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2757 disposition = SCTP_DISPOSITION_NOMEM;
2760 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2762 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2763 * - enter the SHUTDOWN-RECEIVED state,
2764 * - stop accepting new data from its SCTP user
2766 * [This is implicit in the new state.]
2768 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2769 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2770 disposition = SCTP_DISPOSITION_CONSUME;
2772 if (sctp_outq_is_empty(&asoc->outqueue)) {
2773 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2777 if (SCTP_DISPOSITION_NOMEM == disposition)
2780 /* - verify, by checking the Cumulative TSN Ack field of the
2781 * chunk, that all its outstanding DATA chunks have been
2782 * received by the SHUTDOWN sender.
2784 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2785 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2792 * sctp_sf_do_9_2_shut_ctsn
2794 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2795 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2796 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2797 * MUST be processed.
2799 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2801 const struct sctp_endpoint *ep,
2802 const struct sctp_association *asoc,
2803 const union sctp_subtype type,
2805 struct sctp_cmd_seq *commands)
2807 struct sctp_chunk *chunk = arg;
2808 struct sctp_shutdownhdr *sdh;
2811 if (!sctp_vtag_verify(chunk, asoc))
2812 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2814 /* Make sure that the SHUTDOWN chunk has a valid length. */
2815 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2816 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2819 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2820 ctsn = ntohl(sdh->cum_tsn_ack);
2822 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2823 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2824 asoc->ctsn_ack_point);
2826 return SCTP_DISPOSITION_DISCARD;
2829 /* If Cumulative TSN Ack beyond the max tsn currently
2830 * send, terminating the association and respond to the
2831 * sender with an ABORT.
2833 if (!TSN_lt(ctsn, asoc->next_tsn))
2834 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2836 /* verify, by checking the Cumulative TSN Ack field of the
2837 * chunk, that all its outstanding DATA chunks have been
2838 * received by the SHUTDOWN sender.
2840 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2841 SCTP_BE32(sdh->cum_tsn_ack));
2843 return SCTP_DISPOSITION_CONSUME;
2847 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2848 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2849 * transport addresses (either in the IP addresses or in the INIT chunk)
2850 * that belong to this association, it should discard the INIT chunk and
2851 * retransmit the SHUTDOWN ACK chunk.
2853 static enum sctp_disposition
2854 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2855 const struct sctp_association *asoc,
2856 const union sctp_subtype type, void *arg,
2857 struct sctp_cmd_seq *commands)
2859 struct sctp_chunk *chunk = arg;
2860 struct sctp_chunk *reply;
2862 /* Make sure that the chunk has a valid length */
2863 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2864 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2867 /* Since we are not going to really process this INIT, there
2868 * is no point in verifying chunk boundries. Just generate
2871 reply = sctp_make_shutdown_ack(asoc, chunk);
2875 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2876 * the T2-SHUTDOWN timer.
2878 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2880 /* and restart the T2-shutdown timer. */
2881 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2882 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2884 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2886 return SCTP_DISPOSITION_CONSUME;
2888 return SCTP_DISPOSITION_NOMEM;
2891 enum sctp_disposition
2892 sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2893 const struct sctp_association *asoc,
2894 const union sctp_subtype type, void *arg,
2895 struct sctp_cmd_seq *commands)
2897 struct sctp_chunk *chunk = arg;
2899 if (!chunk->singleton)
2900 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2902 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
2903 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2905 if (chunk->sctp_hdr->vtag != 0)
2906 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
2908 return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
2912 * sctp_sf_do_ecn_cwr
2914 * Section: Appendix A: Explicit Congestion Notification
2918 * RFC 2481 details a specific bit for a sender to send in the header of
2919 * its next outbound TCP segment to indicate to its peer that it has
2920 * reduced its congestion window. This is termed the CWR bit. For
2921 * SCTP the same indication is made by including the CWR chunk.
2922 * This chunk contains one data element, i.e. the TSN number that
2923 * was sent in the ECNE chunk. This element represents the lowest
2924 * TSN number in the datagram that was originally marked with the
2927 * Verification Tag: 8.5 Verification Tag [Normal verification]
2929 * (endpoint, asoc, chunk)
2932 * (asoc, reply_msg, msg_up, timers, counters)
2934 * The return value is the disposition of the chunk.
2936 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2937 const struct sctp_endpoint *ep,
2938 const struct sctp_association *asoc,
2939 const union sctp_subtype type,
2941 struct sctp_cmd_seq *commands)
2943 struct sctp_chunk *chunk = arg;
2944 struct sctp_cwrhdr *cwr;
2947 if (!sctp_vtag_verify(chunk, asoc))
2948 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2950 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
2951 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2954 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2955 skb_pull(chunk->skb, sizeof(*cwr));
2957 lowest_tsn = ntohl(cwr->lowest_tsn);
2959 /* Does this CWR ack the last sent congestion notification? */
2960 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2961 /* Stop sending ECNE. */
2962 sctp_add_cmd_sf(commands,
2964 SCTP_U32(lowest_tsn));
2966 return SCTP_DISPOSITION_CONSUME;
2972 * Section: Appendix A: Explicit Congestion Notification
2976 * RFC 2481 details a specific bit for a receiver to send back in its
2977 * TCP acknowledgements to notify the sender of the Congestion
2978 * Experienced (CE) bit having arrived from the network. For SCTP this
2979 * same indication is made by including the ECNE chunk. This chunk
2980 * contains one data element, i.e. the lowest TSN associated with the IP
2981 * datagram marked with the CE bit.....
2983 * Verification Tag: 8.5 Verification Tag [Normal verification]
2985 * (endpoint, asoc, chunk)
2988 * (asoc, reply_msg, msg_up, timers, counters)
2990 * The return value is the disposition of the chunk.
2992 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
2993 const struct sctp_endpoint *ep,
2994 const struct sctp_association *asoc,
2995 const union sctp_subtype type,
2996 void *arg, struct sctp_cmd_seq *commands)
2998 struct sctp_chunk *chunk = arg;
2999 struct sctp_ecnehdr *ecne;
3001 if (!sctp_vtag_verify(chunk, asoc))
3002 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3004 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3005 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3008 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3009 skb_pull(chunk->skb, sizeof(*ecne));
3011 /* If this is a newer ECNE than the last CWR packet we sent out */
3012 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3013 SCTP_U32(ntohl(ecne->lowest_tsn)));
3015 return SCTP_DISPOSITION_CONSUME;
3019 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3021 * The SCTP endpoint MUST always acknowledge the reception of each valid
3024 * The guidelines on delayed acknowledgement algorithm specified in
3025 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3026 * acknowledgement SHOULD be generated for at least every second packet
3027 * (not every second DATA chunk) received, and SHOULD be generated within
3028 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3029 * situations it may be beneficial for an SCTP transmitter to be more
3030 * conservative than the algorithms detailed in this document allow.
3031 * However, an SCTP transmitter MUST NOT be more aggressive than the
3032 * following algorithms allow.
3034 * A SCTP receiver MUST NOT generate more than one SACK for every
3035 * incoming packet, other than to update the offered window as the
3036 * receiving application consumes new data.
3038 * Verification Tag: 8.5 Verification Tag [Normal verification]
3041 * (endpoint, asoc, chunk)
3044 * (asoc, reply_msg, msg_up, timers, counters)
3046 * The return value is the disposition of the chunk.
3048 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3049 const struct sctp_endpoint *ep,
3050 const struct sctp_association *asoc,
3051 const union sctp_subtype type,
3053 struct sctp_cmd_seq *commands)
3055 union sctp_arg force = SCTP_NOFORCE();
3056 struct sctp_chunk *chunk = arg;
3059 if (!sctp_vtag_verify(chunk, asoc)) {
3060 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3062 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3065 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
3066 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3069 error = sctp_eat_data(asoc, chunk, commands);
3071 case SCTP_IERROR_NO_ERROR:
3073 case SCTP_IERROR_HIGH_TSN:
3074 case SCTP_IERROR_BAD_STREAM:
3075 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3076 goto discard_noforce;
3077 case SCTP_IERROR_DUP_TSN:
3078 case SCTP_IERROR_IGNORE_TSN:
3079 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3081 case SCTP_IERROR_NO_DATA:
3082 return SCTP_DISPOSITION_ABORT;
3083 case SCTP_IERROR_PROTO_VIOLATION:
3084 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3085 (u8 *)chunk->subh.data_hdr,
3086 sizeof(struct sctp_datahdr));
3091 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3092 force = SCTP_FORCE();
3094 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3095 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3096 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3099 /* If this is the last chunk in a packet, we need to count it
3100 * toward sack generation. Note that we need to SACK every
3101 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3102 * THEM. We elect to NOT generate SACK's if the chunk fails
3103 * the verification tag test.
3105 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3107 * The SCTP endpoint MUST always acknowledge the reception of
3108 * each valid DATA chunk.
3110 * The guidelines on delayed acknowledgement algorithm
3111 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3112 * Specifically, an acknowledgement SHOULD be generated for at
3113 * least every second packet (not every second DATA chunk)
3114 * received, and SHOULD be generated within 200 ms of the
3115 * arrival of any unacknowledged DATA chunk. In some
3116 * situations it may be beneficial for an SCTP transmitter to
3117 * be more conservative than the algorithms detailed in this
3118 * document allow. However, an SCTP transmitter MUST NOT be
3119 * more aggressive than the following algorithms allow.
3121 if (chunk->end_of_packet)
3122 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3124 return SCTP_DISPOSITION_CONSUME;
3127 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3129 * When a packet arrives with duplicate DATA chunk(s) and with
3130 * no new DATA chunk(s), the endpoint MUST immediately send a
3131 * SACK with no delay. If a packet arrives with duplicate
3132 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3133 * MAY immediately send a SACK. Normally receipt of duplicate
3134 * DATA chunks will occur when the original SACK chunk was lost
3135 * and the peer's RTO has expired. The duplicate TSN number(s)
3136 * SHOULD be reported in the SACK as duplicate.
3138 /* In our case, we split the MAY SACK advice up whether or not
3139 * the last chunk is a duplicate.'
3141 if (chunk->end_of_packet)
3142 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3143 return SCTP_DISPOSITION_DISCARD;
3146 if (chunk->end_of_packet)
3147 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3149 return SCTP_DISPOSITION_DISCARD;
3153 * sctp_sf_eat_data_fast_4_4
3156 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3157 * DATA chunks without delay.
3159 * Verification Tag: 8.5 Verification Tag [Normal verification]
3161 * (endpoint, asoc, chunk)
3164 * (asoc, reply_msg, msg_up, timers, counters)
3166 * The return value is the disposition of the chunk.
3168 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3170 const struct sctp_endpoint *ep,
3171 const struct sctp_association *asoc,
3172 const union sctp_subtype type,
3174 struct sctp_cmd_seq *commands)
3176 struct sctp_chunk *chunk = arg;
3179 if (!sctp_vtag_verify(chunk, asoc)) {
3180 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3182 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3185 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
3186 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3189 error = sctp_eat_data(asoc, chunk, commands);
3191 case SCTP_IERROR_NO_ERROR:
3192 case SCTP_IERROR_HIGH_TSN:
3193 case SCTP_IERROR_DUP_TSN:
3194 case SCTP_IERROR_IGNORE_TSN:
3195 case SCTP_IERROR_BAD_STREAM:
3197 case SCTP_IERROR_NO_DATA:
3198 return SCTP_DISPOSITION_ABORT;
3199 case SCTP_IERROR_PROTO_VIOLATION:
3200 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3201 (u8 *)chunk->subh.data_hdr,
3202 sizeof(struct sctp_datahdr));
3207 /* Go a head and force a SACK, since we are shutting down. */
3209 /* Implementor's Guide.
3211 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3212 * respond to each received packet containing one or more DATA chunk(s)
3213 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3215 if (chunk->end_of_packet) {
3216 /* We must delay the chunk creation since the cumulative
3217 * TSN has not been updated yet.
3219 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3220 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3221 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3222 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3225 return SCTP_DISPOSITION_CONSUME;
3229 * Section: 6.2 Processing a Received SACK
3230 * D) Any time a SACK arrives, the endpoint performs the following:
3232 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3233 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3234 * increasing, a SACK whose Cumulative TSN Ack is less than the
3235 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3237 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3238 * of bytes still outstanding after processing the Cumulative TSN Ack
3239 * and the Gap Ack Blocks.
3241 * iii) If the SACK is missing a TSN that was previously
3242 * acknowledged via a Gap Ack Block (e.g., the data receiver
3243 * reneged on the data), then mark the corresponding DATA chunk
3244 * as available for retransmit: Mark it as missing for fast
3245 * retransmit as described in Section 7.2.4 and if no retransmit
3246 * timer is running for the destination address to which the DATA
3247 * chunk was originally transmitted, then T3-rtx is started for
3248 * that destination address.
3250 * Verification Tag: 8.5 Verification Tag [Normal verification]
3253 * (endpoint, asoc, chunk)
3256 * (asoc, reply_msg, msg_up, timers, counters)
3258 * The return value is the disposition of the chunk.
3260 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3261 const struct sctp_endpoint *ep,
3262 const struct sctp_association *asoc,
3263 const union sctp_subtype type,
3265 struct sctp_cmd_seq *commands)
3267 struct sctp_chunk *chunk = arg;
3268 struct sctp_sackhdr *sackh;
3271 if (!sctp_vtag_verify(chunk, asoc))
3272 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3274 /* Make sure that the SACK chunk has a valid length. */
3275 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3276 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3279 /* Pull the SACK chunk from the data buffer */
3280 sackh = sctp_sm_pull_sack(chunk);
3281 /* Was this a bogus SACK? */
3283 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3284 chunk->subh.sack_hdr = sackh;
3285 ctsn = ntohl(sackh->cum_tsn_ack);
3287 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3288 * Ack Point, then drop the SACK. Since Cumulative TSN
3289 * Ack is monotonically increasing, a SACK whose
3290 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3291 * Point indicates an out-of-order SACK.
3293 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3294 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3295 asoc->ctsn_ack_point);
3297 return SCTP_DISPOSITION_DISCARD;
3300 /* If Cumulative TSN Ack beyond the max tsn currently
3301 * send, terminating the association and respond to the
3302 * sender with an ABORT.
3304 if (!TSN_lt(ctsn, asoc->next_tsn))
3305 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3307 /* Return this SACK for further processing. */
3308 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3310 /* Note: We do the rest of the work on the PROCESS_SACK
3313 return SCTP_DISPOSITION_CONSUME;
3317 * Generate an ABORT in response to a packet.
3319 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3321 * 8) The receiver should respond to the sender of the OOTB packet with
3322 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3323 * MUST fill in the Verification Tag field of the outbound packet
3324 * with the value found in the Verification Tag field of the OOTB
3325 * packet and set the T-bit in the Chunk Flags to indicate that the
3326 * Verification Tag is reflected. After sending this ABORT, the
3327 * receiver of the OOTB packet shall discard the OOTB packet and take
3328 * no further action.
3332 * The return value is the disposition of the chunk.
3334 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3336 const struct sctp_endpoint *ep,
3337 const struct sctp_association *asoc,
3338 const union sctp_subtype type,
3340 struct sctp_cmd_seq *commands)
3342 struct sctp_packet *packet = NULL;
3343 struct sctp_chunk *chunk = arg;
3344 struct sctp_chunk *abort;
3346 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3348 return SCTP_DISPOSITION_NOMEM;
3350 /* Make an ABORT. The T bit will be set if the asoc
3353 abort = sctp_make_abort(asoc, chunk, 0);
3355 sctp_ootb_pkt_free(packet);
3356 return SCTP_DISPOSITION_NOMEM;
3359 /* Reflect vtag if T-Bit is set */
3360 if (sctp_test_T_bit(abort))
3361 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3363 /* Set the skb to the belonging sock for accounting. */
3364 abort->skb->sk = ep->base.sk;
3366 sctp_packet_append_chunk(packet, abort);
3368 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3369 SCTP_PACKET(packet));
3371 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3373 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3374 return SCTP_DISPOSITION_CONSUME;
3378 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3379 * event as ULP notification for each cause included in the chunk.
3381 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3383 * The return value is the disposition of the chunk.
3385 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3386 const struct sctp_endpoint *ep,
3387 const struct sctp_association *asoc,
3388 const union sctp_subtype type,
3390 struct sctp_cmd_seq *commands)
3392 struct sctp_chunk *chunk = arg;
3393 struct sctp_errhdr *err;
3395 if (!sctp_vtag_verify(chunk, asoc))
3396 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3398 /* Make sure that the ERROR chunk has a valid length. */
3399 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3400 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3402 sctp_walk_errors(err, chunk->chunk_hdr);
3403 if ((void *)err != (void *)chunk->chunk_end)
3404 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3405 (void *)err, commands);
3407 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3410 return SCTP_DISPOSITION_CONSUME;
3414 * Process an inbound SHUTDOWN ACK.
3417 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3418 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3419 * peer, and remove all record of the association.
3421 * The return value is the disposition.
3423 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3424 const struct sctp_endpoint *ep,
3425 const struct sctp_association *asoc,
3426 const union sctp_subtype type,
3428 struct sctp_cmd_seq *commands)
3430 struct sctp_chunk *chunk = arg;
3431 struct sctp_chunk *reply;
3432 struct sctp_ulpevent *ev;
3434 if (!sctp_vtag_verify(chunk, asoc))
3435 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3437 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3438 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3441 /* 10.2 H) SHUTDOWN COMPLETE notification
3443 * When SCTP completes the shutdown procedures (section 9.2) this
3444 * notification is passed to the upper layer.
3446 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3447 0, 0, 0, NULL, GFP_ATOMIC);
3451 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3452 reply = sctp_make_shutdown_complete(asoc, chunk);
3456 /* Do all the commands now (after allocation), so that we
3457 * have consistent state if memory allocation failes
3459 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3461 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3462 * stop the T2-shutdown timer,
3464 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3465 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3467 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3468 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3470 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3471 SCTP_STATE(SCTP_STATE_CLOSED));
3472 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3473 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3474 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3476 /* ...and remove all record of the association. */
3477 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3478 return SCTP_DISPOSITION_DELETE_TCB;
3481 sctp_ulpevent_free(ev);
3483 return SCTP_DISPOSITION_NOMEM;
3487 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3489 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3490 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3491 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3492 * packet must fill in the Verification Tag field of the outbound
3493 * packet with the Verification Tag received in the SHUTDOWN ACK and
3494 * set the T-bit in the Chunk Flags to indicate that the Verification
3497 * 8) The receiver should respond to the sender of the OOTB packet with
3498 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3499 * MUST fill in the Verification Tag field of the outbound packet
3500 * with the value found in the Verification Tag field of the OOTB
3501 * packet and set the T-bit in the Chunk Flags to indicate that the
3502 * Verification Tag is reflected. After sending this ABORT, the
3503 * receiver of the OOTB packet shall discard the OOTB packet and take
3504 * no further action.
3506 enum sctp_disposition sctp_sf_ootb(struct net *net,
3507 const struct sctp_endpoint *ep,
3508 const struct sctp_association *asoc,
3509 const union sctp_subtype type,
3510 void *arg, struct sctp_cmd_seq *commands)
3512 struct sctp_chunk *chunk = arg;
3513 struct sk_buff *skb = chunk->skb;
3514 struct sctp_chunkhdr *ch;
3515 struct sctp_errhdr *err;
3516 int ootb_cookie_ack = 0;
3517 int ootb_shut_ack = 0;
3520 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3522 if (asoc && !sctp_vtag_verify(chunk, asoc))
3525 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3527 /* Report violation if the chunk is less then minimal */
3528 if (ntohs(ch->length) < sizeof(*ch))
3529 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3532 /* Report violation if chunk len overflows */
3533 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3534 if (ch_end > skb_tail_pointer(skb))
3535 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3538 /* Now that we know we at least have a chunk header,
3539 * do things that are type appropriate.
3541 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3544 /* RFC 2960, Section 3.3.7
3545 * Moreover, under any circumstances, an endpoint that
3546 * receives an ABORT MUST NOT respond to that ABORT by
3547 * sending an ABORT of its own.
3549 if (SCTP_CID_ABORT == ch->type)
3550 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3552 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3553 * or a COOKIE ACK the SCTP Packet should be silently
3557 if (SCTP_CID_COOKIE_ACK == ch->type)
3558 ootb_cookie_ack = 1;
3560 if (SCTP_CID_ERROR == ch->type) {
3561 sctp_walk_errors(err, ch) {
3562 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3563 ootb_cookie_ack = 1;
3569 ch = (struct sctp_chunkhdr *)ch_end;
3570 } while (ch_end < skb_tail_pointer(skb));
3573 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3574 else if (ootb_cookie_ack)
3575 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3577 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3581 * Handle an "Out of the blue" SHUTDOWN ACK.
3583 * Section: 8.4 5, sctpimpguide 2.41.
3585 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3586 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3587 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3588 * packet must fill in the Verification Tag field of the outbound
3589 * packet with the Verification Tag received in the SHUTDOWN ACK and
3590 * set the T-bit in the Chunk Flags to indicate that the Verification
3594 * (endpoint, asoc, type, arg, commands)
3597 * (enum sctp_disposition)
3599 * The return value is the disposition of the chunk.
3601 static enum sctp_disposition sctp_sf_shut_8_4_5(
3603 const struct sctp_endpoint *ep,
3604 const struct sctp_association *asoc,
3605 const union sctp_subtype type,
3607 struct sctp_cmd_seq *commands)
3609 struct sctp_packet *packet = NULL;
3610 struct sctp_chunk *chunk = arg;
3611 struct sctp_chunk *shut;
3613 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3615 return SCTP_DISPOSITION_NOMEM;
3617 /* Make an SHUTDOWN_COMPLETE.
3618 * The T bit will be set if the asoc is NULL.
3620 shut = sctp_make_shutdown_complete(asoc, chunk);
3622 sctp_ootb_pkt_free(packet);
3623 return SCTP_DISPOSITION_NOMEM;
3626 /* Reflect vtag if T-Bit is set */
3627 if (sctp_test_T_bit(shut))
3628 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3630 /* Set the skb to the belonging sock for accounting. */
3631 shut->skb->sk = ep->base.sk;
3633 sctp_packet_append_chunk(packet, shut);
3635 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3636 SCTP_PACKET(packet));
3638 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3640 /* We need to discard the rest of the packet to prevent
3641 * potential bomming attacks from additional bundled chunks.
3642 * This is documented in SCTP Threats ID.
3644 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3648 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3650 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3651 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3652 * procedures in section 8.4 SHOULD be followed, in other words it
3653 * should be treated as an Out Of The Blue packet.
3654 * [This means that we do NOT check the Verification Tag on these
3658 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3659 const struct sctp_endpoint *ep,
3660 const struct sctp_association *asoc,
3661 const union sctp_subtype type,
3663 struct sctp_cmd_seq *commands)
3665 struct sctp_chunk *chunk = arg;
3667 if (!sctp_vtag_verify(chunk, asoc))
3670 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3671 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3672 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3675 /* Although we do have an association in this case, it corresponds
3676 * to a restarted association. So the packet is treated as an OOTB
3677 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3678 * called with a NULL association.
3680 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3682 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3685 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3686 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3687 const struct sctp_endpoint *ep,
3688 const struct sctp_association *asoc,
3689 const union sctp_subtype type,
3691 struct sctp_cmd_seq *commands)
3693 struct sctp_paramhdr *err_param = NULL;
3694 struct sctp_chunk *asconf_ack = NULL;
3695 struct sctp_chunk *chunk = arg;
3696 struct sctp_addiphdr *hdr;
3699 if (!sctp_vtag_verify(chunk, asoc)) {
3700 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3702 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3705 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3706 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3707 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3710 /* ADD-IP: Section 4.1.1
3711 * This chunk MUST be sent in an authenticated way by using
3712 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3713 * is received unauthenticated it MUST be silently discarded as
3714 * described in [I-D.ietf-tsvwg-sctp-auth].
3716 if (!net->sctp.addip_noauth && !chunk->auth)
3717 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3719 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3720 serial = ntohl(hdr->serial);
3722 /* Verify the ASCONF chunk before processing it. */
3723 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3724 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3725 (void *)err_param, commands);
3727 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3728 * the endpoint stored in a new association variable
3729 * 'Peer-Serial-Number'.
3731 if (serial == asoc->peer.addip_serial + 1) {
3732 /* If this is the first instance of ASCONF in the packet,
3733 * we can clean our old ASCONF-ACKs.
3735 if (!chunk->has_asconf)
3736 sctp_assoc_clean_asconf_ack_cache(asoc);
3738 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3739 * expected, process the ASCONF as described below and after
3740 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3741 * the response packet and cache a copy of it (in the event it
3742 * later needs to be retransmitted).
3744 * Essentially, do V1-V5.
3746 asconf_ack = sctp_process_asconf((struct sctp_association *)
3749 return SCTP_DISPOSITION_NOMEM;
3750 } else if (serial < asoc->peer.addip_serial + 1) {
3752 * If the value found in the Sequence Number is less than the
3753 * ('Peer- Sequence-Number' + 1), simply skip to the next
3754 * ASCONF, and include in the outbound response packet
3755 * any previously cached ASCONF-ACK response that was
3756 * sent and saved that matches the Sequence Number of the
3757 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3758 * Chunk exists. This will occur when an older ASCONF
3759 * arrives out of order. In such a case, the receiver
3760 * should skip the ASCONF Chunk and not include ASCONF-ACK
3761 * Chunk for that chunk.
3763 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3765 return SCTP_DISPOSITION_DISCARD;
3767 /* Reset the transport so that we select the correct one
3768 * this time around. This is to make sure that we don't
3769 * accidentally use a stale transport that's been removed.
3771 asconf_ack->transport = NULL;
3773 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3774 * it must be either a stale packet or from an attacker.
3776 return SCTP_DISPOSITION_DISCARD;
3779 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3780 * containing the ASCONF-ACK Chunks MUST be the source address of
3781 * the SCTP packet that held the ASCONF Chunks.
3783 * To do this properly, we'll set the destination address of the chunk
3784 * and at the transmit time, will try look up the transport to use.
3785 * Since ASCONFs may be bundled, the correct transport may not be
3786 * created until we process the entire packet, thus this workaround.
3788 asconf_ack->dest = chunk->source;
3789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3790 if (asoc->new_transport) {
3791 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3792 ((struct sctp_association *)asoc)->new_transport = NULL;
3795 return SCTP_DISPOSITION_CONSUME;
3798 static enum sctp_disposition sctp_send_next_asconf(
3800 const struct sctp_endpoint *ep,
3801 struct sctp_association *asoc,
3802 const union sctp_subtype type,
3803 struct sctp_cmd_seq *commands)
3805 struct sctp_chunk *asconf;
3806 struct list_head *entry;
3808 if (list_empty(&asoc->addip_chunk_list))
3809 return SCTP_DISPOSITION_CONSUME;
3811 entry = asoc->addip_chunk_list.next;
3812 asconf = list_entry(entry, struct sctp_chunk, list);
3814 list_del_init(entry);
3815 sctp_chunk_hold(asconf);
3816 asoc->addip_last_asconf = asconf;
3818 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3822 * ADDIP Section 4.3 General rules for address manipulation
3823 * When building TLV parameters for the ASCONF Chunk that will add or
3824 * delete IP addresses the D0 to D13 rules should be applied:
3826 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3827 const struct sctp_endpoint *ep,
3828 const struct sctp_association *asoc,
3829 const union sctp_subtype type,
3831 struct sctp_cmd_seq *commands)
3833 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3834 struct sctp_paramhdr *err_param = NULL;
3835 struct sctp_chunk *asconf_ack = arg;
3836 struct sctp_addiphdr *addip_hdr;
3837 __u32 sent_serial, rcvd_serial;
3838 struct sctp_chunk *abort;
3840 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3841 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3843 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3846 /* Make sure that the ADDIP chunk has a valid length. */
3847 if (!sctp_chunk_length_valid(asconf_ack,
3848 sizeof(struct sctp_addip_chunk)))
3849 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3852 /* ADD-IP, Section 4.1.2:
3853 * This chunk MUST be sent in an authenticated way by using
3854 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3855 * is received unauthenticated it MUST be silently discarded as
3856 * described in [I-D.ietf-tsvwg-sctp-auth].
3858 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3859 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3861 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3862 rcvd_serial = ntohl(addip_hdr->serial);
3864 /* Verify the ASCONF-ACK chunk before processing it. */
3865 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3866 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3867 (void *)err_param, commands);
3870 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3871 sent_serial = ntohl(addip_hdr->serial);
3873 sent_serial = asoc->addip_serial - 1;
3876 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3877 * equal to the next serial number to be used but no ASCONF chunk is
3878 * outstanding the endpoint MUST ABORT the association. Note that a
3879 * sequence number is greater than if it is no more than 2^^31-1
3880 * larger than the current sequence number (using serial arithmetic).
3882 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3883 !(asoc->addip_last_asconf)) {
3884 abort = sctp_make_abort(asoc, asconf_ack,
3885 sizeof(struct sctp_errhdr));
3887 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3888 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3891 /* We are going to ABORT, so we might as well stop
3892 * processing the rest of the chunks in the packet.
3894 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3895 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3896 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3897 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3898 SCTP_ERROR(ECONNABORTED));
3899 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3900 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3901 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3902 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3903 return SCTP_DISPOSITION_ABORT;
3906 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3908 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3910 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3912 return sctp_send_next_asconf(net, ep,
3913 (struct sctp_association *)asoc,
3916 abort = sctp_make_abort(asoc, asconf_ack,
3917 sizeof(struct sctp_errhdr));
3919 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3920 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3923 /* We are going to ABORT, so we might as well stop
3924 * processing the rest of the chunks in the packet.
3926 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3927 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3928 SCTP_ERROR(ECONNABORTED));
3929 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3930 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3931 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3932 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3933 return SCTP_DISPOSITION_ABORT;
3936 return SCTP_DISPOSITION_DISCARD;
3939 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3940 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
3941 const struct sctp_endpoint *ep,
3942 const struct sctp_association *asoc,
3943 const union sctp_subtype type,
3945 struct sctp_cmd_seq *commands)
3947 struct sctp_paramhdr *err_param = NULL;
3948 struct sctp_chunk *chunk = arg;
3949 struct sctp_reconf_chunk *hdr;
3950 union sctp_params param;
3952 if (!sctp_vtag_verify(chunk, asoc)) {
3953 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3955 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3958 /* Make sure that the RECONF chunk has a valid length. */
3959 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3960 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3963 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3964 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3965 (void *)err_param, commands);
3967 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3968 sctp_walk_params(param, hdr, params) {
3969 struct sctp_chunk *reply = NULL;
3970 struct sctp_ulpevent *ev = NULL;
3972 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3973 reply = sctp_process_strreset_outreq(
3974 (struct sctp_association *)asoc, param, &ev);
3975 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3976 reply = sctp_process_strreset_inreq(
3977 (struct sctp_association *)asoc, param, &ev);
3978 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3979 reply = sctp_process_strreset_tsnreq(
3980 (struct sctp_association *)asoc, param, &ev);
3981 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3982 reply = sctp_process_strreset_addstrm_out(
3983 (struct sctp_association *)asoc, param, &ev);
3984 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3985 reply = sctp_process_strreset_addstrm_in(
3986 (struct sctp_association *)asoc, param, &ev);
3987 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3988 reply = sctp_process_strreset_resp(
3989 (struct sctp_association *)asoc, param, &ev);
3992 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3996 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4000 return SCTP_DISPOSITION_CONSUME;
4004 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4006 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4007 * its cumulative TSN point to the value carried in the FORWARD TSN
4008 * chunk, and then MUST further advance its cumulative TSN point locally
4010 * After the above processing, the data receiver MUST stop reporting any
4011 * missing TSNs earlier than or equal to the new cumulative TSN point.
4013 * Verification Tag: 8.5 Verification Tag [Normal verification]
4015 * The return value is the disposition of the chunk.
4017 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4018 const struct sctp_endpoint *ep,
4019 const struct sctp_association *asoc,
4020 const union sctp_subtype type,
4022 struct sctp_cmd_seq *commands)
4024 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4025 struct sctp_chunk *chunk = arg;
4026 struct sctp_fwdtsn_skip *skip;
4030 if (!sctp_vtag_verify(chunk, asoc)) {
4031 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4033 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4036 if (!asoc->peer.prsctp_capable)
4037 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4039 /* Make sure that the FORWARD_TSN chunk has valid length. */
4040 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
4041 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4044 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4045 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4046 len = ntohs(chunk->chunk_hdr->length);
4047 len -= sizeof(struct sctp_chunkhdr);
4048 skb_pull(chunk->skb, len);
4050 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4051 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4053 /* The TSN is too high--silently discard the chunk and count on it
4054 * getting retransmitted later.
4056 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4057 goto discard_noforce;
4059 /* Silently discard the chunk if stream-id is not valid */
4060 sctp_walk_fwdtsn(skip, chunk) {
4061 if (ntohs(skip->stream) >= asoc->stream.incnt)
4062 goto discard_noforce;
4065 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4066 if (len > sizeof(struct sctp_fwdtsn_hdr))
4067 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4070 /* Count this as receiving DATA. */
4071 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4072 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4073 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4076 /* FIXME: For now send a SACK, but DATA processing may
4079 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4081 return SCTP_DISPOSITION_CONSUME;
4084 return SCTP_DISPOSITION_DISCARD;
4087 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4089 const struct sctp_endpoint *ep,
4090 const struct sctp_association *asoc,
4091 const union sctp_subtype type,
4093 struct sctp_cmd_seq *commands)
4095 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4096 struct sctp_chunk *chunk = arg;
4097 struct sctp_fwdtsn_skip *skip;
4101 if (!sctp_vtag_verify(chunk, asoc)) {
4102 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4107 if (!asoc->peer.prsctp_capable)
4108 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4110 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4111 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
4112 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4115 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4116 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4117 len = ntohs(chunk->chunk_hdr->length);
4118 len -= sizeof(struct sctp_chunkhdr);
4119 skb_pull(chunk->skb, len);
4121 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4122 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4124 /* The TSN is too high--silently discard the chunk and count on it
4125 * getting retransmitted later.
4127 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4130 /* Silently discard the chunk if stream-id is not valid */
4131 sctp_walk_fwdtsn(skip, chunk) {
4132 if (ntohs(skip->stream) >= asoc->stream.incnt)
4136 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4137 if (len > sizeof(struct sctp_fwdtsn_hdr))
4138 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4141 /* Go a head and force a SACK, since we are shutting down. */
4143 /* Implementor's Guide.
4145 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4146 * respond to each received packet containing one or more DATA chunk(s)
4147 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4149 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4150 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4151 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4152 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4154 return SCTP_DISPOSITION_CONSUME;
4158 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4160 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4161 * Identifier field. If this algorithm was not specified by the
4162 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4163 * during association setup, the AUTH chunk and all chunks after it MUST
4164 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4165 * defined in Section 4.1.
4167 * If an endpoint with no shared key receives a Shared Key Identifier
4168 * other than 0, it MUST silently discard all authenticated chunks. If
4169 * the endpoint has at least one endpoint pair shared key for the peer,
4170 * it MUST use the key specified by the Shared Key Identifier if a
4171 * key has been configured for that Shared Key Identifier. If no
4172 * endpoint pair shared key has been configured for that Shared Key
4173 * Identifier, all authenticated chunks MUST be silently discarded.
4175 * Verification Tag: 8.5 Verification Tag [Normal verification]
4177 * The return value is the disposition of the chunk.
4179 static enum sctp_ierror sctp_sf_authenticate(
4180 const struct sctp_association *asoc,
4181 struct sctp_chunk *chunk)
4183 struct sctp_authhdr *auth_hdr;
4184 __u8 *save_digest, *digest;
4185 struct sctp_hmac *hmac;
4186 unsigned int sig_len;
4189 /* Pull in the auth header, so we can do some more verification */
4190 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4191 chunk->subh.auth_hdr = auth_hdr;
4192 skb_pull(chunk->skb, sizeof(*auth_hdr));
4194 /* Make sure that we support the HMAC algorithm from the auth
4197 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4198 return SCTP_IERROR_AUTH_BAD_HMAC;
4200 /* Make sure that the provided shared key identifier has been
4203 key_id = ntohs(auth_hdr->shkey_id);
4204 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4205 return SCTP_IERROR_AUTH_BAD_KEYID;
4208 /* Make sure that the length of the signature matches what
4211 sig_len = ntohs(chunk->chunk_hdr->length) -
4212 sizeof(struct sctp_auth_chunk);
4213 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4214 if (sig_len != hmac->hmac_len)
4215 return SCTP_IERROR_PROTO_VIOLATION;
4217 /* Now that we've done validation checks, we can compute and
4218 * verify the hmac. The steps involved are:
4219 * 1. Save the digest from the chunk.
4220 * 2. Zero out the digest in the chunk.
4221 * 3. Compute the new digest
4222 * 4. Compare saved and new digests.
4224 digest = auth_hdr->hmac;
4225 skb_pull(chunk->skb, sig_len);
4227 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4231 memset(digest, 0, sig_len);
4233 sctp_auth_calculate_hmac(asoc, chunk->skb,
4234 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4237 /* Discard the packet if the digests do not match */
4238 if (memcmp(save_digest, digest, sig_len)) {
4240 return SCTP_IERROR_BAD_SIG;
4246 return SCTP_IERROR_NO_ERROR;
4248 return SCTP_IERROR_NOMEM;
4251 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4252 const struct sctp_endpoint *ep,
4253 const struct sctp_association *asoc,
4254 const union sctp_subtype type,
4255 void *arg, struct sctp_cmd_seq *commands)
4257 struct sctp_chunk *chunk = arg;
4258 struct sctp_authhdr *auth_hdr;
4259 struct sctp_chunk *err_chunk;
4260 enum sctp_ierror error;
4262 /* Make sure that the peer has AUTH capable */
4263 if (!asoc->peer.auth_capable)
4264 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4266 if (!sctp_vtag_verify(chunk, asoc)) {
4267 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4269 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4272 /* Make sure that the AUTH chunk has valid length. */
4273 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4274 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4277 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4278 error = sctp_sf_authenticate(asoc, chunk);
4280 case SCTP_IERROR_AUTH_BAD_HMAC:
4281 /* Generate the ERROR chunk and discard the rest
4284 err_chunk = sctp_make_op_error(asoc, chunk,
4285 SCTP_ERROR_UNSUP_HMAC,
4289 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4290 SCTP_CHUNK(err_chunk));
4293 case SCTP_IERROR_AUTH_BAD_KEYID:
4294 case SCTP_IERROR_BAD_SIG:
4295 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4297 case SCTP_IERROR_PROTO_VIOLATION:
4298 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4301 case SCTP_IERROR_NOMEM:
4302 return SCTP_DISPOSITION_NOMEM;
4304 default: /* Prevent gcc warnings */
4308 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4309 struct sctp_ulpevent *ev;
4311 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4312 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4317 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4321 return SCTP_DISPOSITION_CONSUME;
4325 * Process an unknown chunk.
4327 * Section: 3.2. Also, 2.1 in the implementor's guide.
4329 * Chunk Types are encoded such that the highest-order two bits specify
4330 * the action that must be taken if the processing endpoint does not
4331 * recognize the Chunk Type.
4333 * 00 - Stop processing this SCTP packet and discard it, do not process
4334 * any further chunks within it.
4336 * 01 - Stop processing this SCTP packet and discard it, do not process
4337 * any further chunks within it, and report the unrecognized
4338 * chunk in an 'Unrecognized Chunk Type'.
4340 * 10 - Skip this chunk and continue processing.
4342 * 11 - Skip this chunk and continue processing, but report in an ERROR
4343 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4345 * The return value is the disposition of the chunk.
4347 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4348 const struct sctp_endpoint *ep,
4349 const struct sctp_association *asoc,
4350 const union sctp_subtype type,
4352 struct sctp_cmd_seq *commands)
4354 struct sctp_chunk *unk_chunk = arg;
4355 struct sctp_chunk *err_chunk;
4356 struct sctp_chunkhdr *hdr;
4358 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4360 if (!sctp_vtag_verify(unk_chunk, asoc))
4361 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4363 /* Make sure that the chunk has a valid length.
4364 * Since we don't know the chunk type, we use a general
4365 * chunkhdr structure to make a comparison.
4367 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4368 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4371 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4372 case SCTP_CID_ACTION_DISCARD:
4373 /* Discard the packet. */
4374 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4375 case SCTP_CID_ACTION_DISCARD_ERR:
4376 /* Generate an ERROR chunk as response. */
4377 hdr = unk_chunk->chunk_hdr;
4378 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4379 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4380 SCTP_PAD4(ntohs(hdr->length)),
4383 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4384 SCTP_CHUNK(err_chunk));
4387 /* Discard the packet. */
4388 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4389 return SCTP_DISPOSITION_CONSUME;
4390 case SCTP_CID_ACTION_SKIP:
4391 /* Skip the chunk. */
4392 return SCTP_DISPOSITION_DISCARD;
4393 case SCTP_CID_ACTION_SKIP_ERR:
4394 /* Generate an ERROR chunk as response. */
4395 hdr = unk_chunk->chunk_hdr;
4396 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4397 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4398 SCTP_PAD4(ntohs(hdr->length)),
4401 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4402 SCTP_CHUNK(err_chunk));
4404 /* Skip the chunk. */
4405 return SCTP_DISPOSITION_CONSUME;
4410 return SCTP_DISPOSITION_DISCARD;
4414 * Discard the chunk.
4416 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4417 * [Too numerous to mention...]
4418 * Verification Tag: No verification needed.
4420 * (endpoint, asoc, chunk)
4423 * (asoc, reply_msg, msg_up, timers, counters)
4425 * The return value is the disposition of the chunk.
4427 enum sctp_disposition sctp_sf_discard_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 *chunk = arg;
4436 if (asoc && !sctp_vtag_verify(chunk, asoc))
4437 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4439 /* Make sure that the chunk has a valid length.
4440 * Since we don't know the chunk type, we use a general
4441 * chunkhdr structure to make a comparison.
4443 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4444 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4447 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4449 return SCTP_DISPOSITION_DISCARD;
4453 * Discard the whole packet.
4457 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4458 * silently discard the OOTB packet and take no further action.
4460 * Verification Tag: No verification necessary
4463 * (endpoint, asoc, chunk)
4466 * (asoc, reply_msg, msg_up, timers, counters)
4468 * The return value is the disposition of the chunk.
4470 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4471 const struct sctp_endpoint *ep,
4472 const struct sctp_association *asoc,
4473 const union sctp_subtype type,
4474 void *arg, struct sctp_cmd_seq *commands)
4476 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4477 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4479 return SCTP_DISPOSITION_CONSUME;
4484 * The other end is violating protocol.
4486 * Section: Not specified
4487 * Verification Tag: Not specified
4489 * (endpoint, asoc, chunk)
4492 * (asoc, reply_msg, msg_up, timers, counters)
4494 * We simply tag the chunk as a violation. The state machine will log
4495 * the violation and continue.
4497 enum sctp_disposition sctp_sf_violation(struct net *net,
4498 const struct sctp_endpoint *ep,
4499 const struct sctp_association *asoc,
4500 const union sctp_subtype type,
4502 struct sctp_cmd_seq *commands)
4504 struct sctp_chunk *chunk = arg;
4506 if (!sctp_vtag_verify(chunk, asoc))
4507 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4509 /* Make sure that the chunk has a valid length. */
4510 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4511 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4514 return SCTP_DISPOSITION_VIOLATION;
4518 * Common function to handle a protocol violation.
4520 static enum sctp_disposition sctp_sf_abort_violation(
4522 const struct sctp_endpoint *ep,
4523 const struct sctp_association *asoc,
4525 struct sctp_cmd_seq *commands,
4526 const __u8 *payload,
4527 const size_t paylen)
4529 struct sctp_packet *packet = NULL;
4530 struct sctp_chunk *chunk = arg;
4531 struct sctp_chunk *abort = NULL;
4533 /* SCTP-AUTH, Section 6.3:
4534 * It should be noted that if the receiver wants to tear
4535 * down an association in an authenticated way only, the
4536 * handling of malformed packets should not result in
4537 * tearing down the association.
4539 * This means that if we only want to abort associations
4540 * in an authenticated way (i.e AUTH+ABORT), then we
4541 * can't destroy this association just because the packet
4544 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4547 /* Make the abort chunk. */
4548 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4553 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4554 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4555 !asoc->peer.i.init_tag) {
4556 struct sctp_initack_chunk *initack;
4558 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4559 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4560 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4562 unsigned int inittag;
4564 inittag = ntohl(initack->init_hdr.init_tag);
4565 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4570 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4571 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4573 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4574 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4575 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4576 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4577 SCTP_ERROR(ECONNREFUSED));
4578 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4579 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4581 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4582 SCTP_ERROR(ECONNABORTED));
4583 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4584 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4585 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4588 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4593 if (sctp_test_T_bit(abort))
4594 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4596 abort->skb->sk = ep->base.sk;
4598 sctp_packet_append_chunk(packet, abort);
4600 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4601 SCTP_PACKET(packet));
4603 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4606 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4609 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4610 return SCTP_DISPOSITION_ABORT;
4613 sctp_chunk_free(abort);
4615 return SCTP_DISPOSITION_NOMEM;
4619 * Handle a protocol violation when the chunk length is invalid.
4620 * "Invalid" length is identified as smaller than the minimal length a
4621 * given chunk can be. For example, a SACK chunk has invalid length
4622 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4624 * We inform the other end by sending an ABORT with a Protocol Violation
4627 * Section: Not specified
4628 * Verification Tag: Nothing to do
4630 * (endpoint, asoc, chunk)
4633 * (reply_msg, msg_up, counters)
4635 * Generate an ABORT chunk and terminate the association.
4637 static enum sctp_disposition sctp_sf_violation_chunklen(
4639 const struct sctp_endpoint *ep,
4640 const struct sctp_association *asoc,
4641 const union sctp_subtype type,
4643 struct sctp_cmd_seq *commands)
4645 static const char err_str[] = "The following chunk had invalid length:";
4647 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4652 * Handle a protocol violation when the parameter length is invalid.
4653 * If the length is smaller than the minimum length of a given parameter,
4654 * or accumulated length in multi parameters exceeds the end of the chunk,
4655 * the length is considered as invalid.
4657 static enum sctp_disposition sctp_sf_violation_paramlen(
4659 const struct sctp_endpoint *ep,
4660 const struct sctp_association *asoc,
4661 const union sctp_subtype type,
4662 void *arg, void *ext,
4663 struct sctp_cmd_seq *commands)
4665 struct sctp_paramhdr *param = ext;
4666 struct sctp_chunk *abort = NULL;
4667 struct sctp_chunk *chunk = arg;
4669 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4672 /* Make the abort chunk. */
4673 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4677 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4678 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4680 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4681 SCTP_ERROR(ECONNABORTED));
4682 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4683 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4684 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4685 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4688 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4689 return SCTP_DISPOSITION_ABORT;
4691 return SCTP_DISPOSITION_NOMEM;
4694 /* Handle a protocol violation when the peer trying to advance the
4695 * cumulative tsn ack to a point beyond the max tsn currently sent.
4697 * We inform the other end by sending an ABORT with a Protocol Violation
4700 static enum sctp_disposition sctp_sf_violation_ctsn(
4702 const struct sctp_endpoint *ep,
4703 const struct sctp_association *asoc,
4704 const union sctp_subtype type,
4706 struct sctp_cmd_seq *commands)
4708 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4710 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4714 /* Handle protocol violation of an invalid chunk bundling. For example,
4715 * when we have an association and we receive bundled INIT-ACK, or
4716 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4717 * statement from the specs. Additionally, there might be an attacker
4718 * on the path and we may not want to continue this communication.
4720 static enum sctp_disposition sctp_sf_violation_chunk(
4722 const struct sctp_endpoint *ep,
4723 const struct sctp_association *asoc,
4724 const union sctp_subtype type,
4726 struct sctp_cmd_seq *commands)
4728 static const char err_str[] = "The following chunk violates protocol:";
4731 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4733 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4736 /***************************************************************************
4737 * These are the state functions for handling primitive (Section 10) events.
4738 ***************************************************************************/
4740 * sctp_sf_do_prm_asoc
4742 * Section: 10.1 ULP-to-SCTP
4745 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4746 * outbound stream count)
4747 * -> association id [,destination transport addr list] [,outbound stream
4750 * This primitive allows the upper layer to initiate an association to a
4751 * specific peer endpoint.
4753 * The peer endpoint shall be specified by one of the transport addresses
4754 * which defines the endpoint (see Section 1.4). If the local SCTP
4755 * instance has not been initialized, the ASSOCIATE is considered an
4757 * [This is not relevant for the kernel implementation since we do all
4758 * initialization at boot time. It we hadn't initialized we wouldn't
4759 * get anywhere near this code.]
4761 * An association id, which is a local handle to the SCTP association,
4762 * will be returned on successful establishment of the association. If
4763 * SCTP is not able to open an SCTP association with the peer endpoint,
4764 * an error is returned.
4765 * [In the kernel implementation, the struct sctp_association needs to
4766 * be created BEFORE causing this primitive to run.]
4768 * Other association parameters may be returned, including the
4769 * complete destination transport addresses of the peer as well as the
4770 * outbound stream count of the local endpoint. One of the transport
4771 * address from the returned destination addresses will be selected by
4772 * the local endpoint as default primary path for sending SCTP packets
4773 * to this peer. The returned "destination transport addr list" can
4774 * be used by the ULP to change the default primary path or to force
4775 * sending a packet to a specific transport address. [All of this
4776 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4779 * Mandatory attributes:
4781 * o local SCTP instance name - obtained from the INITIALIZE operation.
4782 * [This is the argument asoc.]
4783 * o destination transport addr - specified as one of the transport
4784 * addresses of the peer endpoint with which the association is to be
4786 * [This is asoc->peer.active_path.]
4787 * o outbound stream count - the number of outbound streams the ULP
4788 * would like to open towards this peer endpoint.
4789 * [BUG: This is not currently implemented.]
4790 * Optional attributes:
4794 * The return value is a disposition.
4796 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4797 const struct sctp_endpoint *ep,
4798 const struct sctp_association *asoc,
4799 const union sctp_subtype type,
4801 struct sctp_cmd_seq *commands)
4803 struct sctp_association *my_asoc;
4804 struct sctp_chunk *repl;
4806 /* The comment below says that we enter COOKIE-WAIT AFTER
4807 * sending the INIT, but that doesn't actually work in our
4810 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4811 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4813 /* RFC 2960 5.1 Normal Establishment of an Association
4815 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4816 * must provide its Verification Tag (Tag_A) in the Initiate
4817 * Tag field. Tag_A SHOULD be a random number in the range of
4818 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4821 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4825 /* Choose transport for INIT. */
4826 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4829 /* Cast away the const modifier, as we want to just
4830 * rerun it through as a sideffect.
4832 my_asoc = (struct sctp_association *)asoc;
4833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4835 /* After sending the INIT, "A" starts the T1-init timer and
4836 * enters the COOKIE-WAIT state.
4838 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4839 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4841 return SCTP_DISPOSITION_CONSUME;
4844 return SCTP_DISPOSITION_NOMEM;
4848 * Process the SEND primitive.
4850 * Section: 10.1 ULP-to-SCTP
4853 * Format: SEND(association id, buffer address, byte count [,context]
4854 * [,stream id] [,life time] [,destination transport address]
4855 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4858 * This is the main method to send user data via SCTP.
4860 * Mandatory attributes:
4862 * o association id - local handle to the SCTP association
4864 * o buffer address - the location where the user message to be
4865 * transmitted is stored;
4867 * o byte count - The size of the user data in number of bytes;
4869 * Optional attributes:
4871 * o context - an optional 32 bit integer that will be carried in the
4872 * sending failure notification to the ULP if the transportation of
4873 * this User Message fails.
4875 * o stream id - to indicate which stream to send the data on. If not
4876 * specified, stream 0 will be used.
4878 * o life time - specifies the life time of the user data. The user data
4879 * will not be sent by SCTP after the life time expires. This
4880 * parameter can be used to avoid efforts to transmit stale
4881 * user messages. SCTP notifies the ULP if the data cannot be
4882 * initiated to transport (i.e. sent to the destination via SCTP's
4883 * send primitive) within the life time variable. However, the
4884 * user data will be transmitted if SCTP has attempted to transmit a
4885 * chunk before the life time expired.
4887 * o destination transport address - specified as one of the destination
4888 * transport addresses of the peer endpoint to which this packet
4889 * should be sent. Whenever possible, SCTP should use this destination
4890 * transport address for sending the packets, instead of the current
4893 * o unorder flag - this flag, if present, indicates that the user
4894 * would like the data delivered in an unordered fashion to the peer
4895 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4898 * o no-bundle flag - instructs SCTP not to bundle this user data with
4899 * other outbound DATA chunks. SCTP MAY still bundle even when
4900 * this flag is present, when faced with network congestion.
4902 * o payload protocol-id - A 32 bit unsigned integer that is to be
4903 * passed to the peer indicating the type of payload protocol data
4904 * being transmitted. This value is passed as opaque data by SCTP.
4906 * The return value is the disposition.
4908 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4909 const struct sctp_endpoint *ep,
4910 const struct sctp_association *asoc,
4911 const union sctp_subtype type,
4913 struct sctp_cmd_seq *commands)
4915 struct sctp_datamsg *msg = arg;
4917 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4918 return SCTP_DISPOSITION_CONSUME;
4922 * Process the SHUTDOWN primitive.
4927 * Format: SHUTDOWN(association id)
4930 * Gracefully closes an association. Any locally queued user data
4931 * will be delivered to the peer. The association will be terminated only
4932 * after the peer acknowledges all the SCTP packets sent. A success code
4933 * will be returned on successful termination of the association. If
4934 * attempting to terminate the association results in a failure, an error
4935 * code shall be returned.
4937 * Mandatory attributes:
4939 * o association id - local handle to the SCTP association
4941 * Optional attributes:
4945 * The return value is the disposition.
4947 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
4949 const struct sctp_endpoint *ep,
4950 const struct sctp_association *asoc,
4951 const union sctp_subtype type,
4953 struct sctp_cmd_seq *commands)
4955 enum sctp_disposition disposition;
4957 /* From 9.2 Shutdown of an Association
4958 * Upon receipt of the SHUTDOWN primitive from its upper
4959 * layer, the endpoint enters SHUTDOWN-PENDING state and
4960 * remains there until all outstanding data has been
4961 * acknowledged by its peer. The endpoint accepts no new data
4962 * from its upper layer, but retransmits data to the far end
4963 * if necessary to fill gaps.
4965 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4966 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4968 disposition = SCTP_DISPOSITION_CONSUME;
4969 if (sctp_outq_is_empty(&asoc->outqueue)) {
4970 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4978 * Process the ABORT primitive.
4983 * Format: Abort(association id [, cause code])
4986 * Ungracefully closes an association. Any locally queued user data
4987 * will be discarded and an ABORT chunk is sent to the peer. A success code
4988 * will be returned on successful abortion of the association. If
4989 * attempting to abort the association results in a failure, an error
4990 * code shall be returned.
4992 * Mandatory attributes:
4994 * o association id - local handle to the SCTP association
4996 * Optional attributes:
4998 * o cause code - reason of the abort to be passed to the peer
5002 * The return value is the disposition.
5004 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5006 const struct sctp_endpoint *ep,
5007 const struct sctp_association *asoc,
5008 const union sctp_subtype type,
5010 struct sctp_cmd_seq *commands)
5012 /* From 9.1 Abort of an Association
5013 * Upon receipt of the ABORT primitive from its upper
5014 * layer, the endpoint enters CLOSED state and
5015 * discard all outstanding data has been
5016 * acknowledged by its peer. The endpoint accepts no new data
5017 * from its upper layer, but retransmits data to the far end
5018 * if necessary to fill gaps.
5020 struct sctp_chunk *abort = arg;
5023 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5025 /* Even if we can't send the ABORT due to low memory delete the
5026 * TCB. This is a departure from our typical NOMEM handling.
5029 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5030 SCTP_ERROR(ECONNABORTED));
5031 /* Delete the established association. */
5032 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5033 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5035 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5036 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5038 return SCTP_DISPOSITION_ABORT;
5041 /* We tried an illegal operation on an association which is closed. */
5042 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5043 const struct sctp_endpoint *ep,
5044 const struct sctp_association *asoc,
5045 const union sctp_subtype type,
5047 struct sctp_cmd_seq *commands)
5049 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5050 return SCTP_DISPOSITION_CONSUME;
5053 /* We tried an illegal operation on an association which is shutting
5056 enum sctp_disposition sctp_sf_error_shutdown(
5058 const struct sctp_endpoint *ep,
5059 const struct sctp_association *asoc,
5060 const union sctp_subtype type,
5062 struct sctp_cmd_seq *commands)
5064 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5065 SCTP_ERROR(-ESHUTDOWN));
5066 return SCTP_DISPOSITION_CONSUME;
5070 * sctp_cookie_wait_prm_shutdown
5072 * Section: 4 Note: 2
5077 * The RFC does not explicitly address this issue, but is the route through the
5078 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5083 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5085 const struct sctp_endpoint *ep,
5086 const struct sctp_association *asoc,
5087 const union sctp_subtype type,
5089 struct sctp_cmd_seq *commands)
5091 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5092 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5094 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5095 SCTP_STATE(SCTP_STATE_CLOSED));
5097 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5099 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5101 return SCTP_DISPOSITION_DELETE_TCB;
5105 * sctp_cookie_echoed_prm_shutdown
5107 * Section: 4 Note: 2
5112 * The RFC does not explcitly address this issue, but is the route through the
5113 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5118 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5120 const struct sctp_endpoint *ep,
5121 const struct sctp_association *asoc,
5122 const union sctp_subtype type,
5124 struct sctp_cmd_seq *commands)
5126 /* There is a single T1 timer, so we should be able to use
5127 * common function with the COOKIE-WAIT state.
5129 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5133 * sctp_sf_cookie_wait_prm_abort
5135 * Section: 4 Note: 2
5140 * The RFC does not explicitly address this issue, but is the route through the
5141 * state table when someone issues an abort while in COOKIE_WAIT state.
5146 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5148 const struct sctp_endpoint *ep,
5149 const struct sctp_association *asoc,
5150 const union sctp_subtype type,
5152 struct sctp_cmd_seq *commands)
5154 struct sctp_chunk *abort = arg;
5156 /* Stop T1-init timer */
5157 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5158 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5161 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5163 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5164 SCTP_STATE(SCTP_STATE_CLOSED));
5166 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5168 /* Even if we can't send the ABORT due to low memory delete the
5169 * TCB. This is a departure from our typical NOMEM handling.
5172 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5173 SCTP_ERROR(ECONNREFUSED));
5174 /* Delete the established association. */
5175 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5176 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5178 return SCTP_DISPOSITION_ABORT;
5182 * sctp_sf_cookie_echoed_prm_abort
5184 * Section: 4 Note: 3
5189 * The RFC does not explcitly address this issue, but is the route through the
5190 * state table when someone issues an abort while in COOKIE_ECHOED state.
5195 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5197 const struct sctp_endpoint *ep,
5198 const struct sctp_association *asoc,
5199 const union sctp_subtype type,
5201 struct sctp_cmd_seq *commands)
5203 /* There is a single T1 timer, so we should be able to use
5204 * common function with the COOKIE-WAIT state.
5206 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5210 * sctp_sf_shutdown_pending_prm_abort
5215 * The RFC does not explicitly address this issue, but is the route through the
5216 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5221 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5223 const struct sctp_endpoint *ep,
5224 const struct sctp_association *asoc,
5225 const union sctp_subtype type,
5227 struct sctp_cmd_seq *commands)
5229 /* Stop the T5-shutdown guard timer. */
5230 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5231 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5233 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5237 * sctp_sf_shutdown_sent_prm_abort
5242 * The RFC does not explicitly address this issue, but is the route through the
5243 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5248 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5250 const struct sctp_endpoint *ep,
5251 const struct sctp_association *asoc,
5252 const union sctp_subtype type,
5254 struct sctp_cmd_seq *commands)
5256 /* Stop the T2-shutdown timer. */
5257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5258 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5260 /* Stop the T5-shutdown guard timer. */
5261 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5262 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5264 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5268 * sctp_sf_cookie_echoed_prm_abort
5273 * The RFC does not explcitly address this issue, but is the route through the
5274 * state table when someone issues an abort while in COOKIE_ECHOED state.
5279 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5281 const struct sctp_endpoint *ep,
5282 const struct sctp_association *asoc,
5283 const union sctp_subtype type,
5285 struct sctp_cmd_seq *commands)
5287 /* The same T2 timer, so we should be able to use
5288 * common function with the SHUTDOWN-SENT state.
5290 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5294 * Process the REQUESTHEARTBEAT primitive
5297 * J) Request Heartbeat
5299 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5303 * Instructs the local endpoint to perform a HeartBeat on the specified
5304 * destination transport address of the given association. The returned
5305 * result should indicate whether the transmission of the HEARTBEAT
5306 * chunk to the destination address is successful.
5308 * Mandatory attributes:
5310 * o association id - local handle to the SCTP association
5312 * o destination transport address - the transport address of the
5313 * association on which a heartbeat should be issued.
5315 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5317 const struct sctp_endpoint *ep,
5318 const struct sctp_association *asoc,
5319 const union sctp_subtype type,
5321 struct sctp_cmd_seq *commands)
5323 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5324 (struct sctp_transport *)arg, commands))
5325 return SCTP_DISPOSITION_NOMEM;
5328 * RFC 2960 (bis), section 8.3
5330 * D) Request an on-demand HEARTBEAT on a specific destination
5331 * transport address of a given association.
5333 * The endpoint should increment the respective error counter of
5334 * the destination transport address each time a HEARTBEAT is sent
5335 * to that address and not acknowledged within one RTO.
5338 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5339 SCTP_TRANSPORT(arg));
5340 return SCTP_DISPOSITION_CONSUME;
5344 * ADDIP Section 4.1 ASCONF Chunk Procedures
5345 * When an endpoint has an ASCONF signaled change to be sent to the
5346 * remote endpoint it should do A1 to A9
5348 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5349 const struct sctp_endpoint *ep,
5350 const struct sctp_association *asoc,
5351 const union sctp_subtype type,
5353 struct sctp_cmd_seq *commands)
5355 struct sctp_chunk *chunk = arg;
5357 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5358 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5359 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5360 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5361 return SCTP_DISPOSITION_CONSUME;
5364 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5365 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5366 const struct sctp_endpoint *ep,
5367 const struct sctp_association *asoc,
5368 const union sctp_subtype type,
5370 struct sctp_cmd_seq *commands)
5372 struct sctp_chunk *chunk = arg;
5374 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5375 return SCTP_DISPOSITION_CONSUME;
5379 * Ignore the primitive event
5381 * The return value is the disposition of the primitive.
5383 enum sctp_disposition sctp_sf_ignore_primitive(
5385 const struct sctp_endpoint *ep,
5386 const struct sctp_association *asoc,
5387 const union sctp_subtype type,
5389 struct sctp_cmd_seq *commands)
5391 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5394 return SCTP_DISPOSITION_DISCARD;
5397 /***************************************************************************
5398 * These are the state functions for the OTHER events.
5399 ***************************************************************************/
5402 * When the SCTP stack has no more user data to send or retransmit, this
5403 * notification is given to the user. Also, at the time when a user app
5404 * subscribes to this event, if there is no data to be sent or
5405 * retransmit, the stack will immediately send up this notification.
5407 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5409 const struct sctp_endpoint *ep,
5410 const struct sctp_association *asoc,
5411 const union sctp_subtype type,
5413 struct sctp_cmd_seq *commands)
5415 struct sctp_ulpevent *event;
5417 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5419 return SCTP_DISPOSITION_NOMEM;
5421 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5423 return SCTP_DISPOSITION_CONSUME;
5427 * Start the shutdown negotiation.
5430 * Once all its outstanding data has been acknowledged, the endpoint
5431 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5432 * TSN Ack field the last sequential TSN it has received from the peer.
5433 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5434 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5435 * with the updated last sequential TSN received from its peer.
5437 * The return value is the disposition.
5439 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5441 const struct sctp_endpoint *ep,
5442 const struct sctp_association *asoc,
5443 const union sctp_subtype type,
5445 struct sctp_cmd_seq *commands)
5447 struct sctp_chunk *reply;
5449 /* Once all its outstanding data has been acknowledged, the
5450 * endpoint shall send a SHUTDOWN chunk to its peer including
5451 * in the Cumulative TSN Ack field the last sequential TSN it
5452 * has received from the peer.
5454 reply = sctp_make_shutdown(asoc, NULL);
5458 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5459 * T2-shutdown timer.
5461 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5463 /* It shall then start the T2-shutdown timer */
5464 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5465 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5467 /* RFC 4960 Section 9.2
5468 * The sender of the SHUTDOWN MAY also start an overall guard timer
5469 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5471 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5472 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5474 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5475 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5476 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5478 /* and enter the SHUTDOWN-SENT state. */
5479 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5480 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5482 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5484 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5487 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5491 return SCTP_DISPOSITION_CONSUME;
5494 return SCTP_DISPOSITION_NOMEM;
5498 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5502 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5503 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5504 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5505 * endpoint must re-send the SHUTDOWN ACK.
5507 * The return value is the disposition.
5509 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5511 const struct sctp_endpoint *ep,
5512 const struct sctp_association *asoc,
5513 const union sctp_subtype type,
5515 struct sctp_cmd_seq *commands)
5517 struct sctp_chunk *chunk = arg;
5518 struct sctp_chunk *reply;
5520 /* There are 2 ways of getting here:
5521 * 1) called in response to a SHUTDOWN chunk
5522 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5524 * For the case (2), the arg parameter is set to NULL. We need
5525 * to check that we have a chunk before accessing it's fields.
5528 if (!sctp_vtag_verify(chunk, asoc))
5529 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5532 /* Make sure that the SHUTDOWN chunk has a valid length. */
5533 if (!sctp_chunk_length_valid(
5534 chunk, sizeof(struct sctp_shutdown_chunk)))
5535 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5539 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5540 * shall send a SHUTDOWN ACK ...
5542 reply = sctp_make_shutdown_ack(asoc, chunk);
5546 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5547 * the T2-shutdown timer.
5549 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5551 /* and start/restart a T2-shutdown timer of its own, */
5552 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5553 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5555 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5556 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5557 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5559 /* Enter the SHUTDOWN-ACK-SENT state. */
5560 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5561 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5563 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5565 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5568 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5570 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5572 return SCTP_DISPOSITION_CONSUME;
5575 return SCTP_DISPOSITION_NOMEM;
5579 * Ignore the event defined as other
5581 * The return value is the disposition of the event.
5583 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5584 const struct sctp_endpoint *ep,
5585 const struct sctp_association *asoc,
5586 const union sctp_subtype type,
5588 struct sctp_cmd_seq *commands)
5590 pr_debug("%s: the event other type:%d is ignored\n",
5591 __func__, type.other);
5593 return SCTP_DISPOSITION_DISCARD;
5596 /************************************************************
5597 * These are the state functions for handling timeout events.
5598 ************************************************************/
5603 * Section: 6.3.3 Handle T3-rtx Expiration
5605 * Whenever the retransmission timer T3-rtx expires for a destination
5606 * address, do the following:
5609 * The return value is the disposition of the chunk.
5611 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5612 const struct sctp_endpoint *ep,
5613 const struct sctp_association *asoc,
5614 const union sctp_subtype type,
5616 struct sctp_cmd_seq *commands)
5618 struct sctp_transport *transport = arg;
5620 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5622 if (asoc->overall_error_count >= asoc->max_retrans) {
5623 if (asoc->peer.zero_window_announced &&
5624 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5626 * We are here likely because the receiver had its rwnd
5627 * closed for a while and we have not been able to
5628 * transmit the locally queued data within the maximum
5629 * retransmission attempts limit. Start the T5
5630 * shutdown guard timer to give the receiver one last
5631 * chance and some additional time to recover before
5634 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5635 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5637 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5638 SCTP_ERROR(ETIMEDOUT));
5639 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5640 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5641 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5642 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5643 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5644 return SCTP_DISPOSITION_DELETE_TCB;
5648 /* E1) For the destination address for which the timer
5649 * expires, adjust its ssthresh with rules defined in Section
5650 * 7.2.3 and set the cwnd <- MTU.
5653 /* E2) For the destination address for which the timer
5654 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5655 * maximum value discussed in rule C7 above (RTO.max) may be
5656 * used to provide an upper bound to this doubling operation.
5659 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5660 * outstanding DATA chunks for the address for which the
5661 * T3-rtx has expired will fit into a single packet, subject
5662 * to the MTU constraint for the path corresponding to the
5663 * destination transport address to which the retransmission
5664 * is being sent (this may be different from the address for
5665 * which the timer expires [see Section 6.4]). Call this
5666 * value K. Bundle and retransmit those K DATA chunks in a
5667 * single packet to the destination endpoint.
5669 * Note: Any DATA chunks that were sent to the address for
5670 * which the T3-rtx timer expired but did not fit in one MTU
5671 * (rule E3 above), should be marked for retransmission and
5672 * sent as soon as cwnd allows (normally when a SACK arrives).
5675 /* Do some failure management (Section 8.2). */
5676 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5678 /* NB: Rules E4 and F1 are implicit in R1. */
5679 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5681 return SCTP_DISPOSITION_CONSUME;
5685 * Generate delayed SACK on timeout
5687 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5689 * The guidelines on delayed acknowledgement algorithm specified in
5690 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5691 * acknowledgement SHOULD be generated for at least every second packet
5692 * (not every second DATA chunk) received, and SHOULD be generated
5693 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5694 * some situations it may be beneficial for an SCTP transmitter to be
5695 * more conservative than the algorithms detailed in this document
5696 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5697 * the following algorithms allow.
5699 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5700 const struct sctp_endpoint *ep,
5701 const struct sctp_association *asoc,
5702 const union sctp_subtype type,
5704 struct sctp_cmd_seq *commands)
5706 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5707 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5708 return SCTP_DISPOSITION_CONSUME;
5712 * sctp_sf_t1_init_timer_expire
5714 * Section: 4 Note: 2
5719 * RFC 2960 Section 4 Notes
5720 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5721 * and re-start the T1-init timer without changing state. This MUST
5722 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5723 * endpoint MUST abort the initialization process and report the
5724 * error to SCTP user.
5730 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5732 const struct sctp_endpoint *ep,
5733 const struct sctp_association *asoc,
5734 const union sctp_subtype type,
5736 struct sctp_cmd_seq *commands)
5738 int attempts = asoc->init_err_counter + 1;
5739 struct sctp_chunk *repl = NULL;
5740 struct sctp_bind_addr *bp;
5742 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5744 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5746 if (attempts <= asoc->max_init_attempts) {
5747 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5748 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5750 return SCTP_DISPOSITION_NOMEM;
5752 /* Choose transport for INIT. */
5753 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5756 /* Issue a sideeffect to do the needed accounting. */
5757 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5758 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5762 pr_debug("%s: giving up on INIT, attempts:%d "
5763 "max_init_attempts:%d\n", __func__, attempts,
5764 asoc->max_init_attempts);
5766 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5767 SCTP_ERROR(ETIMEDOUT));
5768 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5769 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5770 return SCTP_DISPOSITION_DELETE_TCB;
5773 return SCTP_DISPOSITION_CONSUME;
5777 * sctp_sf_t1_cookie_timer_expire
5779 * Section: 4 Note: 2
5784 * RFC 2960 Section 4 Notes
5785 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5786 * COOKIE ECHO and re-start the T1-cookie timer without changing
5787 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5788 * After that, the endpoint MUST abort the initialization process and
5789 * report the error to SCTP user.
5795 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5797 const struct sctp_endpoint *ep,
5798 const struct sctp_association *asoc,
5799 const union sctp_subtype type,
5801 struct sctp_cmd_seq *commands)
5803 int attempts = asoc->init_err_counter + 1;
5804 struct sctp_chunk *repl = NULL;
5806 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5808 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5810 if (attempts <= asoc->max_init_attempts) {
5811 repl = sctp_make_cookie_echo(asoc, NULL);
5813 return SCTP_DISPOSITION_NOMEM;
5815 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5817 /* Issue a sideeffect to do the needed accounting. */
5818 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5819 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5823 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5824 SCTP_ERROR(ETIMEDOUT));
5825 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5826 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5827 return SCTP_DISPOSITION_DELETE_TCB;
5830 return SCTP_DISPOSITION_CONSUME;
5833 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5834 * with the updated last sequential TSN received from its peer.
5836 * An endpoint should limit the number of retransmissions of the
5837 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5838 * If this threshold is exceeded the endpoint should destroy the TCB and
5839 * MUST report the peer endpoint unreachable to the upper layer (and
5840 * thus the association enters the CLOSED state). The reception of any
5841 * packet from its peer (i.e. as the peer sends all of its queued DATA
5842 * chunks) should clear the endpoint's retransmission count and restart
5843 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5844 * all of its queued DATA chunks that have not yet been sent.
5846 enum sctp_disposition sctp_sf_t2_timer_expire(
5848 const struct sctp_endpoint *ep,
5849 const struct sctp_association *asoc,
5850 const union sctp_subtype type,
5852 struct sctp_cmd_seq *commands)
5854 struct sctp_chunk *reply = NULL;
5856 pr_debug("%s: timer T2 expired\n", __func__);
5858 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5860 ((struct sctp_association *)asoc)->shutdown_retries++;
5862 if (asoc->overall_error_count >= asoc->max_retrans) {
5863 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5864 SCTP_ERROR(ETIMEDOUT));
5865 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5866 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5867 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5868 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5869 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5870 return SCTP_DISPOSITION_DELETE_TCB;
5873 switch (asoc->state) {
5874 case SCTP_STATE_SHUTDOWN_SENT:
5875 reply = sctp_make_shutdown(asoc, NULL);
5878 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5879 reply = sctp_make_shutdown_ack(asoc, NULL);
5890 /* Do some failure management (Section 8.2).
5891 * If we remove the transport an SHUTDOWN was last sent to, don't
5892 * do failure management.
5894 if (asoc->shutdown_last_sent_to)
5895 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5896 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5898 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5899 * the T2-shutdown timer.
5901 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5903 /* Restart the T2-shutdown timer. */
5904 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5905 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5906 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5907 return SCTP_DISPOSITION_CONSUME;
5910 return SCTP_DISPOSITION_NOMEM;
5914 * ADDIP Section 4.1 ASCONF CHunk Procedures
5915 * If the T4 RTO timer expires the endpoint should do B1 to B5
5917 enum sctp_disposition sctp_sf_t4_timer_expire(
5919 const struct sctp_endpoint *ep,
5920 const struct sctp_association *asoc,
5921 const union sctp_subtype type,
5923 struct sctp_cmd_seq *commands)
5925 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5926 struct sctp_transport *transport = chunk->transport;
5928 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5930 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5931 * detection on the appropriate destination address as defined in
5932 * RFC2960 [5] section 8.1 and 8.2.
5935 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5936 SCTP_TRANSPORT(transport));
5938 /* Reconfig T4 timer and transport. */
5939 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5941 /* ADDIP 4.1 B2) Increment the association error counters and perform
5942 * endpoint failure detection on the association as defined in
5943 * RFC2960 [5] section 8.1 and 8.2.
5944 * association error counter is incremented in SCTP_CMD_STRIKE.
5946 if (asoc->overall_error_count >= asoc->max_retrans) {
5947 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5948 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5949 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5950 SCTP_ERROR(ETIMEDOUT));
5951 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5952 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5953 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5954 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5955 return SCTP_DISPOSITION_ABORT;
5958 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5959 * the ASCONF chunk was sent by doubling the RTO timer value.
5960 * This is done in SCTP_CMD_STRIKE.
5963 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5964 * choose an alternate destination address (please refer to RFC2960
5965 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5966 * chunk, it MUST be the same (including its serial number) as the last
5969 sctp_chunk_hold(asoc->addip_last_asconf);
5970 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5971 SCTP_CHUNK(asoc->addip_last_asconf));
5973 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5974 * destination is selected, then the RTO used will be that of the new
5975 * destination address.
5977 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5978 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5980 return SCTP_DISPOSITION_CONSUME;
5983 /* sctpimpguide-05 Section 2.12.2
5984 * The sender of the SHUTDOWN MAY also start an overall guard timer
5985 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5986 * At the expiration of this timer the sender SHOULD abort the association
5987 * by sending an ABORT chunk.
5989 enum sctp_disposition sctp_sf_t5_timer_expire(
5991 const struct sctp_endpoint *ep,
5992 const struct sctp_association *asoc,
5993 const union sctp_subtype type,
5995 struct sctp_cmd_seq *commands)
5997 struct sctp_chunk *reply = NULL;
5999 pr_debug("%s: timer T5 expired\n", __func__);
6001 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6003 reply = sctp_make_abort(asoc, NULL, 0);
6007 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6008 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6009 SCTP_ERROR(ETIMEDOUT));
6010 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6011 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6013 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6014 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6016 return SCTP_DISPOSITION_DELETE_TCB;
6018 return SCTP_DISPOSITION_NOMEM;
6021 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6022 * the association is automatically closed by starting the shutdown process.
6023 * The work that needs to be done is same as when SHUTDOWN is initiated by
6024 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6026 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6028 const struct sctp_endpoint *ep,
6029 const struct sctp_association *asoc,
6030 const union sctp_subtype type,
6032 struct sctp_cmd_seq *commands)
6034 enum sctp_disposition disposition;
6036 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6038 /* From 9.2 Shutdown of an Association
6039 * Upon receipt of the SHUTDOWN primitive from its upper
6040 * layer, the endpoint enters SHUTDOWN-PENDING state and
6041 * remains there until all outstanding data has been
6042 * acknowledged by its peer. The endpoint accepts no new data
6043 * from its upper layer, but retransmits data to the far end
6044 * if necessary to fill gaps.
6046 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6047 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6049 disposition = SCTP_DISPOSITION_CONSUME;
6050 if (sctp_outq_is_empty(&asoc->outqueue)) {
6051 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6058 /*****************************************************************************
6059 * These are sa state functions which could apply to all types of events.
6060 ****************************************************************************/
6063 * This table entry is not implemented.
6066 * (endpoint, asoc, chunk)
6068 * The return value is the disposition of the chunk.
6070 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6071 const struct sctp_endpoint *ep,
6072 const struct sctp_association *asoc,
6073 const union sctp_subtype type,
6074 void *arg, struct sctp_cmd_seq *commands)
6076 return SCTP_DISPOSITION_NOT_IMPL;
6080 * This table entry represents a bug.
6083 * (endpoint, asoc, chunk)
6085 * The return value is the disposition of the chunk.
6087 enum sctp_disposition sctp_sf_bug(struct net *net,
6088 const struct sctp_endpoint *ep,
6089 const struct sctp_association *asoc,
6090 const union sctp_subtype type,
6091 void *arg, struct sctp_cmd_seq *commands)
6093 return SCTP_DISPOSITION_BUG;
6097 * This table entry represents the firing of a timer in the wrong state.
6098 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6099 * when the association is in the wrong state. This event should
6100 * be ignored, so as to prevent any rearming of the timer.
6103 * (endpoint, asoc, chunk)
6105 * The return value is the disposition of the chunk.
6107 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
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 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6116 return SCTP_DISPOSITION_CONSUME;
6119 /********************************************************************
6120 * 2nd Level Abstractions
6121 ********************************************************************/
6123 /* Pull the SACK chunk based on the SACK header. */
6124 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6126 struct sctp_sackhdr *sack;
6131 /* Protect ourselves from reading too far into
6132 * the skb from a bogus sender.
6134 sack = (struct sctp_sackhdr *) chunk->skb->data;
6136 num_blocks = ntohs(sack->num_gap_ack_blocks);
6137 num_dup_tsns = ntohs(sack->num_dup_tsns);
6138 len = sizeof(struct sctp_sackhdr);
6139 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6140 if (len > chunk->skb->len)
6143 skb_pull(chunk->skb, len);
6148 /* Create an ABORT packet to be sent as a response, with the specified
6151 static struct sctp_packet *sctp_abort_pkt_new(
6153 const struct sctp_endpoint *ep,
6154 const struct sctp_association *asoc,
6155 struct sctp_chunk *chunk,
6156 const void *payload, size_t paylen)
6158 struct sctp_packet *packet;
6159 struct sctp_chunk *abort;
6161 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6165 * The T bit will be set if the asoc is NULL.
6167 abort = sctp_make_abort(asoc, chunk, paylen);
6169 sctp_ootb_pkt_free(packet);
6173 /* Reflect vtag if T-Bit is set */
6174 if (sctp_test_T_bit(abort))
6175 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6177 /* Add specified error causes, i.e., payload, to the
6180 sctp_addto_chunk(abort, paylen, payload);
6182 /* Set the skb to the belonging sock for accounting. */
6183 abort->skb->sk = ep->base.sk;
6185 sctp_packet_append_chunk(packet, abort);
6192 /* Allocate a packet for responding in the OOTB conditions. */
6193 static struct sctp_packet *sctp_ootb_pkt_new(
6195 const struct sctp_association *asoc,
6196 const struct sctp_chunk *chunk)
6198 struct sctp_transport *transport;
6199 struct sctp_packet *packet;
6203 /* Get the source and destination port from the inbound packet. */
6204 sport = ntohs(chunk->sctp_hdr->dest);
6205 dport = ntohs(chunk->sctp_hdr->source);
6207 /* The V-tag is going to be the same as the inbound packet if no
6208 * association exists, otherwise, use the peer's vtag.
6211 /* Special case the INIT-ACK as there is no peer's vtag
6214 switch (chunk->chunk_hdr->type) {
6216 case SCTP_CID_INIT_ACK:
6218 struct sctp_initack_chunk *initack;
6220 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6221 vtag = ntohl(initack->init_hdr.init_tag);
6225 vtag = asoc->peer.i.init_tag;
6229 /* Special case the INIT and stale COOKIE_ECHO as there is no
6232 switch (chunk->chunk_hdr->type) {
6235 struct sctp_init_chunk *init;
6237 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6238 vtag = ntohl(init->init_hdr.init_tag);
6242 vtag = ntohl(chunk->sctp_hdr->vtag);
6247 /* Make a transport for the bucket, Eliza... */
6248 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6252 /* Cache a route for the transport with the chunk's destination as
6253 * the source address.
6255 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6256 sctp_sk(net->sctp.ctl_sock));
6258 packet = &transport->packet;
6259 sctp_packet_init(packet, transport, sport, dport);
6260 sctp_packet_config(packet, vtag, 0);
6268 /* Free the packet allocated earlier for responding in the OOTB condition. */
6269 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6271 sctp_transport_free(packet->transport);
6274 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6275 static void sctp_send_stale_cookie_err(struct net *net,
6276 const struct sctp_endpoint *ep,
6277 const struct sctp_association *asoc,
6278 const struct sctp_chunk *chunk,
6279 struct sctp_cmd_seq *commands,
6280 struct sctp_chunk *err_chunk)
6282 struct sctp_packet *packet;
6285 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6287 struct sctp_signed_cookie *cookie;
6289 /* Override the OOTB vtag from the cookie. */
6290 cookie = chunk->subh.cookie_hdr;
6291 packet->vtag = cookie->c.peer_vtag;
6293 /* Set the skb to the belonging sock for accounting. */
6294 err_chunk->skb->sk = ep->base.sk;
6295 sctp_packet_append_chunk(packet, err_chunk);
6296 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6297 SCTP_PACKET(packet));
6298 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6300 sctp_chunk_free (err_chunk);
6305 /* Process a data chunk */
6306 static int sctp_eat_data(const struct sctp_association *asoc,
6307 struct sctp_chunk *chunk,
6308 struct sctp_cmd_seq *commands)
6310 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6311 struct sock *sk = asoc->base.sk;
6312 struct net *net = sock_net(sk);
6313 struct sctp_datahdr *data_hdr;
6314 struct sctp_chunk *err;
6315 enum sctp_verb deliver;
6322 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6323 chunk->subh.data_hdr = data_hdr;
6324 skb_pull(chunk->skb, sizeof(*data_hdr));
6326 tsn = ntohl(data_hdr->tsn);
6327 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6329 /* ASSERT: Now skb->data is really the user data. */
6331 /* Process ECN based congestion.
6333 * Since the chunk structure is reused for all chunks within
6334 * a packet, we use ecn_ce_done to track if we've already
6335 * done CE processing for this packet.
6337 * We need to do ECN processing even if we plan to discard the
6341 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6342 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6343 chunk->ecn_ce_done = 1;
6345 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6346 /* Do real work as sideffect. */
6347 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6352 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6354 /* The TSN is too high--silently discard the chunk and
6355 * count on it getting retransmitted later.
6358 chunk->asoc->stats.outofseqtsns++;
6359 return SCTP_IERROR_HIGH_TSN;
6360 } else if (tmp > 0) {
6361 /* This is a duplicate. Record it. */
6362 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6363 return SCTP_IERROR_DUP_TSN;
6366 /* This is a new TSN. */
6368 /* Discard if there is no room in the receive window.
6369 * Actually, allow a little bit of overflow (up to a MTU).
6371 datalen = ntohs(chunk->chunk_hdr->length);
6372 datalen -= sizeof(struct sctp_data_chunk);
6374 deliver = SCTP_CMD_CHUNK_ULP;
6376 /* Think about partial delivery. */
6377 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6379 /* Even if we don't accept this chunk there is
6382 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6385 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6386 * seems a bit troublesome in that frag_point varies based on
6387 * PMTU. In cases, such as loopback, this might be a rather
6390 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6391 (datalen > asoc->rwnd + asoc->frag_point))) {
6393 /* If this is the next TSN, consider reneging to make
6394 * room. Note: Playing nice with a confused sender. A
6395 * malicious sender can still eat up all our buffer
6396 * space and in the future we may want to detect and
6397 * do more drastic reneging.
6399 if (sctp_tsnmap_has_gap(map) &&
6400 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6401 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6402 deliver = SCTP_CMD_RENEGE;
6404 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6405 __func__, tsn, datalen, asoc->rwnd);
6407 return SCTP_IERROR_IGNORE_TSN;
6412 * Also try to renege to limit our memory usage in the event that
6413 * we are under memory pressure
6414 * If we can't renege, don't worry about it, the sk_rmem_schedule
6415 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6416 * memory usage too much
6418 if (*sk->sk_prot_creator->memory_pressure) {
6419 if (sctp_tsnmap_has_gap(map) &&
6420 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6421 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6423 deliver = SCTP_CMD_RENEGE;
6428 * Section 3.3.10.9 No User Data (9)
6432 * No User Data: This error cause is returned to the originator of a
6433 * DATA chunk if a received DATA chunk has no user data.
6435 if (unlikely(0 == datalen)) {
6436 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6438 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6441 /* We are going to ABORT, so we might as well stop
6442 * processing the rest of the chunks in the packet.
6444 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6445 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6446 SCTP_ERROR(ECONNABORTED));
6447 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6448 SCTP_PERR(SCTP_ERROR_NO_DATA));
6449 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6450 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6451 return SCTP_IERROR_NO_DATA;
6454 chunk->data_accepted = 1;
6456 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6457 * if we renege and the chunk arrives again.
6459 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6460 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6462 chunk->asoc->stats.iuodchunks++;
6464 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6466 chunk->asoc->stats.iodchunks++;
6470 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6472 * If an endpoint receive a DATA chunk with an invalid stream
6473 * identifier, it shall acknowledge the reception of the DATA chunk
6474 * following the normal procedure, immediately send an ERROR chunk
6475 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6476 * and discard the DATA chunk.
6478 sid = ntohs(data_hdr->stream);
6479 if (sid >= asoc->stream.incnt) {
6480 /* Mark tsn as received even though we drop it */
6481 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6483 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6485 sizeof(data_hdr->stream),
6488 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6490 return SCTP_IERROR_BAD_STREAM;
6493 /* Check to see if the SSN is possible for this TSN.
6494 * The biggest gap we can record is 4K wide. Since SSNs wrap
6495 * at an unsigned short, there is no way that an SSN can
6496 * wrap and for a valid TSN. We can simply check if the current
6497 * SSN is smaller then the next expected one. If it is, it wrapped
6500 ssn = ntohs(data_hdr->ssn);
6501 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
6502 return SCTP_IERROR_PROTO_VIOLATION;
6504 /* Send the data up to the user. Note: Schedule the
6505 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6506 * chunk needs the updated rwnd.
6508 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6510 return SCTP_IERROR_NO_ERROR;