Linux 3.12.28
[linux/fpc-iii.git] / net / sctp / sm_statefuns.c
blob5dcfe8ca7f69dca6f56bcbc4ddc12be4f5eed1e9
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)
16 * any later version.
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, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <linux-sctp@vger.kernel.org>
33 * Written or modified by:
34 * La Monte H.P. Yarroll <piggy@acm.org>
35 * Karl Knutson <karl@athena.chicago.il.us>
36 * Mathew Kotowsky <kotowsky@sctp.org>
37 * Sridhar Samudrala <samudrala@us.ibm.com>
38 * Jon Grimm <jgrimm@us.ibm.com>
39 * Hui Huang <hui.huang@nokia.com>
40 * Dajiang Zhang <dajiang.zhang@nokia.com>
41 * Daisy Chang <daisyc@us.ibm.com>
42 * Ardelle Fan <ardelle.fan@intel.com>
43 * Ryan Layer <rmlayer@us.ibm.com>
44 * Kevin Gao <kevin.gao@intel.com>
47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49 #include <linux/types.h>
50 #include <linux/kernel.h>
51 #include <linux/ip.h>
52 #include <linux/ipv6.h>
53 #include <linux/net.h>
54 #include <linux/inet.h>
55 #include <linux/slab.h>
56 #include <net/sock.h>
57 #include <net/inet_ecn.h>
58 #include <linux/skbuff.h>
59 #include <net/sctp/sctp.h>
60 #include <net/sctp/sm.h>
61 #include <net/sctp/structs.h>
63 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
64 const struct sctp_endpoint *ep,
65 const struct sctp_association *asoc,
66 struct sctp_chunk *chunk,
67 const void *payload,
68 size_t paylen);
69 static int sctp_eat_data(const struct sctp_association *asoc,
70 struct sctp_chunk *chunk,
71 sctp_cmd_seq_t *commands);
72 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
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 sctp_cmd_seq_t *commands,
80 struct sctp_chunk *err_chunk);
81 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
82 const struct sctp_endpoint *ep,
83 const struct sctp_association *asoc,
84 const sctp_subtype_t type,
85 void *arg,
86 sctp_cmd_seq_t *commands);
87 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
88 const struct sctp_endpoint *ep,
89 const struct sctp_association *asoc,
90 const sctp_subtype_t type,
91 void *arg,
92 sctp_cmd_seq_t *commands);
93 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
94 const struct sctp_endpoint *ep,
95 const struct sctp_association *asoc,
96 const sctp_subtype_t type,
97 void *arg,
98 sctp_cmd_seq_t *commands);
99 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
101 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
102 sctp_cmd_seq_t *commands,
103 __be16 error, int sk_err,
104 const struct sctp_association *asoc,
105 struct sctp_transport *transport);
107 static sctp_disposition_t sctp_sf_abort_violation(
108 struct net *net,
109 const struct sctp_endpoint *ep,
110 const struct sctp_association *asoc,
111 void *arg,
112 sctp_cmd_seq_t *commands,
113 const __u8 *payload,
114 const size_t paylen);
116 static sctp_disposition_t sctp_sf_violation_chunklen(
117 struct net *net,
118 const struct sctp_endpoint *ep,
119 const struct sctp_association *asoc,
120 const sctp_subtype_t type,
121 void *arg,
122 sctp_cmd_seq_t *commands);
124 static sctp_disposition_t sctp_sf_violation_paramlen(
125 struct net *net,
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const sctp_subtype_t type,
129 void *arg, void *ext,
130 sctp_cmd_seq_t *commands);
132 static sctp_disposition_t sctp_sf_violation_ctsn(
133 struct net *net,
134 const struct sctp_endpoint *ep,
135 const struct sctp_association *asoc,
136 const sctp_subtype_t type,
137 void *arg,
138 sctp_cmd_seq_t *commands);
140 static sctp_disposition_t sctp_sf_violation_chunk(
141 struct net *net,
142 const struct sctp_endpoint *ep,
143 const struct sctp_association *asoc,
144 const sctp_subtype_t type,
145 void *arg,
146 sctp_cmd_seq_t *commands);
148 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
149 const struct sctp_endpoint *ep,
150 const struct sctp_association *asoc,
151 const sctp_subtype_t type,
152 struct sctp_chunk *chunk);
154 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
155 const struct sctp_endpoint *ep,
156 const struct sctp_association *asoc,
157 const sctp_subtype_t type,
158 void *arg,
159 sctp_cmd_seq_t *commands);
161 /* Small helper function that checks if the chunk length
162 * is of the appropriate length. The 'required_length' argument
163 * is set to be the size of a specific chunk we are testing.
164 * Return Values: 1 = Valid length
165 * 0 = Invalid length
168 static inline int
169 sctp_chunk_length_valid(struct sctp_chunk *chunk,
170 __u16 required_length)
172 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
174 if (unlikely(chunk_length < required_length))
175 return 0;
177 return 1;
180 /**********************************************************
181 * These are the state functions for handling chunk events.
182 **********************************************************/
185 * Process the final SHUTDOWN COMPLETE.
187 * Section: 4 (C) (diagram), 9.2
188 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
189 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
190 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
191 * should stop the T2-shutdown timer and remove all knowledge of the
192 * association (and thus the association enters the CLOSED state).
194 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
195 * C) Rules for packet carrying SHUTDOWN COMPLETE:
196 * ...
197 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
198 * if the Verification Tag field of the packet matches its own tag and
199 * the T bit is not set
200 * OR
201 * it is set to its peer's tag and the T bit is set in the Chunk
202 * Flags.
203 * Otherwise, the receiver MUST silently discard the packet
204 * and take no further action. An endpoint MUST ignore the
205 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
207 * Inputs
208 * (endpoint, asoc, chunk)
210 * Outputs
211 * (asoc, reply_msg, msg_up, timers, counters)
213 * The return value is the disposition of the chunk.
215 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
216 const struct sctp_endpoint *ep,
217 const struct sctp_association *asoc,
218 const sctp_subtype_t type,
219 void *arg,
220 sctp_cmd_seq_t *commands)
222 struct sctp_chunk *chunk = arg;
223 struct sctp_ulpevent *ev;
225 if (!sctp_vtag_verify_either(chunk, asoc))
226 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
228 /* RFC 2960 6.10 Bundling
230 * An endpoint MUST NOT bundle INIT, INIT ACK or
231 * SHUTDOWN COMPLETE with any other chunks.
233 if (!chunk->singleton)
234 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
236 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
237 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
238 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
239 commands);
241 /* RFC 2960 10.2 SCTP-to-ULP
243 * H) SHUTDOWN COMPLETE notification
245 * When SCTP completes the shutdown procedures (section 9.2) this
246 * notification is passed to the upper layer.
248 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
249 0, 0, 0, NULL, GFP_ATOMIC);
250 if (ev)
251 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
252 SCTP_ULPEVENT(ev));
254 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
255 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
256 * not the chunk should be discarded. If the endpoint is in
257 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
258 * T2-shutdown timer and remove all knowledge of the
259 * association (and thus the association enters the CLOSED
260 * state).
262 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
263 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
265 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
266 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
268 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
269 SCTP_STATE(SCTP_STATE_CLOSED));
271 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
272 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
274 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
276 return SCTP_DISPOSITION_DELETE_TCB;
280 * Respond to a normal INIT chunk.
281 * We are the side that is being asked for an association.
283 * Section: 5.1 Normal Establishment of an Association, B
284 * B) "Z" shall respond immediately with an INIT ACK chunk. The
285 * destination IP address of the INIT ACK MUST be set to the source
286 * IP address of the INIT to which this INIT ACK is responding. In
287 * the response, besides filling in other parameters, "Z" must set the
288 * Verification Tag field to Tag_A, and also provide its own
289 * Verification Tag (Tag_Z) in the Initiate Tag field.
291 * Verification Tag: Must be 0.
293 * Inputs
294 * (endpoint, asoc, chunk)
296 * Outputs
297 * (asoc, reply_msg, msg_up, timers, counters)
299 * The return value is the disposition of the chunk.
301 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
302 const struct sctp_endpoint *ep,
303 const struct sctp_association *asoc,
304 const sctp_subtype_t type,
305 void *arg,
306 sctp_cmd_seq_t *commands)
308 struct sctp_chunk *chunk = arg;
309 struct sctp_chunk *repl;
310 struct sctp_association *new_asoc;
311 struct sctp_chunk *err_chunk;
312 struct sctp_packet *packet;
313 sctp_unrecognized_param_t *unk_param;
314 int len;
316 /* 6.10 Bundling
317 * An endpoint MUST NOT bundle INIT, INIT ACK or
318 * SHUTDOWN COMPLETE with any other chunks.
320 * IG Section 2.11.2
321 * Furthermore, we require that the receiver of an INIT chunk MUST
322 * enforce these rules by silently discarding an arriving packet
323 * with an INIT chunk that is bundled with other chunks.
325 if (!chunk->singleton)
326 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
328 /* If the packet is an OOTB packet which is temporarily on the
329 * control endpoint, respond with an ABORT.
331 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
332 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
333 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
336 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
337 * Tag.
339 if (chunk->sctp_hdr->vtag != 0)
340 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
342 /* Make sure that the INIT chunk has a valid length.
343 * Normally, this would cause an ABORT with a Protocol Violation
344 * error, but since we don't have an association, we'll
345 * just discard the packet.
347 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
348 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
350 /* If the INIT is coming toward a closing socket, we'll send back
351 * and ABORT. Essentially, this catches the race of INIT being
352 * backloged to the socket at the same time as the user isses close().
353 * Since the socket and all its associations are going away, we
354 * can treat this OOTB
356 if (sctp_sstate(ep->base.sk, CLOSING))
357 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
359 /* Verify the INIT chunk before processing it. */
360 err_chunk = NULL;
361 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
362 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
363 &err_chunk)) {
364 /* This chunk contains fatal error. It is to be discarded.
365 * Send an ABORT, with causes if there is any.
367 if (err_chunk) {
368 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
369 (__u8 *)(err_chunk->chunk_hdr) +
370 sizeof(sctp_chunkhdr_t),
371 ntohs(err_chunk->chunk_hdr->length) -
372 sizeof(sctp_chunkhdr_t));
374 sctp_chunk_free(err_chunk);
376 if (packet) {
377 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
378 SCTP_PACKET(packet));
379 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
380 return SCTP_DISPOSITION_CONSUME;
381 } else {
382 return SCTP_DISPOSITION_NOMEM;
384 } else {
385 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
386 commands);
390 /* Grab the INIT header. */
391 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
393 /* Tag the variable length parameters. */
394 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
396 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
397 if (!new_asoc)
398 goto nomem;
400 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
401 sctp_scope(sctp_source(chunk)),
402 GFP_ATOMIC) < 0)
403 goto nomem_init;
405 /* The call, sctp_process_init(), can fail on memory allocation. */
406 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
407 (sctp_init_chunk_t *)chunk->chunk_hdr,
408 GFP_ATOMIC))
409 goto nomem_init;
411 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
413 /* If there are errors need to be reported for unknown parameters,
414 * make sure to reserve enough room in the INIT ACK for them.
416 len = 0;
417 if (err_chunk)
418 len = ntohs(err_chunk->chunk_hdr->length) -
419 sizeof(sctp_chunkhdr_t);
421 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
422 if (!repl)
423 goto nomem_init;
425 /* If there are errors need to be reported for unknown parameters,
426 * include them in the outgoing INIT ACK as "Unrecognized parameter"
427 * parameter.
429 if (err_chunk) {
430 /* Get the "Unrecognized parameter" parameter(s) out of the
431 * ERROR chunk generated by sctp_verify_init(). Since the
432 * error cause code for "unknown parameter" and the
433 * "Unrecognized parameter" type is the same, we can
434 * construct the parameters in INIT ACK by copying the
435 * ERROR causes over.
437 unk_param = (sctp_unrecognized_param_t *)
438 ((__u8 *)(err_chunk->chunk_hdr) +
439 sizeof(sctp_chunkhdr_t));
440 /* Replace the cause code with the "Unrecognized parameter"
441 * parameter type.
443 sctp_addto_chunk(repl, len, unk_param);
444 sctp_chunk_free(err_chunk);
447 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
449 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
452 * Note: After sending out INIT ACK with the State Cookie parameter,
453 * "Z" MUST NOT allocate any resources, nor keep any states for the
454 * new association. Otherwise, "Z" will be vulnerable to resource
455 * attacks.
457 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
459 return SCTP_DISPOSITION_DELETE_TCB;
461 nomem_init:
462 sctp_association_free(new_asoc);
463 nomem:
464 if (err_chunk)
465 sctp_chunk_free(err_chunk);
466 return SCTP_DISPOSITION_NOMEM;
470 * Respond to a normal INIT ACK chunk.
471 * We are the side that is initiating the association.
473 * Section: 5.1 Normal Establishment of an Association, C
474 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
475 * timer and leave COOKIE-WAIT state. "A" shall then send the State
476 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
477 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
479 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
480 * DATA chunks, but it MUST be the first chunk in the packet and
481 * until the COOKIE ACK is returned the sender MUST NOT send any
482 * other packets to the peer.
484 * Verification Tag: 3.3.3
485 * If the value of the Initiate Tag in a received INIT ACK chunk is
486 * found to be 0, the receiver MUST treat it as an error and close the
487 * association by transmitting an ABORT.
489 * Inputs
490 * (endpoint, asoc, chunk)
492 * Outputs
493 * (asoc, reply_msg, msg_up, timers, counters)
495 * The return value is the disposition of the chunk.
497 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
498 const struct sctp_endpoint *ep,
499 const struct sctp_association *asoc,
500 const sctp_subtype_t type,
501 void *arg,
502 sctp_cmd_seq_t *commands)
504 struct sctp_chunk *chunk = arg;
505 sctp_init_chunk_t *initchunk;
506 struct sctp_chunk *err_chunk;
507 struct sctp_packet *packet;
509 if (!sctp_vtag_verify(chunk, asoc))
510 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
512 /* 6.10 Bundling
513 * An endpoint MUST NOT bundle INIT, INIT ACK or
514 * SHUTDOWN COMPLETE with any other chunks.
516 if (!chunk->singleton)
517 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
519 /* Make sure that the INIT-ACK chunk has a valid length */
520 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
521 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
522 commands);
523 /* Grab the INIT header. */
524 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
526 /* Verify the INIT chunk before processing it. */
527 err_chunk = NULL;
528 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
529 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
530 &err_chunk)) {
532 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
534 /* This chunk contains fatal error. It is to be discarded.
535 * Send an ABORT, with causes. If there are no causes,
536 * then there wasn't enough memory. Just terminate
537 * the association.
539 if (err_chunk) {
540 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
541 (__u8 *)(err_chunk->chunk_hdr) +
542 sizeof(sctp_chunkhdr_t),
543 ntohs(err_chunk->chunk_hdr->length) -
544 sizeof(sctp_chunkhdr_t));
546 sctp_chunk_free(err_chunk);
548 if (packet) {
549 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
550 SCTP_PACKET(packet));
551 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
552 error = SCTP_ERROR_INV_PARAM;
556 /* SCTP-AUTH, Section 6.3:
557 * It should be noted that if the receiver wants to tear
558 * down an association in an authenticated way only, the
559 * handling of malformed packets should not result in
560 * tearing down the association.
562 * This means that if we only want to abort associations
563 * in an authenticated way (i.e AUTH+ABORT), then we
564 * can't destroy this association just because the packet
565 * was malformed.
567 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
568 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
570 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
571 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
572 asoc, chunk->transport);
575 /* Tag the variable length parameters. Note that we never
576 * convert the parameters in an INIT chunk.
578 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
580 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
582 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
583 SCTP_PEER_INIT(initchunk));
585 /* Reset init error count upon receipt of INIT-ACK. */
586 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
588 /* 5.1 C) "A" shall stop the T1-init timer and leave
589 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
590 * timer, and enter the COOKIE-ECHOED state.
592 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
593 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
596 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
597 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
599 /* SCTP-AUTH: genereate the assocition shared keys so that
600 * we can potentially signe the COOKIE-ECHO.
602 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
604 /* 5.1 C) "A" shall then send the State Cookie received in the
605 * INIT ACK chunk in a COOKIE ECHO chunk, ...
607 /* If there is any errors to report, send the ERROR chunk generated
608 * for unknown parameters as well.
610 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
611 SCTP_CHUNK(err_chunk));
613 return SCTP_DISPOSITION_CONSUME;
617 * Respond to a normal COOKIE ECHO chunk.
618 * We are the side that is being asked for an association.
620 * Section: 5.1 Normal Establishment of an Association, D
621 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
622 * with a COOKIE ACK chunk after building a TCB and moving to
623 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
624 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
625 * chunk MUST be the first chunk in the packet.
627 * IMPLEMENTATION NOTE: An implementation may choose to send the
628 * Communication Up notification to the SCTP user upon reception
629 * of a valid COOKIE ECHO chunk.
631 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
632 * D) Rules for packet carrying a COOKIE ECHO
634 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
635 * Initial Tag received in the INIT ACK.
637 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
639 * Inputs
640 * (endpoint, asoc, chunk)
642 * Outputs
643 * (asoc, reply_msg, msg_up, timers, counters)
645 * The return value is the disposition of the chunk.
647 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
648 const struct sctp_endpoint *ep,
649 const struct sctp_association *asoc,
650 const sctp_subtype_t type, void *arg,
651 sctp_cmd_seq_t *commands)
653 struct sctp_chunk *chunk = arg;
654 struct sctp_association *new_asoc;
655 sctp_init_chunk_t *peer_init;
656 struct sctp_chunk *repl;
657 struct sctp_ulpevent *ev, *ai_ev = NULL;
658 int error = 0;
659 struct sctp_chunk *err_chk_p;
660 struct sock *sk;
662 /* If the packet is an OOTB packet which is temporarily on the
663 * control endpoint, respond with an ABORT.
665 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
666 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
667 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
670 /* Make sure that the COOKIE_ECHO chunk has a valid length.
671 * In this case, we check that we have enough for at least a
672 * chunk header. More detailed verification is done
673 * in sctp_unpack_cookie().
675 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
676 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
678 /* If the endpoint is not listening or if the number of associations
679 * on the TCP-style socket exceed the max backlog, respond with an
680 * ABORT.
682 sk = ep->base.sk;
683 if (!sctp_sstate(sk, LISTENING) ||
684 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
685 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
687 /* "Decode" the chunk. We have no optional parameters so we
688 * are in good shape.
690 chunk->subh.cookie_hdr =
691 (struct sctp_signed_cookie *)chunk->skb->data;
692 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
693 sizeof(sctp_chunkhdr_t)))
694 goto nomem;
696 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
697 * "Z" will reply with a COOKIE ACK chunk after building a TCB
698 * and moving to the ESTABLISHED state.
700 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
701 &err_chk_p);
703 /* FIXME:
704 * If the re-build failed, what is the proper error path
705 * from here?
707 * [We should abort the association. --piggy]
709 if (!new_asoc) {
710 /* FIXME: Several errors are possible. A bad cookie should
711 * be silently discarded, but think about logging it too.
713 switch (error) {
714 case -SCTP_IERROR_NOMEM:
715 goto nomem;
717 case -SCTP_IERROR_STALE_COOKIE:
718 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
719 err_chk_p);
720 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
722 case -SCTP_IERROR_BAD_SIG:
723 default:
724 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
729 /* Delay state machine commands until later.
731 * Re-build the bind address for the association is done in
732 * the sctp_unpack_cookie() already.
734 /* This is a brand-new association, so these are not yet side
735 * effects--it is safe to run them here.
737 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
739 if (!sctp_process_init(new_asoc, chunk,
740 &chunk->subh.cookie_hdr->c.peer_addr,
741 peer_init, GFP_ATOMIC))
742 goto nomem_init;
744 /* SCTP-AUTH: Now that we've populate required fields in
745 * sctp_process_init, set up the assocaition shared keys as
746 * necessary so that we can potentially authenticate the ACK
748 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
749 if (error)
750 goto nomem_init;
752 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
753 * is supposed to be authenticated and we have to do delayed
754 * authentication. We've just recreated the association using
755 * the information in the cookie and now it's much easier to
756 * do the authentication.
758 if (chunk->auth_chunk) {
759 struct sctp_chunk auth;
760 sctp_ierror_t ret;
762 /* Make sure that we and the peer are AUTH capable */
763 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
764 sctp_association_free(new_asoc);
765 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
768 /* set-up our fake chunk so that we can process it */
769 auth.skb = chunk->auth_chunk;
770 auth.asoc = chunk->asoc;
771 auth.sctp_hdr = chunk->sctp_hdr;
772 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
773 sizeof(sctp_chunkhdr_t));
774 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
775 auth.transport = chunk->transport;
777 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
778 if (ret != SCTP_IERROR_NO_ERROR) {
779 sctp_association_free(new_asoc);
780 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
784 repl = sctp_make_cookie_ack(new_asoc, chunk);
785 if (!repl)
786 goto nomem_init;
788 /* RFC 2960 5.1 Normal Establishment of an Association
790 * D) IMPLEMENTATION NOTE: An implementation may choose to
791 * send the Communication Up notification to the SCTP user
792 * upon reception of a valid COOKIE ECHO chunk.
794 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
795 new_asoc->c.sinit_num_ostreams,
796 new_asoc->c.sinit_max_instreams,
797 NULL, GFP_ATOMIC);
798 if (!ev)
799 goto nomem_ev;
801 /* Sockets API Draft Section 5.3.1.6
802 * When a peer sends a Adaptation Layer Indication parameter , SCTP
803 * delivers this notification to inform the application that of the
804 * peers requested adaptation layer.
806 if (new_asoc->peer.adaptation_ind) {
807 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
808 GFP_ATOMIC);
809 if (!ai_ev)
810 goto nomem_aiev;
813 /* Add all the state machine commands now since we've created
814 * everything. This way we don't introduce memory corruptions
815 * during side-effect processing and correclty count established
816 * associations.
818 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
819 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
820 SCTP_STATE(SCTP_STATE_ESTABLISHED));
821 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
822 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
823 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
825 if (new_asoc->autoclose)
826 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
827 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
829 /* This will send the COOKIE ACK */
830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
832 /* Queue the ASSOC_CHANGE event */
833 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
835 /* Send up the Adaptation Layer Indication event */
836 if (ai_ev)
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
838 SCTP_ULPEVENT(ai_ev));
840 return SCTP_DISPOSITION_CONSUME;
842 nomem_aiev:
843 sctp_ulpevent_free(ev);
844 nomem_ev:
845 sctp_chunk_free(repl);
846 nomem_init:
847 sctp_association_free(new_asoc);
848 nomem:
849 return SCTP_DISPOSITION_NOMEM;
853 * Respond to a normal COOKIE ACK chunk.
854 * We are the side that is being asked for an association.
856 * RFC 2960 5.1 Normal Establishment of an Association
858 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
859 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
860 * timer. It may also notify its ULP about the successful
861 * establishment of the association with a Communication Up
862 * notification (see Section 10).
864 * Verification Tag:
865 * Inputs
866 * (endpoint, asoc, chunk)
868 * Outputs
869 * (asoc, reply_msg, msg_up, timers, counters)
871 * The return value is the disposition of the chunk.
873 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
874 const struct sctp_endpoint *ep,
875 const struct sctp_association *asoc,
876 const sctp_subtype_t type, void *arg,
877 sctp_cmd_seq_t *commands)
879 struct sctp_chunk *chunk = arg;
880 struct sctp_ulpevent *ev;
882 if (!sctp_vtag_verify(chunk, asoc))
883 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
885 /* Verify that the chunk length for the COOKIE-ACK is OK.
886 * If we don't do this, any bundled chunks may be junked.
888 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
889 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
890 commands);
892 /* Reset init error count upon receipt of COOKIE-ACK,
893 * to avoid problems with the managemement of this
894 * counter in stale cookie situations when a transition back
895 * from the COOKIE-ECHOED state to the COOKIE-WAIT
896 * state is performed.
898 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
900 /* RFC 2960 5.1 Normal Establishment of an Association
902 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
903 * from the COOKIE-ECHOED state to the ESTABLISHED state,
904 * stopping the T1-cookie timer.
906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
907 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
908 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
909 SCTP_STATE(SCTP_STATE_ESTABLISHED));
910 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
911 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
912 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
913 if (asoc->autoclose)
914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
915 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
917 /* It may also notify its ULP about the successful
918 * establishment of the association with a Communication Up
919 * notification (see Section 10).
921 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
922 0, asoc->c.sinit_num_ostreams,
923 asoc->c.sinit_max_instreams,
924 NULL, GFP_ATOMIC);
926 if (!ev)
927 goto nomem;
929 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
931 /* Sockets API Draft Section 5.3.1.6
932 * When a peer sends a Adaptation Layer Indication parameter , SCTP
933 * delivers this notification to inform the application that of the
934 * peers requested adaptation layer.
936 if (asoc->peer.adaptation_ind) {
937 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
938 if (!ev)
939 goto nomem;
941 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
942 SCTP_ULPEVENT(ev));
945 return SCTP_DISPOSITION_CONSUME;
946 nomem:
947 return SCTP_DISPOSITION_NOMEM;
950 /* Generate and sendout a heartbeat packet. */
951 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
952 const struct sctp_association *asoc,
953 const sctp_subtype_t type,
954 void *arg,
955 sctp_cmd_seq_t *commands)
957 struct sctp_transport *transport = (struct sctp_transport *) arg;
958 struct sctp_chunk *reply;
960 /* Send a heartbeat to our peer. */
961 reply = sctp_make_heartbeat(asoc, transport);
962 if (!reply)
963 return SCTP_DISPOSITION_NOMEM;
965 /* Set rto_pending indicating that an RTT measurement
966 * is started with this heartbeat chunk.
968 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
969 SCTP_TRANSPORT(transport));
971 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
972 return SCTP_DISPOSITION_CONSUME;
975 /* Generate a HEARTBEAT packet on the given transport. */
976 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
977 const struct sctp_endpoint *ep,
978 const struct sctp_association *asoc,
979 const sctp_subtype_t type,
980 void *arg,
981 sctp_cmd_seq_t *commands)
983 struct sctp_transport *transport = (struct sctp_transport *) arg;
985 if (asoc->overall_error_count >= asoc->max_retrans) {
986 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
987 SCTP_ERROR(ETIMEDOUT));
988 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
989 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
990 SCTP_PERR(SCTP_ERROR_NO_ERROR));
991 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
992 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
993 return SCTP_DISPOSITION_DELETE_TCB;
996 /* Section 3.3.5.
997 * The Sender-specific Heartbeat Info field should normally include
998 * information about the sender's current time when this HEARTBEAT
999 * chunk is sent and the destination transport address to which this
1000 * HEARTBEAT is sent (see Section 8.3).
1003 if (transport->param_flags & SPP_HB_ENABLE) {
1004 if (SCTP_DISPOSITION_NOMEM ==
1005 sctp_sf_heartbeat(ep, asoc, type, arg,
1006 commands))
1007 return SCTP_DISPOSITION_NOMEM;
1009 /* Set transport error counter and association error counter
1010 * when sending heartbeat.
1012 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1013 SCTP_TRANSPORT(transport));
1015 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1016 SCTP_TRANSPORT(transport));
1017 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1018 SCTP_TRANSPORT(transport));
1020 return SCTP_DISPOSITION_CONSUME;
1024 * Process an heartbeat request.
1026 * Section: 8.3 Path Heartbeat
1027 * The receiver of the HEARTBEAT should immediately respond with a
1028 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1029 * from the received HEARTBEAT chunk.
1031 * Verification Tag: 8.5 Verification Tag [Normal verification]
1032 * When receiving an SCTP packet, the endpoint MUST ensure that the
1033 * value in the Verification Tag field of the received SCTP packet
1034 * matches its own Tag. If the received Verification Tag value does not
1035 * match the receiver's own tag value, the receiver shall silently
1036 * discard the packet and shall not process it any further except for
1037 * those cases listed in Section 8.5.1 below.
1039 * Inputs
1040 * (endpoint, asoc, chunk)
1042 * Outputs
1043 * (asoc, reply_msg, msg_up, timers, counters)
1045 * The return value is the disposition of the chunk.
1047 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1048 const struct sctp_endpoint *ep,
1049 const struct sctp_association *asoc,
1050 const sctp_subtype_t type,
1051 void *arg,
1052 sctp_cmd_seq_t *commands)
1054 sctp_paramhdr_t *param_hdr;
1055 struct sctp_chunk *chunk = arg;
1056 struct sctp_chunk *reply;
1057 size_t paylen = 0;
1059 if (!sctp_vtag_verify(chunk, asoc))
1060 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1062 /* Make sure that the HEARTBEAT chunk has a valid length. */
1063 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1064 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1065 commands);
1067 /* 8.3 The receiver of the HEARTBEAT should immediately
1068 * respond with a HEARTBEAT ACK that contains the Heartbeat
1069 * Information field copied from the received HEARTBEAT chunk.
1071 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1072 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
1073 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1075 if (ntohs(param_hdr->length) > paylen)
1076 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1077 param_hdr, commands);
1079 if (!pskb_pull(chunk->skb, paylen))
1080 goto nomem;
1082 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1083 if (!reply)
1084 goto nomem;
1086 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1087 return SCTP_DISPOSITION_CONSUME;
1089 nomem:
1090 return SCTP_DISPOSITION_NOMEM;
1094 * Process the returning HEARTBEAT ACK.
1096 * Section: 8.3 Path Heartbeat
1097 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1098 * should clear the error counter of the destination transport
1099 * address to which the HEARTBEAT was sent, and mark the destination
1100 * transport address as active if it is not so marked. The endpoint may
1101 * optionally report to the upper layer when an inactive destination
1102 * address is marked as active due to the reception of the latest
1103 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1104 * clear the association overall error count as well (as defined
1105 * in section 8.1).
1107 * The receiver of the HEARTBEAT ACK should also perform an RTT
1108 * measurement for that destination transport address using the time
1109 * value carried in the HEARTBEAT ACK chunk.
1111 * Verification Tag: 8.5 Verification Tag [Normal verification]
1113 * Inputs
1114 * (endpoint, asoc, chunk)
1116 * Outputs
1117 * (asoc, reply_msg, msg_up, timers, counters)
1119 * The return value is the disposition of the chunk.
1121 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1122 const struct sctp_endpoint *ep,
1123 const struct sctp_association *asoc,
1124 const sctp_subtype_t type,
1125 void *arg,
1126 sctp_cmd_seq_t *commands)
1128 struct sctp_chunk *chunk = arg;
1129 union sctp_addr from_addr;
1130 struct sctp_transport *link;
1131 sctp_sender_hb_info_t *hbinfo;
1132 unsigned long max_interval;
1134 if (!sctp_vtag_verify(chunk, asoc))
1135 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1137 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1138 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1139 sizeof(sctp_sender_hb_info_t)))
1140 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1141 commands);
1143 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1144 /* Make sure that the length of the parameter is what we expect */
1145 if (ntohs(hbinfo->param_hdr.length) !=
1146 sizeof(sctp_sender_hb_info_t)) {
1147 return SCTP_DISPOSITION_DISCARD;
1150 from_addr = hbinfo->daddr;
1151 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1153 /* This should never happen, but lets log it if so. */
1154 if (unlikely(!link)) {
1155 if (from_addr.sa.sa_family == AF_INET6) {
1156 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1157 __func__,
1158 asoc,
1159 &from_addr.v6.sin6_addr);
1160 } else {
1161 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1162 __func__,
1163 asoc,
1164 &from_addr.v4.sin_addr.s_addr);
1166 return SCTP_DISPOSITION_DISCARD;
1169 /* Validate the 64-bit random nonce. */
1170 if (hbinfo->hb_nonce != link->hb_nonce)
1171 return SCTP_DISPOSITION_DISCARD;
1173 max_interval = link->hbinterval + link->rto;
1175 /* Check if the timestamp looks valid. */
1176 if (time_after(hbinfo->sent_at, jiffies) ||
1177 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1178 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1179 "for transport:%p\n", __func__, link);
1181 return SCTP_DISPOSITION_DISCARD;
1184 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1185 * the HEARTBEAT should clear the error counter of the
1186 * destination transport address to which the HEARTBEAT was
1187 * sent and mark the destination transport address as active if
1188 * it is not so marked.
1190 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1192 return SCTP_DISPOSITION_CONSUME;
1195 /* Helper function to send out an abort for the restart
1196 * condition.
1198 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1199 struct sctp_chunk *init,
1200 sctp_cmd_seq_t *commands)
1202 int len;
1203 struct sctp_packet *pkt;
1204 union sctp_addr_param *addrparm;
1205 struct sctp_errhdr *errhdr;
1206 struct sctp_endpoint *ep;
1207 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1208 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1210 /* Build the error on the stack. We are way to malloc crazy
1211 * throughout the code today.
1213 errhdr = (struct sctp_errhdr *)buffer;
1214 addrparm = (union sctp_addr_param *)errhdr->variable;
1216 /* Copy into a parm format. */
1217 len = af->to_addr_param(ssa, addrparm);
1218 len += sizeof(sctp_errhdr_t);
1220 errhdr->cause = SCTP_ERROR_RESTART;
1221 errhdr->length = htons(len);
1223 /* Assign to the control socket. */
1224 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1226 /* Association is NULL since this may be a restart attack and we
1227 * want to send back the attacker's vtag.
1229 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1231 if (!pkt)
1232 goto out;
1233 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1235 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1237 /* Discard the rest of the inbound packet. */
1238 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1240 out:
1241 /* Even if there is no memory, treat as a failure so
1242 * the packet will get dropped.
1244 return 0;
1247 static bool list_has_sctp_addr(const struct list_head *list,
1248 union sctp_addr *ipaddr)
1250 struct sctp_transport *addr;
1252 list_for_each_entry(addr, list, transports) {
1253 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1254 return true;
1257 return false;
1259 /* A restart is occurring, check to make sure no new addresses
1260 * are being added as we may be under a takeover attack.
1262 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1263 const struct sctp_association *asoc,
1264 struct sctp_chunk *init,
1265 sctp_cmd_seq_t *commands)
1267 struct net *net = sock_net(new_asoc->base.sk);
1268 struct sctp_transport *new_addr;
1269 int ret = 1;
1271 /* Implementor's Guide - Section 5.2.2
1272 * ...
1273 * Before responding the endpoint MUST check to see if the
1274 * unexpected INIT adds new addresses to the association. If new
1275 * addresses are added to the association, the endpoint MUST respond
1276 * with an ABORT..
1279 /* Search through all current addresses and make sure
1280 * we aren't adding any new ones.
1282 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1283 transports) {
1284 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1285 &new_addr->ipaddr)) {
1286 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1287 commands);
1288 ret = 0;
1289 break;
1293 /* Return success if all addresses were found. */
1294 return ret;
1297 /* Populate the verification/tie tags based on overlapping INIT
1298 * scenario.
1300 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1302 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1303 const struct sctp_association *asoc)
1305 switch (asoc->state) {
1307 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1309 case SCTP_STATE_COOKIE_WAIT:
1310 new_asoc->c.my_vtag = asoc->c.my_vtag;
1311 new_asoc->c.my_ttag = asoc->c.my_vtag;
1312 new_asoc->c.peer_ttag = 0;
1313 break;
1315 case SCTP_STATE_COOKIE_ECHOED:
1316 new_asoc->c.my_vtag = asoc->c.my_vtag;
1317 new_asoc->c.my_ttag = asoc->c.my_vtag;
1318 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1319 break;
1321 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1322 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1324 default:
1325 new_asoc->c.my_ttag = asoc->c.my_vtag;
1326 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1327 break;
1330 /* Other parameters for the endpoint SHOULD be copied from the
1331 * existing parameters of the association (e.g. number of
1332 * outbound streams) into the INIT ACK and cookie.
1334 new_asoc->rwnd = asoc->rwnd;
1335 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1336 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1337 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1341 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1342 * handling action.
1344 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1346 * Returns value representing action to be taken. These action values
1347 * correspond to Action/Description values in RFC 2960, Table 2.
1349 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1350 const struct sctp_association *asoc)
1352 /* In this case, the peer may have restarted. */
1353 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1354 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1355 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1356 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1357 return 'A';
1359 /* Collision case B. */
1360 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1361 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1362 (0 == asoc->c.peer_vtag))) {
1363 return 'B';
1366 /* Collision case D. */
1367 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1368 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1369 return 'D';
1371 /* Collision case C. */
1372 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1373 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1374 (0 == new_asoc->c.my_ttag) &&
1375 (0 == new_asoc->c.peer_ttag))
1376 return 'C';
1378 /* No match to any of the special cases; discard this packet. */
1379 return 'E';
1382 /* Common helper routine for both duplicate and simulataneous INIT
1383 * chunk handling.
1385 static sctp_disposition_t sctp_sf_do_unexpected_init(
1386 struct net *net,
1387 const struct sctp_endpoint *ep,
1388 const struct sctp_association *asoc,
1389 const sctp_subtype_t type,
1390 void *arg, sctp_cmd_seq_t *commands)
1392 sctp_disposition_t retval;
1393 struct sctp_chunk *chunk = arg;
1394 struct sctp_chunk *repl;
1395 struct sctp_association *new_asoc;
1396 struct sctp_chunk *err_chunk;
1397 struct sctp_packet *packet;
1398 sctp_unrecognized_param_t *unk_param;
1399 int len;
1401 /* 6.10 Bundling
1402 * An endpoint MUST NOT bundle INIT, INIT ACK or
1403 * SHUTDOWN COMPLETE with any other chunks.
1405 * IG Section 2.11.2
1406 * Furthermore, we require that the receiver of an INIT chunk MUST
1407 * enforce these rules by silently discarding an arriving packet
1408 * with an INIT chunk that is bundled with other chunks.
1410 if (!chunk->singleton)
1411 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1413 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1414 * Tag.
1416 if (chunk->sctp_hdr->vtag != 0)
1417 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1419 /* Make sure that the INIT chunk has a valid length.
1420 * In this case, we generate a protocol violation since we have
1421 * an association established.
1423 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1424 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1425 commands);
1426 /* Grab the INIT header. */
1427 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1429 /* Tag the variable length parameters. */
1430 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1432 /* Verify the INIT chunk before processing it. */
1433 err_chunk = NULL;
1434 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1435 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1436 &err_chunk)) {
1437 /* This chunk contains fatal error. It is to be discarded.
1438 * Send an ABORT, with causes if there is any.
1440 if (err_chunk) {
1441 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1442 (__u8 *)(err_chunk->chunk_hdr) +
1443 sizeof(sctp_chunkhdr_t),
1444 ntohs(err_chunk->chunk_hdr->length) -
1445 sizeof(sctp_chunkhdr_t));
1447 if (packet) {
1448 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1449 SCTP_PACKET(packet));
1450 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1451 retval = SCTP_DISPOSITION_CONSUME;
1452 } else {
1453 retval = SCTP_DISPOSITION_NOMEM;
1455 goto cleanup;
1456 } else {
1457 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1458 commands);
1463 * Other parameters for the endpoint SHOULD be copied from the
1464 * existing parameters of the association (e.g. number of
1465 * outbound streams) into the INIT ACK and cookie.
1466 * FIXME: We are copying parameters from the endpoint not the
1467 * association.
1469 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1470 if (!new_asoc)
1471 goto nomem;
1473 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1474 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1475 goto nomem;
1477 /* In the outbound INIT ACK the endpoint MUST copy its current
1478 * Verification Tag and Peers Verification tag into a reserved
1479 * place (local tie-tag and per tie-tag) within the state cookie.
1481 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1482 (sctp_init_chunk_t *)chunk->chunk_hdr,
1483 GFP_ATOMIC))
1484 goto nomem;
1486 /* Make sure no new addresses are being added during the
1487 * restart. Do not do this check for COOKIE-WAIT state,
1488 * since there are no peer addresses to check against.
1489 * Upon return an ABORT will have been sent if needed.
1491 if (!sctp_state(asoc, COOKIE_WAIT)) {
1492 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1493 commands)) {
1494 retval = SCTP_DISPOSITION_CONSUME;
1495 goto nomem_retval;
1499 sctp_tietags_populate(new_asoc, asoc);
1501 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1503 /* If there are errors need to be reported for unknown parameters,
1504 * make sure to reserve enough room in the INIT ACK for them.
1506 len = 0;
1507 if (err_chunk) {
1508 len = ntohs(err_chunk->chunk_hdr->length) -
1509 sizeof(sctp_chunkhdr_t);
1512 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1513 if (!repl)
1514 goto nomem;
1516 /* If there are errors need to be reported for unknown parameters,
1517 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1518 * parameter.
1520 if (err_chunk) {
1521 /* Get the "Unrecognized parameter" parameter(s) out of the
1522 * ERROR chunk generated by sctp_verify_init(). Since the
1523 * error cause code for "unknown parameter" and the
1524 * "Unrecognized parameter" type is the same, we can
1525 * construct the parameters in INIT ACK by copying the
1526 * ERROR causes over.
1528 unk_param = (sctp_unrecognized_param_t *)
1529 ((__u8 *)(err_chunk->chunk_hdr) +
1530 sizeof(sctp_chunkhdr_t));
1531 /* Replace the cause code with the "Unrecognized parameter"
1532 * parameter type.
1534 sctp_addto_chunk(repl, len, unk_param);
1537 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1538 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1541 * Note: After sending out INIT ACK with the State Cookie parameter,
1542 * "Z" MUST NOT allocate any resources for this new association.
1543 * Otherwise, "Z" will be vulnerable to resource attacks.
1545 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1546 retval = SCTP_DISPOSITION_CONSUME;
1548 return retval;
1550 nomem:
1551 retval = SCTP_DISPOSITION_NOMEM;
1552 nomem_retval:
1553 if (new_asoc)
1554 sctp_association_free(new_asoc);
1555 cleanup:
1556 if (err_chunk)
1557 sctp_chunk_free(err_chunk);
1558 return retval;
1562 * Handle simultaneous INIT.
1563 * This means we started an INIT and then we got an INIT request from
1564 * our peer.
1566 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1567 * This usually indicates an initialization collision, i.e., each
1568 * endpoint is attempting, at about the same time, to establish an
1569 * association with the other endpoint.
1571 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1572 * endpoint MUST respond with an INIT ACK using the same parameters it
1573 * sent in its original INIT chunk (including its Verification Tag,
1574 * unchanged). These original parameters are combined with those from the
1575 * newly received INIT chunk. The endpoint shall also generate a State
1576 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1577 * INIT to calculate the State Cookie.
1579 * After that, the endpoint MUST NOT change its state, the T1-init
1580 * timer shall be left running and the corresponding TCB MUST NOT be
1581 * destroyed. The normal procedures for handling State Cookies when
1582 * a TCB exists will resolve the duplicate INITs to a single association.
1584 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1585 * its Tie-Tags with the Tag information of itself and its peer (see
1586 * section 5.2.2 for a description of the Tie-Tags).
1588 * Verification Tag: Not explicit, but an INIT can not have a valid
1589 * verification tag, so we skip the check.
1591 * Inputs
1592 * (endpoint, asoc, chunk)
1594 * Outputs
1595 * (asoc, reply_msg, msg_up, timers, counters)
1597 * The return value is the disposition of the chunk.
1599 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1600 const struct sctp_endpoint *ep,
1601 const struct sctp_association *asoc,
1602 const sctp_subtype_t type,
1603 void *arg,
1604 sctp_cmd_seq_t *commands)
1606 /* Call helper to do the real work for both simulataneous and
1607 * duplicate INIT chunk handling.
1609 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1613 * Handle duplicated INIT messages. These are usually delayed
1614 * restransmissions.
1616 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1617 * COOKIE-ECHOED and COOKIE-WAIT
1619 * Unless otherwise stated, upon reception of an unexpected INIT for
1620 * this association, the endpoint shall generate an INIT ACK with a
1621 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1622 * current Verification Tag and peer's Verification Tag into a reserved
1623 * place within the state cookie. We shall refer to these locations as
1624 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1625 * containing this INIT ACK MUST carry a Verification Tag value equal to
1626 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1627 * MUST contain a new Initiation Tag (randomly generated see Section
1628 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1629 * existing parameters of the association (e.g. number of outbound
1630 * streams) into the INIT ACK and cookie.
1632 * After sending out the INIT ACK, the endpoint shall take no further
1633 * actions, i.e., the existing association, including its current state,
1634 * and the corresponding TCB MUST NOT be changed.
1636 * Note: Only when a TCB exists and the association is not in a COOKIE-
1637 * WAIT state are the Tie-Tags populated. For a normal association INIT
1638 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1639 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1640 * State Cookie are populated as specified in section 5.2.1.
1642 * Verification Tag: Not specified, but an INIT has no way of knowing
1643 * what the verification tag could be, so we ignore it.
1645 * Inputs
1646 * (endpoint, asoc, chunk)
1648 * Outputs
1649 * (asoc, reply_msg, msg_up, timers, counters)
1651 * The return value is the disposition of the chunk.
1653 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1654 const struct sctp_endpoint *ep,
1655 const struct sctp_association *asoc,
1656 const sctp_subtype_t type,
1657 void *arg,
1658 sctp_cmd_seq_t *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);
1668 * Unexpected INIT-ACK handler.
1670 * Section 5.2.3
1671 * If an INIT ACK received by an endpoint in any state other than the
1672 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1673 * An unexpected INIT ACK usually indicates the processing of an old or
1674 * duplicated INIT chunk.
1676 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1677 const struct sctp_endpoint *ep,
1678 const struct sctp_association *asoc,
1679 const sctp_subtype_t type,
1680 void *arg, sctp_cmd_seq_t *commands)
1682 /* Per the above section, we'll discard the chunk if we have an
1683 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1685 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1686 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1687 else
1688 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1691 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1693 * Section 5.2.4
1694 * A) In this case, the peer may have restarted.
1696 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1697 const struct sctp_endpoint *ep,
1698 const struct sctp_association *asoc,
1699 struct sctp_chunk *chunk,
1700 sctp_cmd_seq_t *commands,
1701 struct sctp_association *new_asoc)
1703 sctp_init_chunk_t *peer_init;
1704 struct sctp_ulpevent *ev;
1705 struct sctp_chunk *repl;
1706 struct sctp_chunk *err;
1707 sctp_disposition_t disposition;
1709 /* new_asoc is a brand-new association, so these are not yet
1710 * side effects--it is safe to run them here.
1712 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1714 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1715 GFP_ATOMIC))
1716 goto nomem;
1718 /* Make sure no new addresses are being added during the
1719 * restart. Though this is a pretty complicated attack
1720 * since you'd have to get inside the cookie.
1722 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1723 return SCTP_DISPOSITION_CONSUME;
1726 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1727 * the peer has restarted (Action A), it MUST NOT setup a new
1728 * association but instead resend the SHUTDOWN ACK and send an ERROR
1729 * chunk with a "Cookie Received while Shutting Down" error cause to
1730 * its peer.
1732 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1733 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1734 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1735 chunk, commands);
1736 if (SCTP_DISPOSITION_NOMEM == disposition)
1737 goto nomem;
1739 err = sctp_make_op_error(asoc, chunk,
1740 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1741 NULL, 0, 0);
1742 if (err)
1743 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1744 SCTP_CHUNK(err));
1746 return SCTP_DISPOSITION_CONSUME;
1749 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1750 * data. Consider the optional choice of resending of this data.
1752 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1753 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1754 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1755 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1757 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1758 * and ASCONF-ACK cache.
1760 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1761 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1762 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1764 repl = sctp_make_cookie_ack(new_asoc, chunk);
1765 if (!repl)
1766 goto nomem;
1768 /* Report association restart to upper layer. */
1769 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1770 new_asoc->c.sinit_num_ostreams,
1771 new_asoc->c.sinit_max_instreams,
1772 NULL, GFP_ATOMIC);
1773 if (!ev)
1774 goto nomem_ev;
1776 /* Update the content of current association. */
1777 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1778 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1779 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1780 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1781 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1782 return SCTP_DISPOSITION_CONSUME;
1784 nomem_ev:
1785 sctp_chunk_free(repl);
1786 nomem:
1787 return SCTP_DISPOSITION_NOMEM;
1790 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1792 * Section 5.2.4
1793 * B) In this case, both sides may be attempting to start an association
1794 * at about the same time but the peer endpoint started its INIT
1795 * after responding to the local endpoint's INIT
1797 /* This case represents an initialization collision. */
1798 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1799 const struct sctp_endpoint *ep,
1800 const struct sctp_association *asoc,
1801 struct sctp_chunk *chunk,
1802 sctp_cmd_seq_t *commands,
1803 struct sctp_association *new_asoc)
1805 sctp_init_chunk_t *peer_init;
1806 struct sctp_chunk *repl;
1808 /* new_asoc is a brand-new association, so these are not yet
1809 * side effects--it is safe to run them here.
1811 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1812 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1813 GFP_ATOMIC))
1814 goto nomem;
1816 /* Update the content of current association. */
1817 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1818 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1819 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1820 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1821 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1823 repl = sctp_make_cookie_ack(new_asoc, chunk);
1824 if (!repl)
1825 goto nomem;
1827 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1829 /* RFC 2960 5.1 Normal Establishment of an Association
1831 * D) IMPLEMENTATION NOTE: An implementation may choose to
1832 * send the Communication Up notification to the SCTP user
1833 * upon reception of a valid COOKIE ECHO chunk.
1835 * Sadly, this needs to be implemented as a side-effect, because
1836 * we are not guaranteed to have set the association id of the real
1837 * association and so these notifications need to be delayed until
1838 * the association id is allocated.
1841 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1843 /* Sockets API Draft Section 5.3.1.6
1844 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1845 * delivers this notification to inform the application that of the
1846 * peers requested adaptation layer.
1848 * This also needs to be done as a side effect for the same reason as
1849 * above.
1851 if (asoc->peer.adaptation_ind)
1852 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1854 return SCTP_DISPOSITION_CONSUME;
1856 nomem:
1857 return SCTP_DISPOSITION_NOMEM;
1860 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1862 * Section 5.2.4
1863 * C) In this case, the local endpoint's cookie has arrived late.
1864 * Before it arrived, the local endpoint sent an INIT and received an
1865 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1866 * but a new tag of its own.
1868 /* This case represents an initialization collision. */
1869 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1870 const struct sctp_endpoint *ep,
1871 const struct sctp_association *asoc,
1872 struct sctp_chunk *chunk,
1873 sctp_cmd_seq_t *commands,
1874 struct sctp_association *new_asoc)
1876 /* The cookie should be silently discarded.
1877 * The endpoint SHOULD NOT change states and should leave
1878 * any timers running.
1880 return SCTP_DISPOSITION_DISCARD;
1883 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1885 * Section 5.2.4
1887 * D) When both local and remote tags match the endpoint should always
1888 * enter the ESTABLISHED state, if it has not already done so.
1890 /* This case represents an initialization collision. */
1891 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1892 const struct sctp_endpoint *ep,
1893 const struct sctp_association *asoc,
1894 struct sctp_chunk *chunk,
1895 sctp_cmd_seq_t *commands,
1896 struct sctp_association *new_asoc)
1898 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1899 struct sctp_chunk *repl;
1901 /* Clarification from Implementor's Guide:
1902 * D) When both local and remote tags match the endpoint should
1903 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1904 * It should stop any cookie timer that may be running and send
1905 * a COOKIE ACK.
1908 /* Don't accidentally move back into established state. */
1909 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1911 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1912 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1913 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1914 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1915 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1916 SCTP_NULL());
1918 /* RFC 2960 5.1 Normal Establishment of an Association
1920 * D) IMPLEMENTATION NOTE: An implementation may choose
1921 * to send the Communication Up notification to the
1922 * SCTP user upon reception of a valid COOKIE
1923 * ECHO chunk.
1925 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1926 SCTP_COMM_UP, 0,
1927 asoc->c.sinit_num_ostreams,
1928 asoc->c.sinit_max_instreams,
1929 NULL, GFP_ATOMIC);
1930 if (!ev)
1931 goto nomem;
1933 /* Sockets API Draft Section 5.3.1.6
1934 * When a peer sends a Adaptation Layer Indication parameter,
1935 * SCTP delivers this notification to inform the application
1936 * that of the peers requested adaptation layer.
1938 if (asoc->peer.adaptation_ind) {
1939 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1940 GFP_ATOMIC);
1941 if (!ai_ev)
1942 goto nomem;
1947 repl = sctp_make_cookie_ack(new_asoc, chunk);
1948 if (!repl)
1949 goto nomem;
1951 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1953 if (ev)
1954 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1955 SCTP_ULPEVENT(ev));
1956 if (ai_ev)
1957 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1958 SCTP_ULPEVENT(ai_ev));
1960 return SCTP_DISPOSITION_CONSUME;
1962 nomem:
1963 if (ai_ev)
1964 sctp_ulpevent_free(ai_ev);
1965 if (ev)
1966 sctp_ulpevent_free(ev);
1967 return SCTP_DISPOSITION_NOMEM;
1971 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1972 * chunk was retransmitted and then delayed in the network.
1974 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1976 * Verification Tag: None. Do cookie validation.
1978 * Inputs
1979 * (endpoint, asoc, chunk)
1981 * Outputs
1982 * (asoc, reply_msg, msg_up, timers, counters)
1984 * The return value is the disposition of the chunk.
1986 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1987 const struct sctp_endpoint *ep,
1988 const struct sctp_association *asoc,
1989 const sctp_subtype_t type,
1990 void *arg,
1991 sctp_cmd_seq_t *commands)
1993 sctp_disposition_t retval;
1994 struct sctp_chunk *chunk = arg;
1995 struct sctp_association *new_asoc;
1996 int error = 0;
1997 char action;
1998 struct sctp_chunk *err_chk_p;
2000 /* Make sure that the chunk has a valid length from the protocol
2001 * perspective. In this case check to make sure we have at least
2002 * enough for the chunk header. Cookie length verification is
2003 * done later.
2005 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2006 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2007 commands);
2009 /* "Decode" the chunk. We have no optional parameters so we
2010 * are in good shape.
2012 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2013 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2014 sizeof(sctp_chunkhdr_t)))
2015 goto nomem;
2017 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2018 * of a duplicate COOKIE ECHO match the Verification Tags of the
2019 * current association, consider the State Cookie valid even if
2020 * the lifespan is exceeded.
2022 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2023 &err_chk_p);
2025 /* FIXME:
2026 * If the re-build failed, what is the proper error path
2027 * from here?
2029 * [We should abort the association. --piggy]
2031 if (!new_asoc) {
2032 /* FIXME: Several errors are possible. A bad cookie should
2033 * be silently discarded, but think about logging it too.
2035 switch (error) {
2036 case -SCTP_IERROR_NOMEM:
2037 goto nomem;
2039 case -SCTP_IERROR_STALE_COOKIE:
2040 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2041 err_chk_p);
2042 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2043 case -SCTP_IERROR_BAD_SIG:
2044 default:
2045 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2049 /* Compare the tie_tag in cookie with the verification tag of
2050 * current association.
2052 action = sctp_tietags_compare(new_asoc, asoc);
2054 switch (action) {
2055 case 'A': /* Association restart. */
2056 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2057 new_asoc);
2058 break;
2060 case 'B': /* Collision case B. */
2061 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2062 new_asoc);
2063 break;
2065 case 'C': /* Collision case C. */
2066 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2067 new_asoc);
2068 break;
2070 case 'D': /* Collision case D. */
2071 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2072 new_asoc);
2073 break;
2075 default: /* Discard packet for all others. */
2076 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2077 break;
2080 /* Delete the tempory new association. */
2081 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2082 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2084 /* Restore association pointer to provide SCTP command interpeter
2085 * with a valid context in case it needs to manipulate
2086 * the queues */
2087 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2088 SCTP_ASOC((struct sctp_association *)asoc));
2090 return retval;
2092 nomem:
2093 return SCTP_DISPOSITION_NOMEM;
2097 * Process an ABORT. (SHUTDOWN-PENDING state)
2099 * See sctp_sf_do_9_1_abort().
2101 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2102 struct net *net,
2103 const struct sctp_endpoint *ep,
2104 const struct sctp_association *asoc,
2105 const sctp_subtype_t type,
2106 void *arg,
2107 sctp_cmd_seq_t *commands)
2109 struct sctp_chunk *chunk = arg;
2111 if (!sctp_vtag_verify_either(chunk, asoc))
2112 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2114 /* Make sure that the ABORT chunk has a valid length.
2115 * Since this is an ABORT chunk, we have to discard it
2116 * because of the following text:
2117 * RFC 2960, Section 3.3.7
2118 * If an endpoint receives an ABORT with a format error or for an
2119 * association that doesn't exist, it MUST silently discard it.
2120 * Because the length is "invalid", we can't really discard just
2121 * as we do not know its true length. So, to be safe, discard the
2122 * packet.
2124 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2125 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2127 /* ADD-IP: Special case for ABORT chunks
2128 * F4) One special consideration is that ABORT Chunks arriving
2129 * destined to the IP address being deleted MUST be
2130 * ignored (see Section 5.3.1 for further details).
2132 if (SCTP_ADDR_DEL ==
2133 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2134 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2136 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2140 * Process an ABORT. (SHUTDOWN-SENT state)
2142 * See sctp_sf_do_9_1_abort().
2144 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2145 const struct sctp_endpoint *ep,
2146 const struct sctp_association *asoc,
2147 const sctp_subtype_t type,
2148 void *arg,
2149 sctp_cmd_seq_t *commands)
2151 struct sctp_chunk *chunk = arg;
2153 if (!sctp_vtag_verify_either(chunk, asoc))
2154 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2156 /* Make sure that the ABORT chunk has a valid length.
2157 * Since this is an ABORT chunk, we have to discard it
2158 * because of the following text:
2159 * RFC 2960, Section 3.3.7
2160 * If an endpoint receives an ABORT with a format error or for an
2161 * association that doesn't exist, it MUST silently discard it.
2162 * Because the length is "invalid", we can't really discard just
2163 * as we do not know its true length. So, to be safe, discard the
2164 * packet.
2166 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2167 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2169 /* ADD-IP: Special case for ABORT chunks
2170 * F4) One special consideration is that ABORT Chunks arriving
2171 * destined to the IP address being deleted MUST be
2172 * ignored (see Section 5.3.1 for further details).
2174 if (SCTP_ADDR_DEL ==
2175 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2176 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2178 /* Stop the T2-shutdown timer. */
2179 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2180 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2182 /* Stop the T5-shutdown guard timer. */
2183 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2184 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2186 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2190 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2192 * See sctp_sf_do_9_1_abort().
2194 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2195 struct net *net,
2196 const struct sctp_endpoint *ep,
2197 const struct sctp_association *asoc,
2198 const sctp_subtype_t type,
2199 void *arg,
2200 sctp_cmd_seq_t *commands)
2202 /* The same T2 timer, so we should be able to use
2203 * common function with the SHUTDOWN-SENT state.
2205 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2209 * Handle an Error received in COOKIE_ECHOED state.
2211 * Only handle the error type of stale COOKIE Error, the other errors will
2212 * be ignored.
2214 * Inputs
2215 * (endpoint, asoc, chunk)
2217 * Outputs
2218 * (asoc, reply_msg, msg_up, timers, counters)
2220 * The return value is the disposition of the chunk.
2222 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2223 const struct sctp_endpoint *ep,
2224 const struct sctp_association *asoc,
2225 const sctp_subtype_t type,
2226 void *arg,
2227 sctp_cmd_seq_t *commands)
2229 struct sctp_chunk *chunk = arg;
2230 sctp_errhdr_t *err;
2232 if (!sctp_vtag_verify(chunk, asoc))
2233 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2235 /* Make sure that the ERROR chunk has a valid length.
2236 * The parameter walking depends on this as well.
2238 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2239 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2240 commands);
2242 /* Process the error here */
2243 /* FUTURE FIXME: When PR-SCTP related and other optional
2244 * parms are emitted, this will have to change to handle multiple
2245 * errors.
2247 sctp_walk_errors(err, chunk->chunk_hdr) {
2248 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2249 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2250 arg, commands);
2253 /* It is possible to have malformed error causes, and that
2254 * will cause us to end the walk early. However, since
2255 * we are discarding the packet, there should be no adverse
2256 * affects.
2258 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2262 * Handle a Stale COOKIE Error
2264 * Section: 5.2.6 Handle Stale COOKIE Error
2265 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2266 * one of the following three alternatives.
2267 * ...
2268 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2269 * Preservative parameter requesting an extension to the lifetime of
2270 * the State Cookie. When calculating the time extension, an
2271 * implementation SHOULD use the RTT information measured based on the
2272 * previous COOKIE ECHO / ERROR exchange, and should add no more
2273 * than 1 second beyond the measured RTT, due to long State Cookie
2274 * lifetimes making the endpoint more subject to a replay attack.
2276 * Verification Tag: Not explicit, but safe to ignore.
2278 * Inputs
2279 * (endpoint, asoc, chunk)
2281 * Outputs
2282 * (asoc, reply_msg, msg_up, timers, counters)
2284 * The return value is the disposition of the chunk.
2286 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2287 const struct sctp_endpoint *ep,
2288 const struct sctp_association *asoc,
2289 const sctp_subtype_t type,
2290 void *arg,
2291 sctp_cmd_seq_t *commands)
2293 struct sctp_chunk *chunk = arg;
2294 time_t stale;
2295 sctp_cookie_preserve_param_t bht;
2296 sctp_errhdr_t *err;
2297 struct sctp_chunk *reply;
2298 struct sctp_bind_addr *bp;
2299 int attempts = asoc->init_err_counter + 1;
2301 if (attempts > asoc->max_init_attempts) {
2302 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2303 SCTP_ERROR(ETIMEDOUT));
2304 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2305 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2306 return SCTP_DISPOSITION_DELETE_TCB;
2309 err = (sctp_errhdr_t *)(chunk->skb->data);
2311 /* When calculating the time extension, an implementation
2312 * SHOULD use the RTT information measured based on the
2313 * previous COOKIE ECHO / ERROR exchange, and should add no
2314 * more than 1 second beyond the measured RTT, due to long
2315 * State Cookie lifetimes making the endpoint more subject to
2316 * a replay attack.
2317 * Measure of Staleness's unit is usec. (1/1000000 sec)
2318 * Suggested Cookie Life-span Increment's unit is msec.
2319 * (1/1000 sec)
2320 * In general, if you use the suggested cookie life, the value
2321 * found in the field of measure of staleness should be doubled
2322 * to give ample time to retransmit the new cookie and thus
2323 * yield a higher probability of success on the reattempt.
2325 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2326 stale = (stale * 2) / 1000;
2328 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2329 bht.param_hdr.length = htons(sizeof(bht));
2330 bht.lifespan_increment = htonl(stale);
2332 /* Build that new INIT chunk. */
2333 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2334 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2335 if (!reply)
2336 goto nomem;
2338 sctp_addto_chunk(reply, sizeof(bht), &bht);
2340 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2341 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2343 /* Stop pending T3-rtx and heartbeat timers */
2344 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2345 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2347 /* Delete non-primary peer ip addresses since we are transitioning
2348 * back to the COOKIE-WAIT state
2350 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2352 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2353 * resend
2355 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2356 SCTP_TRANSPORT(asoc->peer.primary_path));
2358 /* Cast away the const modifier, as we want to just
2359 * rerun it through as a sideffect.
2361 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2364 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2365 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2366 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2367 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2368 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2370 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2372 return SCTP_DISPOSITION_CONSUME;
2374 nomem:
2375 return SCTP_DISPOSITION_NOMEM;
2379 * Process an ABORT.
2381 * Section: 9.1
2382 * After checking the Verification Tag, the receiving endpoint shall
2383 * remove the association from its record, and shall report the
2384 * termination to its upper layer.
2386 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2387 * B) Rules for packet carrying ABORT:
2389 * - The endpoint shall always fill in the Verification Tag field of the
2390 * outbound packet with the destination endpoint's tag value if it
2391 * is known.
2393 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2394 * MUST follow the procedure described in Section 8.4.
2396 * - The receiver MUST accept the packet if the Verification Tag
2397 * matches either its own tag, OR the tag of its peer. Otherwise, the
2398 * receiver MUST silently discard the packet and take no further
2399 * action.
2401 * Inputs
2402 * (endpoint, asoc, chunk)
2404 * Outputs
2405 * (asoc, reply_msg, msg_up, timers, counters)
2407 * The return value is the disposition of the chunk.
2409 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2410 const struct sctp_endpoint *ep,
2411 const struct sctp_association *asoc,
2412 const sctp_subtype_t type,
2413 void *arg,
2414 sctp_cmd_seq_t *commands)
2416 struct sctp_chunk *chunk = arg;
2418 if (!sctp_vtag_verify_either(chunk, asoc))
2419 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2421 /* Make sure that the ABORT chunk has a valid length.
2422 * Since this is an ABORT chunk, we have to discard it
2423 * because of the following text:
2424 * RFC 2960, Section 3.3.7
2425 * If an endpoint receives an ABORT with a format error or for an
2426 * association that doesn't exist, it MUST silently discard it.
2427 * Because the length is "invalid", we can't really discard just
2428 * as we do not know its true length. So, to be safe, discard the
2429 * packet.
2431 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2432 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2434 /* ADD-IP: Special case for ABORT chunks
2435 * F4) One special consideration is that ABORT Chunks arriving
2436 * destined to the IP address being deleted MUST be
2437 * ignored (see Section 5.3.1 for further details).
2439 if (SCTP_ADDR_DEL ==
2440 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2441 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2443 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2446 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2447 const struct sctp_endpoint *ep,
2448 const struct sctp_association *asoc,
2449 const sctp_subtype_t type,
2450 void *arg,
2451 sctp_cmd_seq_t *commands)
2453 struct sctp_chunk *chunk = arg;
2454 unsigned int len;
2455 __be16 error = SCTP_ERROR_NO_ERROR;
2457 /* See if we have an error cause code in the chunk. */
2458 len = ntohs(chunk->chunk_hdr->length);
2459 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2461 sctp_errhdr_t *err;
2462 sctp_walk_errors(err, chunk->chunk_hdr);
2463 if ((void *)err != (void *)chunk->chunk_end)
2464 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2466 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2469 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2470 /* ASSOC_FAILED will DELETE_TCB. */
2471 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2472 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2473 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2475 return SCTP_DISPOSITION_ABORT;
2479 * Process an ABORT. (COOKIE-WAIT state)
2481 * See sctp_sf_do_9_1_abort() above.
2483 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2484 const struct sctp_endpoint *ep,
2485 const struct sctp_association *asoc,
2486 const sctp_subtype_t type,
2487 void *arg,
2488 sctp_cmd_seq_t *commands)
2490 struct sctp_chunk *chunk = arg;
2491 unsigned int len;
2492 __be16 error = SCTP_ERROR_NO_ERROR;
2494 if (!sctp_vtag_verify_either(chunk, asoc))
2495 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2497 /* Make sure that the ABORT chunk has a valid length.
2498 * Since this is an ABORT chunk, we have to discard it
2499 * because of the following text:
2500 * RFC 2960, Section 3.3.7
2501 * If an endpoint receives an ABORT with a format error or for an
2502 * association that doesn't exist, it MUST silently discard it.
2503 * Because the length is "invalid", we can't really discard just
2504 * as we do not know its true length. So, to be safe, discard the
2505 * packet.
2507 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2508 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2510 /* See if we have an error cause code in the chunk. */
2511 len = ntohs(chunk->chunk_hdr->length);
2512 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2513 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2515 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2516 chunk->transport);
2520 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2522 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2523 const struct sctp_endpoint *ep,
2524 const struct sctp_association *asoc,
2525 const sctp_subtype_t type,
2526 void *arg,
2527 sctp_cmd_seq_t *commands)
2529 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2530 ENOPROTOOPT, asoc,
2531 (struct sctp_transport *)arg);
2535 * Process an ABORT. (COOKIE-ECHOED state)
2537 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2538 const struct sctp_endpoint *ep,
2539 const struct sctp_association *asoc,
2540 const sctp_subtype_t type,
2541 void *arg,
2542 sctp_cmd_seq_t *commands)
2544 /* There is a single T1 timer, so we should be able to use
2545 * common function with the COOKIE-WAIT state.
2547 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2551 * Stop T1 timer and abort association with "INIT failed".
2553 * This is common code called by several sctp_sf_*_abort() functions above.
2555 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2556 sctp_cmd_seq_t *commands,
2557 __be16 error, int sk_err,
2558 const struct sctp_association *asoc,
2559 struct sctp_transport *transport)
2561 pr_debug("%s: ABORT received (INIT)\n", __func__);
2563 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2564 SCTP_STATE(SCTP_STATE_CLOSED));
2565 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2566 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2567 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2568 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2569 /* CMD_INIT_FAILED will DELETE_TCB. */
2570 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2571 SCTP_PERR(error));
2573 return SCTP_DISPOSITION_ABORT;
2577 * sctp_sf_do_9_2_shut
2579 * Section: 9.2
2580 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2581 * - enter the SHUTDOWN-RECEIVED state,
2583 * - stop accepting new data from its SCTP user
2585 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2586 * that all its outstanding DATA chunks have been received by the
2587 * SHUTDOWN sender.
2589 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2590 * send a SHUTDOWN in response to a ULP request. And should discard
2591 * subsequent SHUTDOWN chunks.
2593 * If there are still outstanding DATA chunks left, the SHUTDOWN
2594 * receiver shall continue to follow normal data transmission
2595 * procedures defined in Section 6 until all outstanding DATA chunks
2596 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2597 * new data from its SCTP user.
2599 * Verification Tag: 8.5 Verification Tag [Normal verification]
2601 * Inputs
2602 * (endpoint, asoc, chunk)
2604 * Outputs
2605 * (asoc, reply_msg, msg_up, timers, counters)
2607 * The return value is the disposition of the chunk.
2609 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2610 const struct sctp_endpoint *ep,
2611 const struct sctp_association *asoc,
2612 const sctp_subtype_t type,
2613 void *arg,
2614 sctp_cmd_seq_t *commands)
2616 struct sctp_chunk *chunk = arg;
2617 sctp_shutdownhdr_t *sdh;
2618 sctp_disposition_t disposition;
2619 struct sctp_ulpevent *ev;
2620 __u32 ctsn;
2622 if (!sctp_vtag_verify(chunk, asoc))
2623 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2625 /* Make sure that the SHUTDOWN chunk has a valid length. */
2626 if (!sctp_chunk_length_valid(chunk,
2627 sizeof(struct sctp_shutdown_chunk_t)))
2628 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2629 commands);
2631 /* Convert the elaborate header. */
2632 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2633 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2634 chunk->subh.shutdown_hdr = sdh;
2635 ctsn = ntohl(sdh->cum_tsn_ack);
2637 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2638 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2639 asoc->ctsn_ack_point);
2641 return SCTP_DISPOSITION_DISCARD;
2644 /* If Cumulative TSN Ack beyond the max tsn currently
2645 * send, terminating the association and respond to the
2646 * sender with an ABORT.
2648 if (!TSN_lt(ctsn, asoc->next_tsn))
2649 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2651 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2652 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2653 * inform the application that it should cease sending data.
2655 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2656 if (!ev) {
2657 disposition = SCTP_DISPOSITION_NOMEM;
2658 goto out;
2660 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2662 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2663 * - enter the SHUTDOWN-RECEIVED state,
2664 * - stop accepting new data from its SCTP user
2666 * [This is implicit in the new state.]
2668 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2669 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2670 disposition = SCTP_DISPOSITION_CONSUME;
2672 if (sctp_outq_is_empty(&asoc->outqueue)) {
2673 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2674 arg, commands);
2677 if (SCTP_DISPOSITION_NOMEM == disposition)
2678 goto out;
2680 /* - verify, by checking the Cumulative TSN Ack field of the
2681 * chunk, that all its outstanding DATA chunks have been
2682 * received by the SHUTDOWN sender.
2684 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2685 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2687 out:
2688 return disposition;
2692 * sctp_sf_do_9_2_shut_ctsn
2694 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2695 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2696 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2697 * MUST be processed.
2699 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2700 const struct sctp_endpoint *ep,
2701 const struct sctp_association *asoc,
2702 const sctp_subtype_t type,
2703 void *arg,
2704 sctp_cmd_seq_t *commands)
2706 struct sctp_chunk *chunk = arg;
2707 sctp_shutdownhdr_t *sdh;
2708 __u32 ctsn;
2710 if (!sctp_vtag_verify(chunk, asoc))
2711 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2713 /* Make sure that the SHUTDOWN chunk has a valid length. */
2714 if (!sctp_chunk_length_valid(chunk,
2715 sizeof(struct sctp_shutdown_chunk_t)))
2716 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2717 commands);
2719 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2720 ctsn = ntohl(sdh->cum_tsn_ack);
2722 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2723 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2724 asoc->ctsn_ack_point);
2726 return SCTP_DISPOSITION_DISCARD;
2729 /* If Cumulative TSN Ack beyond the max tsn currently
2730 * send, terminating the association and respond to the
2731 * sender with an ABORT.
2733 if (!TSN_lt(ctsn, asoc->next_tsn))
2734 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2736 /* verify, by checking the Cumulative TSN Ack field of the
2737 * chunk, that all its outstanding DATA chunks have been
2738 * received by the SHUTDOWN sender.
2740 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2741 SCTP_BE32(sdh->cum_tsn_ack));
2743 return SCTP_DISPOSITION_CONSUME;
2746 /* RFC 2960 9.2
2747 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2748 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2749 * transport addresses (either in the IP addresses or in the INIT chunk)
2750 * that belong to this association, it should discard the INIT chunk and
2751 * retransmit the SHUTDOWN ACK chunk.
2753 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2754 const struct sctp_endpoint *ep,
2755 const struct sctp_association *asoc,
2756 const sctp_subtype_t type,
2757 void *arg,
2758 sctp_cmd_seq_t *commands)
2760 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2761 struct sctp_chunk *reply;
2763 /* Make sure that the chunk has a valid length */
2764 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2765 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2766 commands);
2768 /* Since we are not going to really process this INIT, there
2769 * is no point in verifying chunk boundries. Just generate
2770 * the SHUTDOWN ACK.
2772 reply = sctp_make_shutdown_ack(asoc, chunk);
2773 if (NULL == reply)
2774 goto nomem;
2776 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2777 * the T2-SHUTDOWN timer.
2779 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2781 /* and restart the T2-shutdown timer. */
2782 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2783 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2787 return SCTP_DISPOSITION_CONSUME;
2788 nomem:
2789 return SCTP_DISPOSITION_NOMEM;
2793 * sctp_sf_do_ecn_cwr
2795 * Section: Appendix A: Explicit Congestion Notification
2797 * CWR:
2799 * RFC 2481 details a specific bit for a sender to send in the header of
2800 * its next outbound TCP segment to indicate to its peer that it has
2801 * reduced its congestion window. This is termed the CWR bit. For
2802 * SCTP the same indication is made by including the CWR chunk.
2803 * This chunk contains one data element, i.e. the TSN number that
2804 * was sent in the ECNE chunk. This element represents the lowest
2805 * TSN number in the datagram that was originally marked with the
2806 * CE bit.
2808 * Verification Tag: 8.5 Verification Tag [Normal verification]
2809 * Inputs
2810 * (endpoint, asoc, chunk)
2812 * Outputs
2813 * (asoc, reply_msg, msg_up, timers, counters)
2815 * The return value is the disposition of the chunk.
2817 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2818 const struct sctp_endpoint *ep,
2819 const struct sctp_association *asoc,
2820 const sctp_subtype_t type,
2821 void *arg,
2822 sctp_cmd_seq_t *commands)
2824 sctp_cwrhdr_t *cwr;
2825 struct sctp_chunk *chunk = arg;
2826 u32 lowest_tsn;
2828 if (!sctp_vtag_verify(chunk, asoc))
2829 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2831 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2832 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2833 commands);
2835 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2836 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2838 lowest_tsn = ntohl(cwr->lowest_tsn);
2840 /* Does this CWR ack the last sent congestion notification? */
2841 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2842 /* Stop sending ECNE. */
2843 sctp_add_cmd_sf(commands,
2844 SCTP_CMD_ECN_CWR,
2845 SCTP_U32(lowest_tsn));
2847 return SCTP_DISPOSITION_CONSUME;
2851 * sctp_sf_do_ecne
2853 * Section: Appendix A: Explicit Congestion Notification
2855 * ECN-Echo
2857 * RFC 2481 details a specific bit for a receiver to send back in its
2858 * TCP acknowledgements to notify the sender of the Congestion
2859 * Experienced (CE) bit having arrived from the network. For SCTP this
2860 * same indication is made by including the ECNE chunk. This chunk
2861 * contains one data element, i.e. the lowest TSN associated with the IP
2862 * datagram marked with the CE bit.....
2864 * Verification Tag: 8.5 Verification Tag [Normal verification]
2865 * Inputs
2866 * (endpoint, asoc, chunk)
2868 * Outputs
2869 * (asoc, reply_msg, msg_up, timers, counters)
2871 * The return value is the disposition of the chunk.
2873 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2874 const struct sctp_endpoint *ep,
2875 const struct sctp_association *asoc,
2876 const sctp_subtype_t type,
2877 void *arg,
2878 sctp_cmd_seq_t *commands)
2880 sctp_ecnehdr_t *ecne;
2881 struct sctp_chunk *chunk = arg;
2883 if (!sctp_vtag_verify(chunk, asoc))
2884 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2886 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2887 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2888 commands);
2890 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2891 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2893 /* If this is a newer ECNE than the last CWR packet we sent out */
2894 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2895 SCTP_U32(ntohl(ecne->lowest_tsn)));
2897 return SCTP_DISPOSITION_CONSUME;
2901 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2903 * The SCTP endpoint MUST always acknowledge the reception of each valid
2904 * DATA chunk.
2906 * The guidelines on delayed acknowledgement algorithm specified in
2907 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2908 * acknowledgement SHOULD be generated for at least every second packet
2909 * (not every second DATA chunk) received, and SHOULD be generated within
2910 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2911 * situations it may be beneficial for an SCTP transmitter to be more
2912 * conservative than the algorithms detailed in this document allow.
2913 * However, an SCTP transmitter MUST NOT be more aggressive than the
2914 * following algorithms allow.
2916 * A SCTP receiver MUST NOT generate more than one SACK for every
2917 * incoming packet, other than to update the offered window as the
2918 * receiving application consumes new data.
2920 * Verification Tag: 8.5 Verification Tag [Normal verification]
2922 * Inputs
2923 * (endpoint, asoc, chunk)
2925 * Outputs
2926 * (asoc, reply_msg, msg_up, timers, counters)
2928 * The return value is the disposition of the chunk.
2930 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2931 const struct sctp_endpoint *ep,
2932 const struct sctp_association *asoc,
2933 const sctp_subtype_t type,
2934 void *arg,
2935 sctp_cmd_seq_t *commands)
2937 struct sctp_chunk *chunk = arg;
2938 sctp_arg_t force = SCTP_NOFORCE();
2939 int error;
2941 if (!sctp_vtag_verify(chunk, asoc)) {
2942 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2943 SCTP_NULL());
2944 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2947 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2948 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2949 commands);
2951 error = sctp_eat_data(asoc, chunk, commands );
2952 switch (error) {
2953 case SCTP_IERROR_NO_ERROR:
2954 break;
2955 case SCTP_IERROR_HIGH_TSN:
2956 case SCTP_IERROR_BAD_STREAM:
2957 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2958 goto discard_noforce;
2959 case SCTP_IERROR_DUP_TSN:
2960 case SCTP_IERROR_IGNORE_TSN:
2961 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2962 goto discard_force;
2963 case SCTP_IERROR_NO_DATA:
2964 goto consume;
2965 case SCTP_IERROR_PROTO_VIOLATION:
2966 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
2967 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2968 default:
2969 BUG();
2972 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2973 force = SCTP_FORCE();
2975 if (asoc->autoclose) {
2976 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2977 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2980 /* If this is the last chunk in a packet, we need to count it
2981 * toward sack generation. Note that we need to SACK every
2982 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2983 * THEM. We elect to NOT generate SACK's if the chunk fails
2984 * the verification tag test.
2986 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2988 * The SCTP endpoint MUST always acknowledge the reception of
2989 * each valid DATA chunk.
2991 * The guidelines on delayed acknowledgement algorithm
2992 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2993 * Specifically, an acknowledgement SHOULD be generated for at
2994 * least every second packet (not every second DATA chunk)
2995 * received, and SHOULD be generated within 200 ms of the
2996 * arrival of any unacknowledged DATA chunk. In some
2997 * situations it may be beneficial for an SCTP transmitter to
2998 * be more conservative than the algorithms detailed in this
2999 * document allow. However, an SCTP transmitter MUST NOT be
3000 * more aggressive than the following algorithms allow.
3002 if (chunk->end_of_packet)
3003 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3005 return SCTP_DISPOSITION_CONSUME;
3007 discard_force:
3008 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3010 * When a packet arrives with duplicate DATA chunk(s) and with
3011 * no new DATA chunk(s), the endpoint MUST immediately send a
3012 * SACK with no delay. If a packet arrives with duplicate
3013 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3014 * MAY immediately send a SACK. Normally receipt of duplicate
3015 * DATA chunks will occur when the original SACK chunk was lost
3016 * and the peer's RTO has expired. The duplicate TSN number(s)
3017 * SHOULD be reported in the SACK as duplicate.
3019 /* In our case, we split the MAY SACK advice up whether or not
3020 * the last chunk is a duplicate.'
3022 if (chunk->end_of_packet)
3023 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3024 return SCTP_DISPOSITION_DISCARD;
3026 discard_noforce:
3027 if (chunk->end_of_packet)
3028 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3030 return SCTP_DISPOSITION_DISCARD;
3031 consume:
3032 return SCTP_DISPOSITION_CONSUME;
3037 * sctp_sf_eat_data_fast_4_4
3039 * Section: 4 (4)
3040 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3041 * DATA chunks without delay.
3043 * Verification Tag: 8.5 Verification Tag [Normal verification]
3044 * Inputs
3045 * (endpoint, asoc, chunk)
3047 * Outputs
3048 * (asoc, reply_msg, msg_up, timers, counters)
3050 * The return value is the disposition of the chunk.
3052 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3053 const struct sctp_endpoint *ep,
3054 const struct sctp_association *asoc,
3055 const sctp_subtype_t type,
3056 void *arg,
3057 sctp_cmd_seq_t *commands)
3059 struct sctp_chunk *chunk = arg;
3060 int error;
3062 if (!sctp_vtag_verify(chunk, asoc)) {
3063 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3064 SCTP_NULL());
3065 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3068 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3069 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3070 commands);
3072 error = sctp_eat_data(asoc, chunk, commands );
3073 switch (error) {
3074 case SCTP_IERROR_NO_ERROR:
3075 case SCTP_IERROR_HIGH_TSN:
3076 case SCTP_IERROR_DUP_TSN:
3077 case SCTP_IERROR_IGNORE_TSN:
3078 case SCTP_IERROR_BAD_STREAM:
3079 break;
3080 case SCTP_IERROR_NO_DATA:
3081 goto consume;
3082 case SCTP_IERROR_PROTO_VIOLATION:
3083 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3084 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3085 default:
3086 BUG();
3089 /* Go a head and force a SACK, since we are shutting down. */
3091 /* Implementor's Guide.
3093 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3094 * respond to each received packet containing one or more DATA chunk(s)
3095 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3097 if (chunk->end_of_packet) {
3098 /* We must delay the chunk creation since the cumulative
3099 * TSN has not been updated yet.
3101 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3102 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3103 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3104 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3107 consume:
3108 return SCTP_DISPOSITION_CONSUME;
3112 * Section: 6.2 Processing a Received SACK
3113 * D) Any time a SACK arrives, the endpoint performs the following:
3115 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3116 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3117 * increasing, a SACK whose Cumulative TSN Ack is less than the
3118 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3120 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3121 * of bytes still outstanding after processing the Cumulative TSN Ack
3122 * and the Gap Ack Blocks.
3124 * iii) If the SACK is missing a TSN that was previously
3125 * acknowledged via a Gap Ack Block (e.g., the data receiver
3126 * reneged on the data), then mark the corresponding DATA chunk
3127 * as available for retransmit: Mark it as missing for fast
3128 * retransmit as described in Section 7.2.4 and if no retransmit
3129 * timer is running for the destination address to which the DATA
3130 * chunk was originally transmitted, then T3-rtx is started for
3131 * that destination address.
3133 * Verification Tag: 8.5 Verification Tag [Normal verification]
3135 * Inputs
3136 * (endpoint, asoc, chunk)
3138 * Outputs
3139 * (asoc, reply_msg, msg_up, timers, counters)
3141 * The return value is the disposition of the chunk.
3143 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3144 const struct sctp_endpoint *ep,
3145 const struct sctp_association *asoc,
3146 const sctp_subtype_t type,
3147 void *arg,
3148 sctp_cmd_seq_t *commands)
3150 struct sctp_chunk *chunk = arg;
3151 sctp_sackhdr_t *sackh;
3152 __u32 ctsn;
3154 if (!sctp_vtag_verify(chunk, asoc))
3155 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3157 /* Make sure that the SACK chunk has a valid length. */
3158 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3159 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3160 commands);
3162 /* Pull the SACK chunk from the data buffer */
3163 sackh = sctp_sm_pull_sack(chunk);
3164 /* Was this a bogus SACK? */
3165 if (!sackh)
3166 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3167 chunk->subh.sack_hdr = sackh;
3168 ctsn = ntohl(sackh->cum_tsn_ack);
3170 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3171 * Ack Point, then drop the SACK. Since Cumulative TSN
3172 * Ack is monotonically increasing, a SACK whose
3173 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3174 * Point indicates an out-of-order SACK.
3176 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3177 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3178 asoc->ctsn_ack_point);
3180 return SCTP_DISPOSITION_DISCARD;
3183 /* If Cumulative TSN Ack beyond the max tsn currently
3184 * send, terminating the association and respond to the
3185 * sender with an ABORT.
3187 if (!TSN_lt(ctsn, asoc->next_tsn))
3188 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3190 /* Return this SACK for further processing. */
3191 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3193 /* Note: We do the rest of the work on the PROCESS_SACK
3194 * sideeffect.
3196 return SCTP_DISPOSITION_CONSUME;
3200 * Generate an ABORT in response to a packet.
3202 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3204 * 8) The receiver should respond to the sender of the OOTB packet with
3205 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3206 * MUST fill in the Verification Tag field of the outbound packet
3207 * with the value found in the Verification Tag field of the OOTB
3208 * packet and set the T-bit in the Chunk Flags to indicate that the
3209 * Verification Tag is reflected. After sending this ABORT, the
3210 * receiver of the OOTB packet shall discard the OOTB packet and take
3211 * no further action.
3213 * Verification Tag:
3215 * The return value is the disposition of the chunk.
3217 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3218 const struct sctp_endpoint *ep,
3219 const struct sctp_association *asoc,
3220 const sctp_subtype_t type,
3221 void *arg,
3222 sctp_cmd_seq_t *commands)
3224 struct sctp_packet *packet = NULL;
3225 struct sctp_chunk *chunk = arg;
3226 struct sctp_chunk *abort;
3228 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3230 if (packet) {
3231 /* Make an ABORT. The T bit will be set if the asoc
3232 * is NULL.
3234 abort = sctp_make_abort(asoc, chunk, 0);
3235 if (!abort) {
3236 sctp_ootb_pkt_free(packet);
3237 return SCTP_DISPOSITION_NOMEM;
3240 /* Reflect vtag if T-Bit is set */
3241 if (sctp_test_T_bit(abort))
3242 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3244 /* Set the skb to the belonging sock for accounting. */
3245 abort->skb->sk = ep->base.sk;
3247 sctp_packet_append_chunk(packet, abort);
3249 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3250 SCTP_PACKET(packet));
3252 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3254 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3255 return SCTP_DISPOSITION_CONSUME;
3258 return SCTP_DISPOSITION_NOMEM;
3262 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3263 * event as ULP notification for each cause included in the chunk.
3265 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3267 * The return value is the disposition of the chunk.
3269 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3270 const struct sctp_endpoint *ep,
3271 const struct sctp_association *asoc,
3272 const sctp_subtype_t type,
3273 void *arg,
3274 sctp_cmd_seq_t *commands)
3276 struct sctp_chunk *chunk = arg;
3277 sctp_errhdr_t *err;
3279 if (!sctp_vtag_verify(chunk, asoc))
3280 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3282 /* Make sure that the ERROR chunk has a valid length. */
3283 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3284 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3285 commands);
3286 sctp_walk_errors(err, chunk->chunk_hdr);
3287 if ((void *)err != (void *)chunk->chunk_end)
3288 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3289 (void *)err, commands);
3291 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3292 SCTP_CHUNK(chunk));
3294 return SCTP_DISPOSITION_CONSUME;
3298 * Process an inbound SHUTDOWN ACK.
3300 * From Section 9.2:
3301 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3302 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3303 * peer, and remove all record of the association.
3305 * The return value is the disposition.
3307 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3308 const struct sctp_endpoint *ep,
3309 const struct sctp_association *asoc,
3310 const sctp_subtype_t type,
3311 void *arg,
3312 sctp_cmd_seq_t *commands)
3314 struct sctp_chunk *chunk = arg;
3315 struct sctp_chunk *reply;
3316 struct sctp_ulpevent *ev;
3318 if (!sctp_vtag_verify(chunk, asoc))
3319 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3321 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3322 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3323 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3324 commands);
3325 /* 10.2 H) SHUTDOWN COMPLETE notification
3327 * When SCTP completes the shutdown procedures (section 9.2) this
3328 * notification is passed to the upper layer.
3330 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3331 0, 0, 0, NULL, GFP_ATOMIC);
3332 if (!ev)
3333 goto nomem;
3335 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3336 reply = sctp_make_shutdown_complete(asoc, chunk);
3337 if (!reply)
3338 goto nomem_chunk;
3340 /* Do all the commands now (after allocation), so that we
3341 * have consistent state if memory allocation failes
3343 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3345 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3346 * stop the T2-shutdown timer,
3348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3349 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3351 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3352 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3354 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3355 SCTP_STATE(SCTP_STATE_CLOSED));
3356 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3357 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3358 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3360 /* ...and remove all record of the association. */
3361 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3362 return SCTP_DISPOSITION_DELETE_TCB;
3364 nomem_chunk:
3365 sctp_ulpevent_free(ev);
3366 nomem:
3367 return SCTP_DISPOSITION_NOMEM;
3371 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3373 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3374 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3375 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3376 * packet must fill in the Verification Tag field of the outbound
3377 * packet with the Verification Tag received in the SHUTDOWN ACK and
3378 * set the T-bit in the Chunk Flags to indicate that the Verification
3379 * Tag is reflected.
3381 * 8) The receiver should respond to the sender of the OOTB packet with
3382 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3383 * MUST fill in the Verification Tag field of the outbound packet
3384 * with the value found in the Verification Tag field of the OOTB
3385 * packet and set the T-bit in the Chunk Flags to indicate that the
3386 * Verification Tag is reflected. After sending this ABORT, the
3387 * receiver of the OOTB packet shall discard the OOTB packet and take
3388 * no further action.
3390 sctp_disposition_t sctp_sf_ootb(struct net *net,
3391 const struct sctp_endpoint *ep,
3392 const struct sctp_association *asoc,
3393 const sctp_subtype_t type,
3394 void *arg,
3395 sctp_cmd_seq_t *commands)
3397 struct sctp_chunk *chunk = arg;
3398 struct sk_buff *skb = chunk->skb;
3399 sctp_chunkhdr_t *ch;
3400 sctp_errhdr_t *err;
3401 __u8 *ch_end;
3402 int ootb_shut_ack = 0;
3403 int ootb_cookie_ack = 0;
3405 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3407 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3408 do {
3409 /* Report violation if the chunk is less then minimal */
3410 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3411 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3412 commands);
3414 /* Now that we know we at least have a chunk header,
3415 * do things that are type appropriate.
3417 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3418 ootb_shut_ack = 1;
3420 /* RFC 2960, Section 3.3.7
3421 * Moreover, under any circumstances, an endpoint that
3422 * receives an ABORT MUST NOT respond to that ABORT by
3423 * sending an ABORT of its own.
3425 if (SCTP_CID_ABORT == ch->type)
3426 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3428 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3429 * or a COOKIE ACK the SCTP Packet should be silently
3430 * discarded.
3433 if (SCTP_CID_COOKIE_ACK == ch->type)
3434 ootb_cookie_ack = 1;
3436 if (SCTP_CID_ERROR == ch->type) {
3437 sctp_walk_errors(err, ch) {
3438 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3439 ootb_cookie_ack = 1;
3440 break;
3445 /* Report violation if chunk len overflows */
3446 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3447 if (ch_end > skb_tail_pointer(skb))
3448 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3449 commands);
3451 ch = (sctp_chunkhdr_t *) ch_end;
3452 } while (ch_end < skb_tail_pointer(skb));
3454 if (ootb_shut_ack)
3455 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3456 else if (ootb_cookie_ack)
3457 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3458 else
3459 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3463 * Handle an "Out of the blue" SHUTDOWN ACK.
3465 * Section: 8.4 5, sctpimpguide 2.41.
3467 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3468 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3469 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3470 * packet must fill in the Verification Tag field of the outbound
3471 * packet with the Verification Tag received in the SHUTDOWN ACK and
3472 * set the T-bit in the Chunk Flags to indicate that the Verification
3473 * Tag is reflected.
3475 * Inputs
3476 * (endpoint, asoc, type, arg, commands)
3478 * Outputs
3479 * (sctp_disposition_t)
3481 * The return value is the disposition of the chunk.
3483 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3484 const struct sctp_endpoint *ep,
3485 const struct sctp_association *asoc,
3486 const sctp_subtype_t type,
3487 void *arg,
3488 sctp_cmd_seq_t *commands)
3490 struct sctp_packet *packet = NULL;
3491 struct sctp_chunk *chunk = arg;
3492 struct sctp_chunk *shut;
3494 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3496 if (packet) {
3497 /* Make an SHUTDOWN_COMPLETE.
3498 * The T bit will be set if the asoc is NULL.
3500 shut = sctp_make_shutdown_complete(asoc, chunk);
3501 if (!shut) {
3502 sctp_ootb_pkt_free(packet);
3503 return SCTP_DISPOSITION_NOMEM;
3506 /* Reflect vtag if T-Bit is set */
3507 if (sctp_test_T_bit(shut))
3508 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3510 /* Set the skb to the belonging sock for accounting. */
3511 shut->skb->sk = ep->base.sk;
3513 sctp_packet_append_chunk(packet, shut);
3515 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3516 SCTP_PACKET(packet));
3518 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3520 /* If the chunk length is invalid, we don't want to process
3521 * the reset of the packet.
3523 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3524 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3526 /* We need to discard the rest of the packet to prevent
3527 * potential bomming attacks from additional bundled chunks.
3528 * This is documented in SCTP Threats ID.
3530 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3533 return SCTP_DISPOSITION_NOMEM;
3537 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3539 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3540 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3541 * procedures in section 8.4 SHOULD be followed, in other words it
3542 * should be treated as an Out Of The Blue packet.
3543 * [This means that we do NOT check the Verification Tag on these
3544 * chunks. --piggy ]
3547 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3548 const struct sctp_endpoint *ep,
3549 const struct sctp_association *asoc,
3550 const sctp_subtype_t type,
3551 void *arg,
3552 sctp_cmd_seq_t *commands)
3554 struct sctp_chunk *chunk = arg;
3556 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3557 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3558 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3559 commands);
3561 /* Although we do have an association in this case, it corresponds
3562 * to a restarted association. So the packet is treated as an OOTB
3563 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3564 * called with a NULL association.
3566 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3568 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3571 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3572 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3573 const struct sctp_endpoint *ep,
3574 const struct sctp_association *asoc,
3575 const sctp_subtype_t type, void *arg,
3576 sctp_cmd_seq_t *commands)
3578 struct sctp_chunk *chunk = arg;
3579 struct sctp_chunk *asconf_ack = NULL;
3580 struct sctp_paramhdr *err_param = NULL;
3581 sctp_addiphdr_t *hdr;
3582 union sctp_addr_param *addr_param;
3583 __u32 serial;
3584 int length;
3586 if (!sctp_vtag_verify(chunk, asoc)) {
3587 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3588 SCTP_NULL());
3589 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3592 /* ADD-IP: Section 4.1.1
3593 * This chunk MUST be sent in an authenticated way by using
3594 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3595 * is received unauthenticated it MUST be silently discarded as
3596 * described in [I-D.ietf-tsvwg-sctp-auth].
3598 if (!net->sctp.addip_noauth && !chunk->auth)
3599 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3601 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3602 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3603 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3604 commands);
3606 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3607 serial = ntohl(hdr->serial);
3609 addr_param = (union sctp_addr_param *)hdr->params;
3610 length = ntohs(addr_param->p.length);
3611 if (length < sizeof(sctp_paramhdr_t))
3612 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3613 (void *)addr_param, commands);
3615 /* Verify the ASCONF chunk before processing it. */
3616 if (!sctp_verify_asconf(asoc,
3617 (sctp_paramhdr_t *)((void *)addr_param + length),
3618 (void *)chunk->chunk_end,
3619 &err_param))
3620 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3621 (void *)err_param, commands);
3623 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3624 * the endpoint stored in a new association variable
3625 * 'Peer-Serial-Number'.
3627 if (serial == asoc->peer.addip_serial + 1) {
3628 /* If this is the first instance of ASCONF in the packet,
3629 * we can clean our old ASCONF-ACKs.
3631 if (!chunk->has_asconf)
3632 sctp_assoc_clean_asconf_ack_cache(asoc);
3634 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3635 * expected, process the ASCONF as described below and after
3636 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3637 * the response packet and cache a copy of it (in the event it
3638 * later needs to be retransmitted).
3640 * Essentially, do V1-V5.
3642 asconf_ack = sctp_process_asconf((struct sctp_association *)
3643 asoc, chunk);
3644 if (!asconf_ack)
3645 return SCTP_DISPOSITION_NOMEM;
3646 } else if (serial < asoc->peer.addip_serial + 1) {
3647 /* ADDIP 5.2 E2)
3648 * If the value found in the Sequence Number is less than the
3649 * ('Peer- Sequence-Number' + 1), simply skip to the next
3650 * ASCONF, and include in the outbound response packet
3651 * any previously cached ASCONF-ACK response that was
3652 * sent and saved that matches the Sequence Number of the
3653 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3654 * Chunk exists. This will occur when an older ASCONF
3655 * arrives out of order. In such a case, the receiver
3656 * should skip the ASCONF Chunk and not include ASCONF-ACK
3657 * Chunk for that chunk.
3659 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3660 if (!asconf_ack)
3661 return SCTP_DISPOSITION_DISCARD;
3663 /* Reset the transport so that we select the correct one
3664 * this time around. This is to make sure that we don't
3665 * accidentally use a stale transport that's been removed.
3667 asconf_ack->transport = NULL;
3668 } else {
3669 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3670 * it must be either a stale packet or from an attacker.
3672 return SCTP_DISPOSITION_DISCARD;
3675 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3676 * containing the ASCONF-ACK Chunks MUST be the source address of
3677 * the SCTP packet that held the ASCONF Chunks.
3679 * To do this properly, we'll set the destination address of the chunk
3680 * and at the transmit time, will try look up the transport to use.
3681 * Since ASCONFs may be bundled, the correct transport may not be
3682 * created until we process the entire packet, thus this workaround.
3684 asconf_ack->dest = chunk->source;
3685 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3686 if (asoc->new_transport) {
3687 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3688 commands);
3689 ((struct sctp_association *)asoc)->new_transport = NULL;
3692 return SCTP_DISPOSITION_CONSUME;
3696 * ADDIP Section 4.3 General rules for address manipulation
3697 * When building TLV parameters for the ASCONF Chunk that will add or
3698 * delete IP addresses the D0 to D13 rules should be applied:
3700 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3701 const struct sctp_endpoint *ep,
3702 const struct sctp_association *asoc,
3703 const sctp_subtype_t type, void *arg,
3704 sctp_cmd_seq_t *commands)
3706 struct sctp_chunk *asconf_ack = arg;
3707 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3708 struct sctp_chunk *abort;
3709 struct sctp_paramhdr *err_param = NULL;
3710 sctp_addiphdr_t *addip_hdr;
3711 __u32 sent_serial, rcvd_serial;
3713 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3714 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3715 SCTP_NULL());
3716 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3719 /* ADD-IP, Section 4.1.2:
3720 * This chunk MUST be sent in an authenticated way by using
3721 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3722 * is received unauthenticated it MUST be silently discarded as
3723 * described in [I-D.ietf-tsvwg-sctp-auth].
3725 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3726 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3728 /* Make sure that the ADDIP chunk has a valid length. */
3729 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3730 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3731 commands);
3733 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3734 rcvd_serial = ntohl(addip_hdr->serial);
3736 /* Verify the ASCONF-ACK chunk before processing it. */
3737 if (!sctp_verify_asconf(asoc,
3738 (sctp_paramhdr_t *)addip_hdr->params,
3739 (void *)asconf_ack->chunk_end,
3740 &err_param))
3741 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3742 (void *)err_param, commands);
3744 if (last_asconf) {
3745 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3746 sent_serial = ntohl(addip_hdr->serial);
3747 } else {
3748 sent_serial = asoc->addip_serial - 1;
3751 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3752 * equal to the next serial number to be used but no ASCONF chunk is
3753 * outstanding the endpoint MUST ABORT the association. Note that a
3754 * sequence number is greater than if it is no more than 2^^31-1
3755 * larger than the current sequence number (using serial arithmetic).
3757 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3758 !(asoc->addip_last_asconf)) {
3759 abort = sctp_make_abort(asoc, asconf_ack,
3760 sizeof(sctp_errhdr_t));
3761 if (abort) {
3762 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3763 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3764 SCTP_CHUNK(abort));
3766 /* We are going to ABORT, so we might as well stop
3767 * processing the rest of the chunks in the packet.
3769 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3770 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3771 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3772 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3773 SCTP_ERROR(ECONNABORTED));
3774 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3775 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3776 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3777 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3778 return SCTP_DISPOSITION_ABORT;
3781 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3782 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3783 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3785 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3786 asconf_ack)) {
3787 /* Successfully processed ASCONF_ACK. We can
3788 * release the next asconf if we have one.
3790 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3791 SCTP_NULL());
3792 return SCTP_DISPOSITION_CONSUME;
3795 abort = sctp_make_abort(asoc, asconf_ack,
3796 sizeof(sctp_errhdr_t));
3797 if (abort) {
3798 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3799 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3800 SCTP_CHUNK(abort));
3802 /* We are going to ABORT, so we might as well stop
3803 * processing the rest of the chunks in the packet.
3805 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3806 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3807 SCTP_ERROR(ECONNABORTED));
3808 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3809 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3810 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3811 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3812 return SCTP_DISPOSITION_ABORT;
3815 return SCTP_DISPOSITION_DISCARD;
3819 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3821 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3822 * its cumulative TSN point to the value carried in the FORWARD TSN
3823 * chunk, and then MUST further advance its cumulative TSN point locally
3824 * if possible.
3825 * After the above processing, the data receiver MUST stop reporting any
3826 * missing TSNs earlier than or equal to the new cumulative TSN point.
3828 * Verification Tag: 8.5 Verification Tag [Normal verification]
3830 * The return value is the disposition of the chunk.
3832 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3833 const struct sctp_endpoint *ep,
3834 const struct sctp_association *asoc,
3835 const sctp_subtype_t type,
3836 void *arg,
3837 sctp_cmd_seq_t *commands)
3839 struct sctp_chunk *chunk = arg;
3840 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3841 struct sctp_fwdtsn_skip *skip;
3842 __u16 len;
3843 __u32 tsn;
3845 if (!sctp_vtag_verify(chunk, asoc)) {
3846 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3847 SCTP_NULL());
3848 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3851 /* Make sure that the FORWARD_TSN chunk has valid length. */
3852 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3853 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3854 commands);
3856 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3857 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3858 len = ntohs(chunk->chunk_hdr->length);
3859 len -= sizeof(struct sctp_chunkhdr);
3860 skb_pull(chunk->skb, len);
3862 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3863 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3865 /* The TSN is too high--silently discard the chunk and count on it
3866 * getting retransmitted later.
3868 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3869 goto discard_noforce;
3871 /* Silently discard the chunk if stream-id is not valid */
3872 sctp_walk_fwdtsn(skip, chunk) {
3873 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3874 goto discard_noforce;
3877 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3878 if (len > sizeof(struct sctp_fwdtsn_hdr))
3879 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3880 SCTP_CHUNK(chunk));
3882 /* Count this as receiving DATA. */
3883 if (asoc->autoclose) {
3884 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3885 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3888 /* FIXME: For now send a SACK, but DATA processing may
3889 * send another.
3891 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3893 return SCTP_DISPOSITION_CONSUME;
3895 discard_noforce:
3896 return SCTP_DISPOSITION_DISCARD;
3899 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3900 struct net *net,
3901 const struct sctp_endpoint *ep,
3902 const struct sctp_association *asoc,
3903 const sctp_subtype_t type,
3904 void *arg,
3905 sctp_cmd_seq_t *commands)
3907 struct sctp_chunk *chunk = arg;
3908 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3909 struct sctp_fwdtsn_skip *skip;
3910 __u16 len;
3911 __u32 tsn;
3913 if (!sctp_vtag_verify(chunk, asoc)) {
3914 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3915 SCTP_NULL());
3916 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3919 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3920 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3921 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3922 commands);
3924 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3925 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3926 len = ntohs(chunk->chunk_hdr->length);
3927 len -= sizeof(struct sctp_chunkhdr);
3928 skb_pull(chunk->skb, len);
3930 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3931 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3933 /* The TSN is too high--silently discard the chunk and count on it
3934 * getting retransmitted later.
3936 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3937 goto gen_shutdown;
3939 /* Silently discard the chunk if stream-id is not valid */
3940 sctp_walk_fwdtsn(skip, chunk) {
3941 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3942 goto gen_shutdown;
3945 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3946 if (len > sizeof(struct sctp_fwdtsn_hdr))
3947 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3948 SCTP_CHUNK(chunk));
3950 /* Go a head and force a SACK, since we are shutting down. */
3951 gen_shutdown:
3952 /* Implementor's Guide.
3954 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3955 * respond to each received packet containing one or more DATA chunk(s)
3956 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3958 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3959 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3960 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3961 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3963 return SCTP_DISPOSITION_CONSUME;
3967 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3969 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3970 * Identifier field. If this algorithm was not specified by the
3971 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3972 * during association setup, the AUTH chunk and all chunks after it MUST
3973 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3974 * defined in Section 4.1.
3976 * If an endpoint with no shared key receives a Shared Key Identifier
3977 * other than 0, it MUST silently discard all authenticated chunks. If
3978 * the endpoint has at least one endpoint pair shared key for the peer,
3979 * it MUST use the key specified by the Shared Key Identifier if a
3980 * key has been configured for that Shared Key Identifier. If no
3981 * endpoint pair shared key has been configured for that Shared Key
3982 * Identifier, all authenticated chunks MUST be silently discarded.
3984 * Verification Tag: 8.5 Verification Tag [Normal verification]
3986 * The return value is the disposition of the chunk.
3988 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3989 const struct sctp_endpoint *ep,
3990 const struct sctp_association *asoc,
3991 const sctp_subtype_t type,
3992 struct sctp_chunk *chunk)
3994 struct sctp_authhdr *auth_hdr;
3995 struct sctp_hmac *hmac;
3996 unsigned int sig_len;
3997 __u16 key_id;
3998 __u8 *save_digest;
3999 __u8 *digest;
4001 /* Pull in the auth header, so we can do some more verification */
4002 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4003 chunk->subh.auth_hdr = auth_hdr;
4004 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4006 /* Make sure that we support the HMAC algorithm from the auth
4007 * chunk.
4009 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4010 return SCTP_IERROR_AUTH_BAD_HMAC;
4012 /* Make sure that the provided shared key identifier has been
4013 * configured
4015 key_id = ntohs(auth_hdr->shkey_id);
4016 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4017 return SCTP_IERROR_AUTH_BAD_KEYID;
4020 /* Make sure that the length of the signature matches what
4021 * we expect.
4023 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4024 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4025 if (sig_len != hmac->hmac_len)
4026 return SCTP_IERROR_PROTO_VIOLATION;
4028 /* Now that we've done validation checks, we can compute and
4029 * verify the hmac. The steps involved are:
4030 * 1. Save the digest from the chunk.
4031 * 2. Zero out the digest in the chunk.
4032 * 3. Compute the new digest
4033 * 4. Compare saved and new digests.
4035 digest = auth_hdr->hmac;
4036 skb_pull(chunk->skb, sig_len);
4038 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4039 if (!save_digest)
4040 goto nomem;
4042 memset(digest, 0, sig_len);
4044 sctp_auth_calculate_hmac(asoc, chunk->skb,
4045 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4046 GFP_ATOMIC);
4048 /* Discard the packet if the digests do not match */
4049 if (memcmp(save_digest, digest, sig_len)) {
4050 kfree(save_digest);
4051 return SCTP_IERROR_BAD_SIG;
4054 kfree(save_digest);
4055 chunk->auth = 1;
4057 return SCTP_IERROR_NO_ERROR;
4058 nomem:
4059 return SCTP_IERROR_NOMEM;
4062 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4063 const struct sctp_endpoint *ep,
4064 const struct sctp_association *asoc,
4065 const sctp_subtype_t type,
4066 void *arg,
4067 sctp_cmd_seq_t *commands)
4069 struct sctp_authhdr *auth_hdr;
4070 struct sctp_chunk *chunk = arg;
4071 struct sctp_chunk *err_chunk;
4072 sctp_ierror_t error;
4074 /* Make sure that the peer has AUTH capable */
4075 if (!asoc->peer.auth_capable)
4076 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4078 if (!sctp_vtag_verify(chunk, asoc)) {
4079 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4080 SCTP_NULL());
4081 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4084 /* Make sure that the AUTH chunk has valid length. */
4085 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4086 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4087 commands);
4089 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4090 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
4091 switch (error) {
4092 case SCTP_IERROR_AUTH_BAD_HMAC:
4093 /* Generate the ERROR chunk and discard the rest
4094 * of the packet
4096 err_chunk = sctp_make_op_error(asoc, chunk,
4097 SCTP_ERROR_UNSUP_HMAC,
4098 &auth_hdr->hmac_id,
4099 sizeof(__u16), 0);
4100 if (err_chunk) {
4101 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4102 SCTP_CHUNK(err_chunk));
4104 /* Fall Through */
4105 case SCTP_IERROR_AUTH_BAD_KEYID:
4106 case SCTP_IERROR_BAD_SIG:
4107 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4109 case SCTP_IERROR_PROTO_VIOLATION:
4110 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4111 commands);
4113 case SCTP_IERROR_NOMEM:
4114 return SCTP_DISPOSITION_NOMEM;
4116 default: /* Prevent gcc warnings */
4117 break;
4120 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4121 struct sctp_ulpevent *ev;
4123 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4124 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4126 if (!ev)
4127 return -ENOMEM;
4129 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4130 SCTP_ULPEVENT(ev));
4133 return SCTP_DISPOSITION_CONSUME;
4137 * Process an unknown chunk.
4139 * Section: 3.2. Also, 2.1 in the implementor's guide.
4141 * Chunk Types are encoded such that the highest-order two bits specify
4142 * the action that must be taken if the processing endpoint does not
4143 * recognize the Chunk Type.
4145 * 00 - Stop processing this SCTP packet and discard it, do not process
4146 * any further chunks within it.
4148 * 01 - Stop processing this SCTP packet and discard it, do not process
4149 * any further chunks within it, and report the unrecognized
4150 * chunk in an 'Unrecognized Chunk Type'.
4152 * 10 - Skip this chunk and continue processing.
4154 * 11 - Skip this chunk and continue processing, but report in an ERROR
4155 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4157 * The return value is the disposition of the chunk.
4159 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4160 const struct sctp_endpoint *ep,
4161 const struct sctp_association *asoc,
4162 const sctp_subtype_t type,
4163 void *arg,
4164 sctp_cmd_seq_t *commands)
4166 struct sctp_chunk *unk_chunk = arg;
4167 struct sctp_chunk *err_chunk;
4168 sctp_chunkhdr_t *hdr;
4170 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4172 if (!sctp_vtag_verify(unk_chunk, asoc))
4173 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4175 /* Make sure that the chunk has a valid length.
4176 * Since we don't know the chunk type, we use a general
4177 * chunkhdr structure to make a comparison.
4179 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4180 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4181 commands);
4183 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4184 case SCTP_CID_ACTION_DISCARD:
4185 /* Discard the packet. */
4186 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4187 break;
4188 case SCTP_CID_ACTION_DISCARD_ERR:
4189 /* Generate an ERROR chunk as response. */
4190 hdr = unk_chunk->chunk_hdr;
4191 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4192 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4193 WORD_ROUND(ntohs(hdr->length)),
4195 if (err_chunk) {
4196 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4197 SCTP_CHUNK(err_chunk));
4200 /* Discard the packet. */
4201 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4202 return SCTP_DISPOSITION_CONSUME;
4203 break;
4204 case SCTP_CID_ACTION_SKIP:
4205 /* Skip the chunk. */
4206 return SCTP_DISPOSITION_DISCARD;
4207 break;
4208 case SCTP_CID_ACTION_SKIP_ERR:
4209 /* Generate an ERROR chunk as response. */
4210 hdr = unk_chunk->chunk_hdr;
4211 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4212 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4213 WORD_ROUND(ntohs(hdr->length)),
4215 if (err_chunk) {
4216 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4217 SCTP_CHUNK(err_chunk));
4219 /* Skip the chunk. */
4220 return SCTP_DISPOSITION_CONSUME;
4221 break;
4222 default:
4223 break;
4226 return SCTP_DISPOSITION_DISCARD;
4230 * Discard the chunk.
4232 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4233 * [Too numerous to mention...]
4234 * Verification Tag: No verification needed.
4235 * Inputs
4236 * (endpoint, asoc, chunk)
4238 * Outputs
4239 * (asoc, reply_msg, msg_up, timers, counters)
4241 * The return value is the disposition of the chunk.
4243 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4244 const struct sctp_endpoint *ep,
4245 const struct sctp_association *asoc,
4246 const sctp_subtype_t type,
4247 void *arg,
4248 sctp_cmd_seq_t *commands)
4250 struct sctp_chunk *chunk = arg;
4252 /* Make sure that the chunk has a valid length.
4253 * Since we don't know the chunk type, we use a general
4254 * chunkhdr structure to make a comparison.
4256 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4257 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4258 commands);
4260 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4262 return SCTP_DISPOSITION_DISCARD;
4266 * Discard the whole packet.
4268 * Section: 8.4 2)
4270 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4271 * silently discard the OOTB packet and take no further action.
4273 * Verification Tag: No verification necessary
4275 * Inputs
4276 * (endpoint, asoc, chunk)
4278 * Outputs
4279 * (asoc, reply_msg, msg_up, timers, counters)
4281 * The return value is the disposition of the chunk.
4283 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4284 const struct sctp_endpoint *ep,
4285 const struct sctp_association *asoc,
4286 const sctp_subtype_t type,
4287 void *arg,
4288 sctp_cmd_seq_t *commands)
4290 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4291 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4293 return SCTP_DISPOSITION_CONSUME;
4298 * The other end is violating protocol.
4300 * Section: Not specified
4301 * Verification Tag: Not specified
4302 * Inputs
4303 * (endpoint, asoc, chunk)
4305 * Outputs
4306 * (asoc, reply_msg, msg_up, timers, counters)
4308 * We simply tag the chunk as a violation. The state machine will log
4309 * the violation and continue.
4311 sctp_disposition_t sctp_sf_violation(struct net *net,
4312 const struct sctp_endpoint *ep,
4313 const struct sctp_association *asoc,
4314 const sctp_subtype_t type,
4315 void *arg,
4316 sctp_cmd_seq_t *commands)
4318 struct sctp_chunk *chunk = arg;
4320 /* Make sure that the chunk has a valid length. */
4321 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4322 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4323 commands);
4325 return SCTP_DISPOSITION_VIOLATION;
4329 * Common function to handle a protocol violation.
4331 static sctp_disposition_t sctp_sf_abort_violation(
4332 struct net *net,
4333 const struct sctp_endpoint *ep,
4334 const struct sctp_association *asoc,
4335 void *arg,
4336 sctp_cmd_seq_t *commands,
4337 const __u8 *payload,
4338 const size_t paylen)
4340 struct sctp_packet *packet = NULL;
4341 struct sctp_chunk *chunk = arg;
4342 struct sctp_chunk *abort = NULL;
4344 /* SCTP-AUTH, Section 6.3:
4345 * It should be noted that if the receiver wants to tear
4346 * down an association in an authenticated way only, the
4347 * handling of malformed packets should not result in
4348 * tearing down the association.
4350 * This means that if we only want to abort associations
4351 * in an authenticated way (i.e AUTH+ABORT), then we
4352 * can't destroy this association just because the packet
4353 * was malformed.
4355 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4356 goto discard;
4358 /* Make the abort chunk. */
4359 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4360 if (!abort)
4361 goto nomem;
4363 if (asoc) {
4364 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4365 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4366 !asoc->peer.i.init_tag) {
4367 sctp_initack_chunk_t *initack;
4369 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4370 if (!sctp_chunk_length_valid(chunk,
4371 sizeof(sctp_initack_chunk_t)))
4372 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4373 else {
4374 unsigned int inittag;
4376 inittag = ntohl(initack->init_hdr.init_tag);
4377 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4378 SCTP_U32(inittag));
4382 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4383 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4385 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4386 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4387 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4388 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4389 SCTP_ERROR(ECONNREFUSED));
4390 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4391 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4392 } else {
4393 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4394 SCTP_ERROR(ECONNABORTED));
4395 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4396 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4397 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4399 } else {
4400 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4402 if (!packet)
4403 goto nomem_pkt;
4405 if (sctp_test_T_bit(abort))
4406 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4408 abort->skb->sk = ep->base.sk;
4410 sctp_packet_append_chunk(packet, abort);
4412 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4413 SCTP_PACKET(packet));
4415 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4418 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4420 discard:
4421 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4422 return SCTP_DISPOSITION_ABORT;
4424 nomem_pkt:
4425 sctp_chunk_free(abort);
4426 nomem:
4427 return SCTP_DISPOSITION_NOMEM;
4431 * Handle a protocol violation when the chunk length is invalid.
4432 * "Invalid" length is identified as smaller than the minimal length a
4433 * given chunk can be. For example, a SACK chunk has invalid length
4434 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4436 * We inform the other end by sending an ABORT with a Protocol Violation
4437 * error code.
4439 * Section: Not specified
4440 * Verification Tag: Nothing to do
4441 * Inputs
4442 * (endpoint, asoc, chunk)
4444 * Outputs
4445 * (reply_msg, msg_up, counters)
4447 * Generate an ABORT chunk and terminate the association.
4449 static sctp_disposition_t sctp_sf_violation_chunklen(
4450 struct net *net,
4451 const struct sctp_endpoint *ep,
4452 const struct sctp_association *asoc,
4453 const sctp_subtype_t type,
4454 void *arg,
4455 sctp_cmd_seq_t *commands)
4457 static const char err_str[]="The following chunk had invalid length:";
4459 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4460 sizeof(err_str));
4464 * Handle a protocol violation when the parameter length is invalid.
4465 * If the length is smaller than the minimum length of a given parameter,
4466 * or accumulated length in multi parameters exceeds the end of the chunk,
4467 * the length is considered as invalid.
4469 static sctp_disposition_t sctp_sf_violation_paramlen(
4470 struct net *net,
4471 const struct sctp_endpoint *ep,
4472 const struct sctp_association *asoc,
4473 const sctp_subtype_t type,
4474 void *arg, void *ext,
4475 sctp_cmd_seq_t *commands)
4477 struct sctp_chunk *chunk = arg;
4478 struct sctp_paramhdr *param = ext;
4479 struct sctp_chunk *abort = NULL;
4481 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4482 goto discard;
4484 /* Make the abort chunk. */
4485 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4486 if (!abort)
4487 goto nomem;
4489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4490 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4492 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4493 SCTP_ERROR(ECONNABORTED));
4494 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4495 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4496 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4497 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4499 discard:
4500 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4501 return SCTP_DISPOSITION_ABORT;
4502 nomem:
4503 return SCTP_DISPOSITION_NOMEM;
4506 /* Handle a protocol violation when the peer trying to advance the
4507 * cumulative tsn ack to a point beyond the max tsn currently sent.
4509 * We inform the other end by sending an ABORT with a Protocol Violation
4510 * error code.
4512 static sctp_disposition_t sctp_sf_violation_ctsn(
4513 struct net *net,
4514 const struct sctp_endpoint *ep,
4515 const struct sctp_association *asoc,
4516 const sctp_subtype_t type,
4517 void *arg,
4518 sctp_cmd_seq_t *commands)
4520 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4522 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4523 sizeof(err_str));
4526 /* Handle protocol violation of an invalid chunk bundling. For example,
4527 * when we have an association and we receive bundled INIT-ACK, or
4528 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4529 * statement from the specs. Additionally, there might be an attacker
4530 * on the path and we may not want to continue this communication.
4532 static sctp_disposition_t sctp_sf_violation_chunk(
4533 struct net *net,
4534 const struct sctp_endpoint *ep,
4535 const struct sctp_association *asoc,
4536 const sctp_subtype_t type,
4537 void *arg,
4538 sctp_cmd_seq_t *commands)
4540 static const char err_str[]="The following chunk violates protocol:";
4542 if (!asoc)
4543 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4545 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4546 sizeof(err_str));
4548 /***************************************************************************
4549 * These are the state functions for handling primitive (Section 10) events.
4550 ***************************************************************************/
4552 * sctp_sf_do_prm_asoc
4554 * Section: 10.1 ULP-to-SCTP
4555 * B) Associate
4557 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4558 * outbound stream count)
4559 * -> association id [,destination transport addr list] [,outbound stream
4560 * count]
4562 * This primitive allows the upper layer to initiate an association to a
4563 * specific peer endpoint.
4565 * The peer endpoint shall be specified by one of the transport addresses
4566 * which defines the endpoint (see Section 1.4). If the local SCTP
4567 * instance has not been initialized, the ASSOCIATE is considered an
4568 * error.
4569 * [This is not relevant for the kernel implementation since we do all
4570 * initialization at boot time. It we hadn't initialized we wouldn't
4571 * get anywhere near this code.]
4573 * An association id, which is a local handle to the SCTP association,
4574 * will be returned on successful establishment of the association. If
4575 * SCTP is not able to open an SCTP association with the peer endpoint,
4576 * an error is returned.
4577 * [In the kernel implementation, the struct sctp_association needs to
4578 * be created BEFORE causing this primitive to run.]
4580 * Other association parameters may be returned, including the
4581 * complete destination transport addresses of the peer as well as the
4582 * outbound stream count of the local endpoint. One of the transport
4583 * address from the returned destination addresses will be selected by
4584 * the local endpoint as default primary path for sending SCTP packets
4585 * to this peer. The returned "destination transport addr list" can
4586 * be used by the ULP to change the default primary path or to force
4587 * sending a packet to a specific transport address. [All of this
4588 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4589 * function.]
4591 * Mandatory attributes:
4593 * o local SCTP instance name - obtained from the INITIALIZE operation.
4594 * [This is the argument asoc.]
4595 * o destination transport addr - specified as one of the transport
4596 * addresses of the peer endpoint with which the association is to be
4597 * established.
4598 * [This is asoc->peer.active_path.]
4599 * o outbound stream count - the number of outbound streams the ULP
4600 * would like to open towards this peer endpoint.
4601 * [BUG: This is not currently implemented.]
4602 * Optional attributes:
4604 * None.
4606 * The return value is a disposition.
4608 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4609 const struct sctp_endpoint *ep,
4610 const struct sctp_association *asoc,
4611 const sctp_subtype_t type,
4612 void *arg,
4613 sctp_cmd_seq_t *commands)
4615 struct sctp_chunk *repl;
4616 struct sctp_association* my_asoc;
4618 /* The comment below says that we enter COOKIE-WAIT AFTER
4619 * sending the INIT, but that doesn't actually work in our
4620 * implementation...
4622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4623 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4625 /* RFC 2960 5.1 Normal Establishment of an Association
4627 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4628 * must provide its Verification Tag (Tag_A) in the Initiate
4629 * Tag field. Tag_A SHOULD be a random number in the range of
4630 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4633 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4634 if (!repl)
4635 goto nomem;
4637 /* Choose transport for INIT. */
4638 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4639 SCTP_CHUNK(repl));
4641 /* Cast away the const modifier, as we want to just
4642 * rerun it through as a sideffect.
4644 my_asoc = (struct sctp_association *)asoc;
4645 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4647 /* After sending the INIT, "A" starts the T1-init timer and
4648 * enters the COOKIE-WAIT state.
4650 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4651 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4652 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4653 return SCTP_DISPOSITION_CONSUME;
4655 nomem:
4656 return SCTP_DISPOSITION_NOMEM;
4660 * Process the SEND primitive.
4662 * Section: 10.1 ULP-to-SCTP
4663 * E) Send
4665 * Format: SEND(association id, buffer address, byte count [,context]
4666 * [,stream id] [,life time] [,destination transport address]
4667 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4668 * -> result
4670 * This is the main method to send user data via SCTP.
4672 * Mandatory attributes:
4674 * o association id - local handle to the SCTP association
4676 * o buffer address - the location where the user message to be
4677 * transmitted is stored;
4679 * o byte count - The size of the user data in number of bytes;
4681 * Optional attributes:
4683 * o context - an optional 32 bit integer that will be carried in the
4684 * sending failure notification to the ULP if the transportation of
4685 * this User Message fails.
4687 * o stream id - to indicate which stream to send the data on. If not
4688 * specified, stream 0 will be used.
4690 * o life time - specifies the life time of the user data. The user data
4691 * will not be sent by SCTP after the life time expires. This
4692 * parameter can be used to avoid efforts to transmit stale
4693 * user messages. SCTP notifies the ULP if the data cannot be
4694 * initiated to transport (i.e. sent to the destination via SCTP's
4695 * send primitive) within the life time variable. However, the
4696 * user data will be transmitted if SCTP has attempted to transmit a
4697 * chunk before the life time expired.
4699 * o destination transport address - specified as one of the destination
4700 * transport addresses of the peer endpoint to which this packet
4701 * should be sent. Whenever possible, SCTP should use this destination
4702 * transport address for sending the packets, instead of the current
4703 * primary path.
4705 * o unorder flag - this flag, if present, indicates that the user
4706 * would like the data delivered in an unordered fashion to the peer
4707 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4708 * message).
4710 * o no-bundle flag - instructs SCTP not to bundle this user data with
4711 * other outbound DATA chunks. SCTP MAY still bundle even when
4712 * this flag is present, when faced with network congestion.
4714 * o payload protocol-id - A 32 bit unsigned integer that is to be
4715 * passed to the peer indicating the type of payload protocol data
4716 * being transmitted. This value is passed as opaque data by SCTP.
4718 * The return value is the disposition.
4720 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4721 const struct sctp_endpoint *ep,
4722 const struct sctp_association *asoc,
4723 const sctp_subtype_t type,
4724 void *arg,
4725 sctp_cmd_seq_t *commands)
4727 struct sctp_datamsg *msg = arg;
4729 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4730 return SCTP_DISPOSITION_CONSUME;
4734 * Process the SHUTDOWN primitive.
4736 * Section: 10.1:
4737 * C) Shutdown
4739 * Format: SHUTDOWN(association id)
4740 * -> result
4742 * Gracefully closes an association. Any locally queued user data
4743 * will be delivered to the peer. The association will be terminated only
4744 * after the peer acknowledges all the SCTP packets sent. A success code
4745 * will be returned on successful termination of the association. If
4746 * attempting to terminate the association results in a failure, an error
4747 * code shall be returned.
4749 * Mandatory attributes:
4751 * o association id - local handle to the SCTP association
4753 * Optional attributes:
4755 * None.
4757 * The return value is the disposition.
4759 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4760 struct net *net,
4761 const struct sctp_endpoint *ep,
4762 const struct sctp_association *asoc,
4763 const sctp_subtype_t type,
4764 void *arg,
4765 sctp_cmd_seq_t *commands)
4767 int disposition;
4769 /* From 9.2 Shutdown of an Association
4770 * Upon receipt of the SHUTDOWN primitive from its upper
4771 * layer, the endpoint enters SHUTDOWN-PENDING state and
4772 * remains there until all outstanding data has been
4773 * acknowledged by its peer. The endpoint accepts no new data
4774 * from its upper layer, but retransmits data to the far end
4775 * if necessary to fill gaps.
4777 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4778 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4780 disposition = SCTP_DISPOSITION_CONSUME;
4781 if (sctp_outq_is_empty(&asoc->outqueue)) {
4782 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4783 arg, commands);
4785 return disposition;
4789 * Process the ABORT primitive.
4791 * Section: 10.1:
4792 * C) Abort
4794 * Format: Abort(association id [, cause code])
4795 * -> result
4797 * Ungracefully closes an association. Any locally queued user data
4798 * will be discarded and an ABORT chunk is sent to the peer. A success code
4799 * will be returned on successful abortion of the association. If
4800 * attempting to abort the association results in a failure, an error
4801 * code shall be returned.
4803 * Mandatory attributes:
4805 * o association id - local handle to the SCTP association
4807 * Optional attributes:
4809 * o cause code - reason of the abort to be passed to the peer
4811 * None.
4813 * The return value is the disposition.
4815 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4816 struct net *net,
4817 const struct sctp_endpoint *ep,
4818 const struct sctp_association *asoc,
4819 const sctp_subtype_t type,
4820 void *arg,
4821 sctp_cmd_seq_t *commands)
4823 /* From 9.1 Abort of an Association
4824 * Upon receipt of the ABORT primitive from its upper
4825 * layer, the endpoint enters CLOSED state and
4826 * discard all outstanding data has been
4827 * acknowledged by its peer. The endpoint accepts no new data
4828 * from its upper layer, but retransmits data to the far end
4829 * if necessary to fill gaps.
4831 struct sctp_chunk *abort = arg;
4832 sctp_disposition_t retval;
4834 retval = SCTP_DISPOSITION_CONSUME;
4836 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4838 /* Even if we can't send the ABORT due to low memory delete the
4839 * TCB. This is a departure from our typical NOMEM handling.
4842 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4843 SCTP_ERROR(ECONNABORTED));
4844 /* Delete the established association. */
4845 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4846 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4848 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4849 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4851 return retval;
4854 /* We tried an illegal operation on an association which is closed. */
4855 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4856 const struct sctp_endpoint *ep,
4857 const struct sctp_association *asoc,
4858 const sctp_subtype_t type,
4859 void *arg,
4860 sctp_cmd_seq_t *commands)
4862 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4863 return SCTP_DISPOSITION_CONSUME;
4866 /* We tried an illegal operation on an association which is shutting
4867 * down.
4869 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4870 const struct sctp_endpoint *ep,
4871 const struct sctp_association *asoc,
4872 const sctp_subtype_t type,
4873 void *arg,
4874 sctp_cmd_seq_t *commands)
4876 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4877 SCTP_ERROR(-ESHUTDOWN));
4878 return SCTP_DISPOSITION_CONSUME;
4882 * sctp_cookie_wait_prm_shutdown
4884 * Section: 4 Note: 2
4885 * Verification Tag:
4886 * Inputs
4887 * (endpoint, asoc)
4889 * The RFC does not explicitly address this issue, but is the route through the
4890 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4892 * Outputs
4893 * (timers)
4895 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4896 struct net *net,
4897 const struct sctp_endpoint *ep,
4898 const struct sctp_association *asoc,
4899 const sctp_subtype_t type,
4900 void *arg,
4901 sctp_cmd_seq_t *commands)
4903 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4904 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4906 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4907 SCTP_STATE(SCTP_STATE_CLOSED));
4909 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4911 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4913 return SCTP_DISPOSITION_DELETE_TCB;
4917 * sctp_cookie_echoed_prm_shutdown
4919 * Section: 4 Note: 2
4920 * Verification Tag:
4921 * Inputs
4922 * (endpoint, asoc)
4924 * The RFC does not explcitly address this issue, but is the route through the
4925 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4927 * Outputs
4928 * (timers)
4930 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4931 struct net *net,
4932 const struct sctp_endpoint *ep,
4933 const struct sctp_association *asoc,
4934 const sctp_subtype_t type,
4935 void *arg, sctp_cmd_seq_t *commands)
4937 /* There is a single T1 timer, so we should be able to use
4938 * common function with the COOKIE-WAIT state.
4940 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4944 * sctp_sf_cookie_wait_prm_abort
4946 * Section: 4 Note: 2
4947 * Verification Tag:
4948 * Inputs
4949 * (endpoint, asoc)
4951 * The RFC does not explicitly address this issue, but is the route through the
4952 * state table when someone issues an abort while in COOKIE_WAIT state.
4954 * Outputs
4955 * (timers)
4957 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4958 struct net *net,
4959 const struct sctp_endpoint *ep,
4960 const struct sctp_association *asoc,
4961 const sctp_subtype_t type,
4962 void *arg,
4963 sctp_cmd_seq_t *commands)
4965 struct sctp_chunk *abort = arg;
4966 sctp_disposition_t retval;
4968 /* Stop T1-init timer */
4969 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4970 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4971 retval = SCTP_DISPOSITION_CONSUME;
4973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4975 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4976 SCTP_STATE(SCTP_STATE_CLOSED));
4978 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4980 /* Even if we can't send the ABORT due to low memory delete the
4981 * TCB. This is a departure from our typical NOMEM handling.
4984 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4985 SCTP_ERROR(ECONNREFUSED));
4986 /* Delete the established association. */
4987 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4988 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4990 return retval;
4994 * sctp_sf_cookie_echoed_prm_abort
4996 * Section: 4 Note: 3
4997 * Verification Tag:
4998 * Inputs
4999 * (endpoint, asoc)
5001 * The RFC does not explcitly address this issue, but is the route through the
5002 * state table when someone issues an abort while in COOKIE_ECHOED state.
5004 * Outputs
5005 * (timers)
5007 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5008 struct net *net,
5009 const struct sctp_endpoint *ep,
5010 const struct sctp_association *asoc,
5011 const sctp_subtype_t type,
5012 void *arg,
5013 sctp_cmd_seq_t *commands)
5015 /* There is a single T1 timer, so we should be able to use
5016 * common function with the COOKIE-WAIT state.
5018 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5022 * sctp_sf_shutdown_pending_prm_abort
5024 * Inputs
5025 * (endpoint, asoc)
5027 * The RFC does not explicitly address this issue, but is the route through the
5028 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5030 * Outputs
5031 * (timers)
5033 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5034 struct net *net,
5035 const struct sctp_endpoint *ep,
5036 const struct sctp_association *asoc,
5037 const sctp_subtype_t type,
5038 void *arg,
5039 sctp_cmd_seq_t *commands)
5041 /* Stop the T5-shutdown guard timer. */
5042 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5043 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5045 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5049 * sctp_sf_shutdown_sent_prm_abort
5051 * Inputs
5052 * (endpoint, asoc)
5054 * The RFC does not explicitly address this issue, but is the route through the
5055 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5057 * Outputs
5058 * (timers)
5060 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5061 struct net *net,
5062 const struct sctp_endpoint *ep,
5063 const struct sctp_association *asoc,
5064 const sctp_subtype_t type,
5065 void *arg,
5066 sctp_cmd_seq_t *commands)
5068 /* Stop the T2-shutdown timer. */
5069 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5070 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5072 /* Stop the T5-shutdown guard timer. */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5076 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5080 * sctp_sf_cookie_echoed_prm_abort
5082 * Inputs
5083 * (endpoint, asoc)
5085 * The RFC does not explcitly address this issue, but is the route through the
5086 * state table when someone issues an abort while in COOKIE_ECHOED state.
5088 * Outputs
5089 * (timers)
5091 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5092 struct net *net,
5093 const struct sctp_endpoint *ep,
5094 const struct sctp_association *asoc,
5095 const sctp_subtype_t type,
5096 void *arg,
5097 sctp_cmd_seq_t *commands)
5099 /* The same T2 timer, so we should be able to use
5100 * common function with the SHUTDOWN-SENT state.
5102 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5106 * Process the REQUESTHEARTBEAT primitive
5108 * 10.1 ULP-to-SCTP
5109 * J) Request Heartbeat
5111 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5113 * -> result
5115 * Instructs the local endpoint to perform a HeartBeat on the specified
5116 * destination transport address of the given association. The returned
5117 * result should indicate whether the transmission of the HEARTBEAT
5118 * chunk to the destination address is successful.
5120 * Mandatory attributes:
5122 * o association id - local handle to the SCTP association
5124 * o destination transport address - the transport address of the
5125 * association on which a heartbeat should be issued.
5127 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5128 struct net *net,
5129 const struct sctp_endpoint *ep,
5130 const struct sctp_association *asoc,
5131 const sctp_subtype_t type,
5132 void *arg,
5133 sctp_cmd_seq_t *commands)
5135 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5136 (struct sctp_transport *)arg, commands))
5137 return SCTP_DISPOSITION_NOMEM;
5140 * RFC 2960 (bis), section 8.3
5142 * D) Request an on-demand HEARTBEAT on a specific destination
5143 * transport address of a given association.
5145 * The endpoint should increment the respective error counter of
5146 * the destination transport address each time a HEARTBEAT is sent
5147 * to that address and not acknowledged within one RTO.
5150 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5151 SCTP_TRANSPORT(arg));
5152 return SCTP_DISPOSITION_CONSUME;
5156 * ADDIP Section 4.1 ASCONF Chunk Procedures
5157 * When an endpoint has an ASCONF signaled change to be sent to the
5158 * remote endpoint it should do A1 to A9
5160 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5161 const struct sctp_endpoint *ep,
5162 const struct sctp_association *asoc,
5163 const sctp_subtype_t type,
5164 void *arg,
5165 sctp_cmd_seq_t *commands)
5167 struct sctp_chunk *chunk = arg;
5169 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5170 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5171 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5172 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5173 return SCTP_DISPOSITION_CONSUME;
5177 * Ignore the primitive event
5179 * The return value is the disposition of the primitive.
5181 sctp_disposition_t sctp_sf_ignore_primitive(
5182 struct net *net,
5183 const struct sctp_endpoint *ep,
5184 const struct sctp_association *asoc,
5185 const sctp_subtype_t type,
5186 void *arg,
5187 sctp_cmd_seq_t *commands)
5189 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5190 type.primitive);
5192 return SCTP_DISPOSITION_DISCARD;
5195 /***************************************************************************
5196 * These are the state functions for the OTHER events.
5197 ***************************************************************************/
5200 * When the SCTP stack has no more user data to send or retransmit, this
5201 * notification is given to the user. Also, at the time when a user app
5202 * subscribes to this event, if there is no data to be sent or
5203 * retransmit, the stack will immediately send up this notification.
5205 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5206 struct net *net,
5207 const struct sctp_endpoint *ep,
5208 const struct sctp_association *asoc,
5209 const sctp_subtype_t type,
5210 void *arg,
5211 sctp_cmd_seq_t *commands)
5213 struct sctp_ulpevent *event;
5215 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5216 if (!event)
5217 return SCTP_DISPOSITION_NOMEM;
5219 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5221 return SCTP_DISPOSITION_CONSUME;
5225 * Start the shutdown negotiation.
5227 * From Section 9.2:
5228 * Once all its outstanding data has been acknowledged, the endpoint
5229 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5230 * TSN Ack field the last sequential TSN it has received from the peer.
5231 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5232 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5233 * with the updated last sequential TSN received from its peer.
5235 * The return value is the disposition.
5237 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5238 struct net *net,
5239 const struct sctp_endpoint *ep,
5240 const struct sctp_association *asoc,
5241 const sctp_subtype_t type,
5242 void *arg,
5243 sctp_cmd_seq_t *commands)
5245 struct sctp_chunk *reply;
5247 /* Once all its outstanding data has been acknowledged, the
5248 * endpoint shall send a SHUTDOWN chunk to its peer including
5249 * in the Cumulative TSN Ack field the last sequential TSN it
5250 * has received from the peer.
5252 reply = sctp_make_shutdown(asoc, NULL);
5253 if (!reply)
5254 goto nomem;
5256 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5257 * T2-shutdown timer.
5259 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5261 /* It shall then start the T2-shutdown timer */
5262 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5263 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5265 /* RFC 4960 Section 9.2
5266 * The sender of the SHUTDOWN MAY also start an overall guard timer
5267 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5269 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5270 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5272 if (asoc->autoclose)
5273 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5274 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5276 /* and enter the SHUTDOWN-SENT state. */
5277 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5278 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5280 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5282 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5283 * or SHUTDOWN-ACK.
5285 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5287 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5289 return SCTP_DISPOSITION_CONSUME;
5291 nomem:
5292 return SCTP_DISPOSITION_NOMEM;
5296 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5298 * From Section 9.2:
5300 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5301 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5302 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5303 * endpoint must re-send the SHUTDOWN ACK.
5305 * The return value is the disposition.
5307 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5308 struct net *net,
5309 const struct sctp_endpoint *ep,
5310 const struct sctp_association *asoc,
5311 const sctp_subtype_t type,
5312 void *arg,
5313 sctp_cmd_seq_t *commands)
5315 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5316 struct sctp_chunk *reply;
5318 /* There are 2 ways of getting here:
5319 * 1) called in response to a SHUTDOWN chunk
5320 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5322 * For the case (2), the arg parameter is set to NULL. We need
5323 * to check that we have a chunk before accessing it's fields.
5325 if (chunk) {
5326 if (!sctp_vtag_verify(chunk, asoc))
5327 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5329 /* Make sure that the SHUTDOWN chunk has a valid length. */
5330 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5331 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5332 commands);
5335 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5336 * shall send a SHUTDOWN ACK ...
5338 reply = sctp_make_shutdown_ack(asoc, chunk);
5339 if (!reply)
5340 goto nomem;
5342 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5343 * the T2-shutdown timer.
5345 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5347 /* and start/restart a T2-shutdown timer of its own, */
5348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5349 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5351 if (asoc->autoclose)
5352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5353 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5355 /* Enter the SHUTDOWN-ACK-SENT state. */
5356 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5357 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5359 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5361 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5362 * or SHUTDOWN-ACK.
5364 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5366 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5368 return SCTP_DISPOSITION_CONSUME;
5370 nomem:
5371 return SCTP_DISPOSITION_NOMEM;
5375 * Ignore the event defined as other
5377 * The return value is the disposition of the event.
5379 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5380 const struct sctp_endpoint *ep,
5381 const struct sctp_association *asoc,
5382 const sctp_subtype_t type,
5383 void *arg,
5384 sctp_cmd_seq_t *commands)
5386 pr_debug("%s: the event other type:%d is ignored\n",
5387 __func__, type.other);
5389 return SCTP_DISPOSITION_DISCARD;
5392 /************************************************************
5393 * These are the state functions for handling timeout events.
5394 ************************************************************/
5397 * RTX Timeout
5399 * Section: 6.3.3 Handle T3-rtx Expiration
5401 * Whenever the retransmission timer T3-rtx expires for a destination
5402 * address, do the following:
5403 * [See below]
5405 * The return value is the disposition of the chunk.
5407 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5408 const struct sctp_endpoint *ep,
5409 const struct sctp_association *asoc,
5410 const sctp_subtype_t type,
5411 void *arg,
5412 sctp_cmd_seq_t *commands)
5414 struct sctp_transport *transport = arg;
5416 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5418 if (asoc->overall_error_count >= asoc->max_retrans) {
5419 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5421 * We are here likely because the receiver had its rwnd
5422 * closed for a while and we have not been able to
5423 * transmit the locally queued data within the maximum
5424 * retransmission attempts limit. Start the T5
5425 * shutdown guard timer to give the receiver one last
5426 * chance and some additional time to recover before
5427 * aborting.
5429 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5430 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5431 } else {
5432 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5433 SCTP_ERROR(ETIMEDOUT));
5434 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5435 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5436 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5437 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5438 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5439 return SCTP_DISPOSITION_DELETE_TCB;
5443 /* E1) For the destination address for which the timer
5444 * expires, adjust its ssthresh with rules defined in Section
5445 * 7.2.3 and set the cwnd <- MTU.
5448 /* E2) For the destination address for which the timer
5449 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5450 * maximum value discussed in rule C7 above (RTO.max) may be
5451 * used to provide an upper bound to this doubling operation.
5454 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5455 * outstanding DATA chunks for the address for which the
5456 * T3-rtx has expired will fit into a single packet, subject
5457 * to the MTU constraint for the path corresponding to the
5458 * destination transport address to which the retransmission
5459 * is being sent (this may be different from the address for
5460 * which the timer expires [see Section 6.4]). Call this
5461 * value K. Bundle and retransmit those K DATA chunks in a
5462 * single packet to the destination endpoint.
5464 * Note: Any DATA chunks that were sent to the address for
5465 * which the T3-rtx timer expired but did not fit in one MTU
5466 * (rule E3 above), should be marked for retransmission and
5467 * sent as soon as cwnd allows (normally when a SACK arrives).
5470 /* Do some failure management (Section 8.2). */
5471 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5473 /* NB: Rules E4 and F1 are implicit in R1. */
5474 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5476 return SCTP_DISPOSITION_CONSUME;
5480 * Generate delayed SACK on timeout
5482 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5484 * The guidelines on delayed acknowledgement algorithm specified in
5485 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5486 * acknowledgement SHOULD be generated for at least every second packet
5487 * (not every second DATA chunk) received, and SHOULD be generated
5488 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5489 * some situations it may be beneficial for an SCTP transmitter to be
5490 * more conservative than the algorithms detailed in this document
5491 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5492 * the following algorithms allow.
5494 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5495 const struct sctp_endpoint *ep,
5496 const struct sctp_association *asoc,
5497 const sctp_subtype_t type,
5498 void *arg,
5499 sctp_cmd_seq_t *commands)
5501 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5502 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5503 return SCTP_DISPOSITION_CONSUME;
5507 * sctp_sf_t1_init_timer_expire
5509 * Section: 4 Note: 2
5510 * Verification Tag:
5511 * Inputs
5512 * (endpoint, asoc)
5514 * RFC 2960 Section 4 Notes
5515 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5516 * and re-start the T1-init timer without changing state. This MUST
5517 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5518 * endpoint MUST abort the initialization process and report the
5519 * error to SCTP user.
5521 * Outputs
5522 * (timers, events)
5525 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5526 const struct sctp_endpoint *ep,
5527 const struct sctp_association *asoc,
5528 const sctp_subtype_t type,
5529 void *arg,
5530 sctp_cmd_seq_t *commands)
5532 struct sctp_chunk *repl = NULL;
5533 struct sctp_bind_addr *bp;
5534 int attempts = asoc->init_err_counter + 1;
5536 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5538 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5540 if (attempts <= asoc->max_init_attempts) {
5541 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5542 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5543 if (!repl)
5544 return SCTP_DISPOSITION_NOMEM;
5546 /* Choose transport for INIT. */
5547 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5548 SCTP_CHUNK(repl));
5550 /* Issue a sideeffect to do the needed accounting. */
5551 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5552 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5554 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5555 } else {
5556 pr_debug("%s: giving up on INIT, attempts:%d "
5557 "max_init_attempts:%d\n", __func__, attempts,
5558 asoc->max_init_attempts);
5560 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5561 SCTP_ERROR(ETIMEDOUT));
5562 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5563 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5564 return SCTP_DISPOSITION_DELETE_TCB;
5567 return SCTP_DISPOSITION_CONSUME;
5571 * sctp_sf_t1_cookie_timer_expire
5573 * Section: 4 Note: 2
5574 * Verification Tag:
5575 * Inputs
5576 * (endpoint, asoc)
5578 * RFC 2960 Section 4 Notes
5579 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5580 * COOKIE ECHO and re-start the T1-cookie timer without changing
5581 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5582 * After that, the endpoint MUST abort the initialization process and
5583 * report the error to SCTP user.
5585 * Outputs
5586 * (timers, events)
5589 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5590 const struct sctp_endpoint *ep,
5591 const struct sctp_association *asoc,
5592 const sctp_subtype_t type,
5593 void *arg,
5594 sctp_cmd_seq_t *commands)
5596 struct sctp_chunk *repl = NULL;
5597 int attempts = asoc->init_err_counter + 1;
5599 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5601 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5603 if (attempts <= asoc->max_init_attempts) {
5604 repl = sctp_make_cookie_echo(asoc, NULL);
5605 if (!repl)
5606 return SCTP_DISPOSITION_NOMEM;
5608 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5609 SCTP_CHUNK(repl));
5610 /* Issue a sideeffect to do the needed accounting. */
5611 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5612 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5614 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5615 } else {
5616 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5617 SCTP_ERROR(ETIMEDOUT));
5618 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5619 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5620 return SCTP_DISPOSITION_DELETE_TCB;
5623 return SCTP_DISPOSITION_CONSUME;
5626 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5627 * with the updated last sequential TSN received from its peer.
5629 * An endpoint should limit the number of retransmissions of the
5630 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5631 * If this threshold is exceeded the endpoint should destroy the TCB and
5632 * MUST report the peer endpoint unreachable to the upper layer (and
5633 * thus the association enters the CLOSED state). The reception of any
5634 * packet from its peer (i.e. as the peer sends all of its queued DATA
5635 * chunks) should clear the endpoint's retransmission count and restart
5636 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5637 * all of its queued DATA chunks that have not yet been sent.
5639 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5640 const struct sctp_endpoint *ep,
5641 const struct sctp_association *asoc,
5642 const sctp_subtype_t type,
5643 void *arg,
5644 sctp_cmd_seq_t *commands)
5646 struct sctp_chunk *reply = NULL;
5648 pr_debug("%s: timer T2 expired\n", __func__);
5650 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5652 ((struct sctp_association *)asoc)->shutdown_retries++;
5654 if (asoc->overall_error_count >= asoc->max_retrans) {
5655 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5656 SCTP_ERROR(ETIMEDOUT));
5657 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5658 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5659 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5660 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5661 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5662 return SCTP_DISPOSITION_DELETE_TCB;
5665 switch (asoc->state) {
5666 case SCTP_STATE_SHUTDOWN_SENT:
5667 reply = sctp_make_shutdown(asoc, NULL);
5668 break;
5670 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5671 reply = sctp_make_shutdown_ack(asoc, NULL);
5672 break;
5674 default:
5675 BUG();
5676 break;
5679 if (!reply)
5680 goto nomem;
5682 /* Do some failure management (Section 8.2).
5683 * If we remove the transport an SHUTDOWN was last sent to, don't
5684 * do failure management.
5686 if (asoc->shutdown_last_sent_to)
5687 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5688 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5690 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5691 * the T2-shutdown timer.
5693 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5695 /* Restart the T2-shutdown timer. */
5696 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5697 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5698 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5699 return SCTP_DISPOSITION_CONSUME;
5701 nomem:
5702 return SCTP_DISPOSITION_NOMEM;
5706 * ADDIP Section 4.1 ASCONF CHunk Procedures
5707 * If the T4 RTO timer expires the endpoint should do B1 to B5
5709 sctp_disposition_t sctp_sf_t4_timer_expire(
5710 struct net *net,
5711 const struct sctp_endpoint *ep,
5712 const struct sctp_association *asoc,
5713 const sctp_subtype_t type,
5714 void *arg,
5715 sctp_cmd_seq_t *commands)
5717 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5718 struct sctp_transport *transport = chunk->transport;
5720 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5722 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5723 * detection on the appropriate destination address as defined in
5724 * RFC2960 [5] section 8.1 and 8.2.
5726 if (transport)
5727 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5728 SCTP_TRANSPORT(transport));
5730 /* Reconfig T4 timer and transport. */
5731 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5733 /* ADDIP 4.1 B2) Increment the association error counters and perform
5734 * endpoint failure detection on the association as defined in
5735 * RFC2960 [5] section 8.1 and 8.2.
5736 * association error counter is incremented in SCTP_CMD_STRIKE.
5738 if (asoc->overall_error_count >= asoc->max_retrans) {
5739 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5740 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5741 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5742 SCTP_ERROR(ETIMEDOUT));
5743 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5744 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5745 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5746 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5747 return SCTP_DISPOSITION_ABORT;
5750 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5751 * the ASCONF chunk was sent by doubling the RTO timer value.
5752 * This is done in SCTP_CMD_STRIKE.
5755 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5756 * choose an alternate destination address (please refer to RFC2960
5757 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5758 * chunk, it MUST be the same (including its serial number) as the last
5759 * ASCONF sent.
5761 sctp_chunk_hold(asoc->addip_last_asconf);
5762 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5763 SCTP_CHUNK(asoc->addip_last_asconf));
5765 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5766 * destination is selected, then the RTO used will be that of the new
5767 * destination address.
5769 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5770 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5772 return SCTP_DISPOSITION_CONSUME;
5775 /* sctpimpguide-05 Section 2.12.2
5776 * The sender of the SHUTDOWN MAY also start an overall guard timer
5777 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5778 * At the expiration of this timer the sender SHOULD abort the association
5779 * by sending an ABORT chunk.
5781 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5782 const struct sctp_endpoint *ep,
5783 const struct sctp_association *asoc,
5784 const sctp_subtype_t type,
5785 void *arg,
5786 sctp_cmd_seq_t *commands)
5788 struct sctp_chunk *reply = NULL;
5790 pr_debug("%s: timer T5 expired\n", __func__);
5792 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5794 reply = sctp_make_abort(asoc, NULL, 0);
5795 if (!reply)
5796 goto nomem;
5798 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5799 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5800 SCTP_ERROR(ETIMEDOUT));
5801 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5802 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5804 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5805 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5807 return SCTP_DISPOSITION_DELETE_TCB;
5808 nomem:
5809 return SCTP_DISPOSITION_NOMEM;
5812 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5813 * the association is automatically closed by starting the shutdown process.
5814 * The work that needs to be done is same as when SHUTDOWN is initiated by
5815 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5817 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5818 struct net *net,
5819 const struct sctp_endpoint *ep,
5820 const struct sctp_association *asoc,
5821 const sctp_subtype_t type,
5822 void *arg,
5823 sctp_cmd_seq_t *commands)
5825 int disposition;
5827 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5829 /* From 9.2 Shutdown of an Association
5830 * Upon receipt of the SHUTDOWN primitive from its upper
5831 * layer, the endpoint enters SHUTDOWN-PENDING state and
5832 * remains there until all outstanding data has been
5833 * acknowledged by its peer. The endpoint accepts no new data
5834 * from its upper layer, but retransmits data to the far end
5835 * if necessary to fill gaps.
5837 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5838 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5840 disposition = SCTP_DISPOSITION_CONSUME;
5841 if (sctp_outq_is_empty(&asoc->outqueue)) {
5842 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5843 arg, commands);
5845 return disposition;
5848 /*****************************************************************************
5849 * These are sa state functions which could apply to all types of events.
5850 ****************************************************************************/
5853 * This table entry is not implemented.
5855 * Inputs
5856 * (endpoint, asoc, chunk)
5858 * The return value is the disposition of the chunk.
5860 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5861 const struct sctp_endpoint *ep,
5862 const struct sctp_association *asoc,
5863 const sctp_subtype_t type,
5864 void *arg,
5865 sctp_cmd_seq_t *commands)
5867 return SCTP_DISPOSITION_NOT_IMPL;
5871 * This table entry represents a bug.
5873 * Inputs
5874 * (endpoint, asoc, chunk)
5876 * The return value is the disposition of the chunk.
5878 sctp_disposition_t sctp_sf_bug(struct net *net,
5879 const struct sctp_endpoint *ep,
5880 const struct sctp_association *asoc,
5881 const sctp_subtype_t type,
5882 void *arg,
5883 sctp_cmd_seq_t *commands)
5885 return SCTP_DISPOSITION_BUG;
5889 * This table entry represents the firing of a timer in the wrong state.
5890 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5891 * when the association is in the wrong state. This event should
5892 * be ignored, so as to prevent any rearming of the timer.
5894 * Inputs
5895 * (endpoint, asoc, chunk)
5897 * The return value is the disposition of the chunk.
5899 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5900 const struct sctp_endpoint *ep,
5901 const struct sctp_association *asoc,
5902 const sctp_subtype_t type,
5903 void *arg,
5904 sctp_cmd_seq_t *commands)
5906 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5908 return SCTP_DISPOSITION_CONSUME;
5911 /********************************************************************
5912 * 2nd Level Abstractions
5913 ********************************************************************/
5915 /* Pull the SACK chunk based on the SACK header. */
5916 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5918 struct sctp_sackhdr *sack;
5919 unsigned int len;
5920 __u16 num_blocks;
5921 __u16 num_dup_tsns;
5923 /* Protect ourselves from reading too far into
5924 * the skb from a bogus sender.
5926 sack = (struct sctp_sackhdr *) chunk->skb->data;
5928 num_blocks = ntohs(sack->num_gap_ack_blocks);
5929 num_dup_tsns = ntohs(sack->num_dup_tsns);
5930 len = sizeof(struct sctp_sackhdr);
5931 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5932 if (len > chunk->skb->len)
5933 return NULL;
5935 skb_pull(chunk->skb, len);
5937 return sack;
5940 /* Create an ABORT packet to be sent as a response, with the specified
5941 * error causes.
5943 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5944 const struct sctp_endpoint *ep,
5945 const struct sctp_association *asoc,
5946 struct sctp_chunk *chunk,
5947 const void *payload,
5948 size_t paylen)
5950 struct sctp_packet *packet;
5951 struct sctp_chunk *abort;
5953 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5955 if (packet) {
5956 /* Make an ABORT.
5957 * The T bit will be set if the asoc is NULL.
5959 abort = sctp_make_abort(asoc, chunk, paylen);
5960 if (!abort) {
5961 sctp_ootb_pkt_free(packet);
5962 return NULL;
5965 /* Reflect vtag if T-Bit is set */
5966 if (sctp_test_T_bit(abort))
5967 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5969 /* Add specified error causes, i.e., payload, to the
5970 * end of the chunk.
5972 sctp_addto_chunk(abort, paylen, payload);
5974 /* Set the skb to the belonging sock for accounting. */
5975 abort->skb->sk = ep->base.sk;
5977 sctp_packet_append_chunk(packet, abort);
5981 return packet;
5984 /* Allocate a packet for responding in the OOTB conditions. */
5985 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5986 const struct sctp_association *asoc,
5987 const struct sctp_chunk *chunk)
5989 struct sctp_packet *packet;
5990 struct sctp_transport *transport;
5991 __u16 sport;
5992 __u16 dport;
5993 __u32 vtag;
5995 /* Get the source and destination port from the inbound packet. */
5996 sport = ntohs(chunk->sctp_hdr->dest);
5997 dport = ntohs(chunk->sctp_hdr->source);
5999 /* The V-tag is going to be the same as the inbound packet if no
6000 * association exists, otherwise, use the peer's vtag.
6002 if (asoc) {
6003 /* Special case the INIT-ACK as there is no peer's vtag
6004 * yet.
6006 switch(chunk->chunk_hdr->type) {
6007 case SCTP_CID_INIT_ACK:
6009 sctp_initack_chunk_t *initack;
6011 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6012 vtag = ntohl(initack->init_hdr.init_tag);
6013 break;
6015 default:
6016 vtag = asoc->peer.i.init_tag;
6017 break;
6019 } else {
6020 /* Special case the INIT and stale COOKIE_ECHO as there is no
6021 * vtag yet.
6023 switch(chunk->chunk_hdr->type) {
6024 case SCTP_CID_INIT:
6026 sctp_init_chunk_t *init;
6028 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6029 vtag = ntohl(init->init_hdr.init_tag);
6030 break;
6032 default:
6033 vtag = ntohl(chunk->sctp_hdr->vtag);
6034 break;
6038 /* Make a transport for the bucket, Eliza... */
6039 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6040 if (!transport)
6041 goto nomem;
6043 /* Cache a route for the transport with the chunk's destination as
6044 * the source address.
6046 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6047 sctp_sk(net->sctp.ctl_sock));
6049 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6050 packet = sctp_packet_config(packet, vtag, 0);
6052 return packet;
6054 nomem:
6055 return NULL;
6058 /* Free the packet allocated earlier for responding in the OOTB condition. */
6059 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6061 sctp_transport_free(packet->transport);
6064 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6065 static void sctp_send_stale_cookie_err(struct net *net,
6066 const struct sctp_endpoint *ep,
6067 const struct sctp_association *asoc,
6068 const struct sctp_chunk *chunk,
6069 sctp_cmd_seq_t *commands,
6070 struct sctp_chunk *err_chunk)
6072 struct sctp_packet *packet;
6074 if (err_chunk) {
6075 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6076 if (packet) {
6077 struct sctp_signed_cookie *cookie;
6079 /* Override the OOTB vtag from the cookie. */
6080 cookie = chunk->subh.cookie_hdr;
6081 packet->vtag = cookie->c.peer_vtag;
6083 /* Set the skb to the belonging sock for accounting. */
6084 err_chunk->skb->sk = ep->base.sk;
6085 sctp_packet_append_chunk(packet, err_chunk);
6086 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6087 SCTP_PACKET(packet));
6088 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6089 } else
6090 sctp_chunk_free (err_chunk);
6095 /* Process a data chunk */
6096 static int sctp_eat_data(const struct sctp_association *asoc,
6097 struct sctp_chunk *chunk,
6098 sctp_cmd_seq_t *commands)
6100 sctp_datahdr_t *data_hdr;
6101 struct sctp_chunk *err;
6102 size_t datalen;
6103 sctp_verb_t deliver;
6104 int tmp;
6105 __u32 tsn;
6106 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6107 struct sock *sk = asoc->base.sk;
6108 struct net *net = sock_net(sk);
6109 u16 ssn;
6110 u16 sid;
6111 u8 ordered = 0;
6113 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6114 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6116 tsn = ntohl(data_hdr->tsn);
6117 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6119 /* ASSERT: Now skb->data is really the user data. */
6121 /* Process ECN based congestion.
6123 * Since the chunk structure is reused for all chunks within
6124 * a packet, we use ecn_ce_done to track if we've already
6125 * done CE processing for this packet.
6127 * We need to do ECN processing even if we plan to discard the
6128 * chunk later.
6131 if (!chunk->ecn_ce_done) {
6132 struct sctp_af *af;
6133 chunk->ecn_ce_done = 1;
6135 af = sctp_get_af_specific(
6136 ipver2af(ip_hdr(chunk->skb)->version));
6138 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6139 /* Do real work as sideffect. */
6140 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6141 SCTP_U32(tsn));
6145 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6146 if (tmp < 0) {
6147 /* The TSN is too high--silently discard the chunk and
6148 * count on it getting retransmitted later.
6150 if (chunk->asoc)
6151 chunk->asoc->stats.outofseqtsns++;
6152 return SCTP_IERROR_HIGH_TSN;
6153 } else if (tmp > 0) {
6154 /* This is a duplicate. Record it. */
6155 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6156 return SCTP_IERROR_DUP_TSN;
6159 /* This is a new TSN. */
6161 /* Discard if there is no room in the receive window.
6162 * Actually, allow a little bit of overflow (up to a MTU).
6164 datalen = ntohs(chunk->chunk_hdr->length);
6165 datalen -= sizeof(sctp_data_chunk_t);
6167 deliver = SCTP_CMD_CHUNK_ULP;
6169 /* Think about partial delivery. */
6170 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6172 /* Even if we don't accept this chunk there is
6173 * memory pressure.
6175 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6178 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6179 * seems a bit troublesome in that frag_point varies based on
6180 * PMTU. In cases, such as loopback, this might be a rather
6181 * large spill over.
6183 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6184 (datalen > asoc->rwnd + asoc->frag_point))) {
6186 /* If this is the next TSN, consider reneging to make
6187 * room. Note: Playing nice with a confused sender. A
6188 * malicious sender can still eat up all our buffer
6189 * space and in the future we may want to detect and
6190 * do more drastic reneging.
6192 if (sctp_tsnmap_has_gap(map) &&
6193 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6194 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6195 deliver = SCTP_CMD_RENEGE;
6196 } else {
6197 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6198 __func__, tsn, datalen, asoc->rwnd);
6200 return SCTP_IERROR_IGNORE_TSN;
6205 * Also try to renege to limit our memory usage in the event that
6206 * we are under memory pressure
6207 * If we can't renege, don't worry about it, the sk_rmem_schedule
6208 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6209 * memory usage too much
6211 if (*sk->sk_prot_creator->memory_pressure) {
6212 if (sctp_tsnmap_has_gap(map) &&
6213 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6214 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6215 __func__, tsn);
6216 deliver = SCTP_CMD_RENEGE;
6221 * Section 3.3.10.9 No User Data (9)
6223 * Cause of error
6224 * ---------------
6225 * No User Data: This error cause is returned to the originator of a
6226 * DATA chunk if a received DATA chunk has no user data.
6228 if (unlikely(0 == datalen)) {
6229 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6230 if (err) {
6231 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6232 SCTP_CHUNK(err));
6234 /* We are going to ABORT, so we might as well stop
6235 * processing the rest of the chunks in the packet.
6237 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6238 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6239 SCTP_ERROR(ECONNABORTED));
6240 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6241 SCTP_PERR(SCTP_ERROR_NO_DATA));
6242 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6243 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6244 return SCTP_IERROR_NO_DATA;
6247 chunk->data_accepted = 1;
6249 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6250 * if we renege and the chunk arrives again.
6252 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6253 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6254 if (chunk->asoc)
6255 chunk->asoc->stats.iuodchunks++;
6256 } else {
6257 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6258 if (chunk->asoc)
6259 chunk->asoc->stats.iodchunks++;
6260 ordered = 1;
6263 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6265 * If an endpoint receive a DATA chunk with an invalid stream
6266 * identifier, it shall acknowledge the reception of the DATA chunk
6267 * following the normal procedure, immediately send an ERROR chunk
6268 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6269 * and discard the DATA chunk.
6271 sid = ntohs(data_hdr->stream);
6272 if (sid >= asoc->c.sinit_max_instreams) {
6273 /* Mark tsn as received even though we drop it */
6274 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6276 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6277 &data_hdr->stream,
6278 sizeof(data_hdr->stream),
6279 sizeof(u16));
6280 if (err)
6281 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6282 SCTP_CHUNK(err));
6283 return SCTP_IERROR_BAD_STREAM;
6286 /* Check to see if the SSN is possible for this TSN.
6287 * The biggest gap we can record is 4K wide. Since SSNs wrap
6288 * at an unsigned short, there is no way that an SSN can
6289 * wrap and for a valid TSN. We can simply check if the current
6290 * SSN is smaller then the next expected one. If it is, it wrapped
6291 * and is invalid.
6293 ssn = ntohs(data_hdr->ssn);
6294 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6295 return SCTP_IERROR_PROTO_VIOLATION;
6298 /* Send the data up to the user. Note: Schedule the
6299 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6300 * chunk needs the updated rwnd.
6302 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6304 return SCTP_IERROR_NO_ERROR;