Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / net / sctp / sm_statefuns.c
blobf6b7109195a629563b7a49a06e32847fd3b23984
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 <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 #include <linux/types.h>
56 #include <linux/kernel.h>
57 #include <linux/ip.h>
58 #include <linux/ipv6.h>
59 #include <linux/net.h>
60 #include <linux/inet.h>
61 #include <linux/slab.h>
62 #include <net/sock.h>
63 #include <net/inet_ecn.h>
64 #include <linux/skbuff.h>
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
67 #include <net/sctp/structs.h>
69 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
70 const struct sctp_endpoint *ep,
71 const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 const void *payload,
74 size_t paylen);
75 static int sctp_eat_data(const struct sctp_association *asoc,
76 struct sctp_chunk *chunk,
77 sctp_cmd_seq_t *commands);
78 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
79 const struct sctp_association *asoc,
80 const struct sctp_chunk *chunk);
81 static void sctp_send_stale_cookie_err(struct net *net,
82 const struct sctp_endpoint *ep,
83 const struct sctp_association *asoc,
84 const struct sctp_chunk *chunk,
85 sctp_cmd_seq_t *commands,
86 struct sctp_chunk *err_chunk);
87 static sctp_disposition_t sctp_sf_do_5_2_6_stale(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_shut_8_4_5(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 sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
100 const struct sctp_endpoint *ep,
101 const struct sctp_association *asoc,
102 const sctp_subtype_t type,
103 void *arg,
104 sctp_cmd_seq_t *commands);
105 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
107 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
108 sctp_cmd_seq_t *commands,
109 __be16 error, int sk_err,
110 const struct sctp_association *asoc,
111 struct sctp_transport *transport);
113 static sctp_disposition_t sctp_sf_abort_violation(
114 struct net *net,
115 const struct sctp_endpoint *ep,
116 const struct sctp_association *asoc,
117 void *arg,
118 sctp_cmd_seq_t *commands,
119 const __u8 *payload,
120 const size_t paylen);
122 static sctp_disposition_t sctp_sf_violation_chunklen(
123 struct net *net,
124 const struct sctp_endpoint *ep,
125 const struct sctp_association *asoc,
126 const sctp_subtype_t type,
127 void *arg,
128 sctp_cmd_seq_t *commands);
130 static sctp_disposition_t sctp_sf_violation_paramlen(
131 struct net *net,
132 const struct sctp_endpoint *ep,
133 const struct sctp_association *asoc,
134 const sctp_subtype_t type,
135 void *arg, void *ext,
136 sctp_cmd_seq_t *commands);
138 static sctp_disposition_t sctp_sf_violation_ctsn(
139 struct net *net,
140 const struct sctp_endpoint *ep,
141 const struct sctp_association *asoc,
142 const sctp_subtype_t type,
143 void *arg,
144 sctp_cmd_seq_t *commands);
146 static sctp_disposition_t sctp_sf_violation_chunk(
147 struct net *net,
148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 void *arg,
152 sctp_cmd_seq_t *commands);
154 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
155 const struct sctp_endpoint *ep,
156 const struct sctp_association *asoc,
157 const sctp_subtype_t type,
158 struct sctp_chunk *chunk);
160 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
161 const struct sctp_endpoint *ep,
162 const struct sctp_association *asoc,
163 const sctp_subtype_t type,
164 void *arg,
165 sctp_cmd_seq_t *commands);
167 /* Small helper function that checks if the chunk length
168 * is of the appropriate length. The 'required_length' argument
169 * is set to be the size of a specific chunk we are testing.
170 * Return Values: 1 = Valid length
171 * 0 = Invalid length
174 static inline int
175 sctp_chunk_length_valid(struct sctp_chunk *chunk,
176 __u16 required_length)
178 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
180 if (unlikely(chunk_length < required_length))
181 return 0;
183 return 1;
186 /**********************************************************
187 * These are the state functions for handling chunk events.
188 **********************************************************/
191 * Process the final SHUTDOWN COMPLETE.
193 * Section: 4 (C) (diagram), 9.2
194 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
195 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
196 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
197 * should stop the T2-shutdown timer and remove all knowledge of the
198 * association (and thus the association enters the CLOSED state).
200 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
201 * C) Rules for packet carrying SHUTDOWN COMPLETE:
202 * ...
203 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
204 * if the Verification Tag field of the packet matches its own tag and
205 * the T bit is not set
206 * OR
207 * it is set to its peer's tag and the T bit is set in the Chunk
208 * Flags.
209 * Otherwise, the receiver MUST silently discard the packet
210 * and take no further action. An endpoint MUST ignore the
211 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
213 * Inputs
214 * (endpoint, asoc, chunk)
216 * Outputs
217 * (asoc, reply_msg, msg_up, timers, counters)
219 * The return value is the disposition of the chunk.
221 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
222 const struct sctp_endpoint *ep,
223 const struct sctp_association *asoc,
224 const sctp_subtype_t type,
225 void *arg,
226 sctp_cmd_seq_t *commands)
228 struct sctp_chunk *chunk = arg;
229 struct sctp_ulpevent *ev;
231 if (!sctp_vtag_verify_either(chunk, asoc))
232 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
234 /* RFC 2960 6.10 Bundling
236 * An endpoint MUST NOT bundle INIT, INIT ACK or
237 * SHUTDOWN COMPLETE with any other chunks.
239 if (!chunk->singleton)
240 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
242 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
243 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
244 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
245 commands);
247 /* RFC 2960 10.2 SCTP-to-ULP
249 * H) SHUTDOWN COMPLETE notification
251 * When SCTP completes the shutdown procedures (section 9.2) this
252 * notification is passed to the upper layer.
254 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
255 0, 0, 0, NULL, GFP_ATOMIC);
256 if (ev)
257 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
258 SCTP_ULPEVENT(ev));
260 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
261 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
262 * not the chunk should be discarded. If the endpoint is in
263 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
264 * T2-shutdown timer and remove all knowledge of the
265 * association (and thus the association enters the CLOSED
266 * state).
268 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
269 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
272 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
274 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
275 SCTP_STATE(SCTP_STATE_CLOSED));
277 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
278 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
280 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
282 return SCTP_DISPOSITION_DELETE_TCB;
286 * Respond to a normal INIT chunk.
287 * We are the side that is being asked for an association.
289 * Section: 5.1 Normal Establishment of an Association, B
290 * B) "Z" shall respond immediately with an INIT ACK chunk. The
291 * destination IP address of the INIT ACK MUST be set to the source
292 * IP address of the INIT to which this INIT ACK is responding. In
293 * the response, besides filling in other parameters, "Z" must set the
294 * Verification Tag field to Tag_A, and also provide its own
295 * Verification Tag (Tag_Z) in the Initiate Tag field.
297 * Verification Tag: Must be 0.
299 * Inputs
300 * (endpoint, asoc, chunk)
302 * Outputs
303 * (asoc, reply_msg, msg_up, timers, counters)
305 * The return value is the disposition of the chunk.
307 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
308 const struct sctp_endpoint *ep,
309 const struct sctp_association *asoc,
310 const sctp_subtype_t type,
311 void *arg,
312 sctp_cmd_seq_t *commands)
314 struct sctp_chunk *chunk = arg;
315 struct sctp_chunk *repl;
316 struct sctp_association *new_asoc;
317 struct sctp_chunk *err_chunk;
318 struct sctp_packet *packet;
319 sctp_unrecognized_param_t *unk_param;
320 int len;
322 /* 6.10 Bundling
323 * An endpoint MUST NOT bundle INIT, INIT ACK or
324 * SHUTDOWN COMPLETE with any other chunks.
326 * IG Section 2.11.2
327 * Furthermore, we require that the receiver of an INIT chunk MUST
328 * enforce these rules by silently discarding an arriving packet
329 * with an INIT chunk that is bundled with other chunks.
331 if (!chunk->singleton)
332 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
334 /* If the packet is an OOTB packet which is temporarily on the
335 * control endpoint, respond with an ABORT.
337 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
338 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
342 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
343 * Tag.
345 if (chunk->sctp_hdr->vtag != 0)
346 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
348 /* Make sure that the INIT chunk has a valid length.
349 * Normally, this would cause an ABORT with a Protocol Violation
350 * error, but since we don't have an association, we'll
351 * just discard the packet.
353 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
354 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
356 /* If the INIT is coming toward a closing socket, we'll send back
357 * and ABORT. Essentially, this catches the race of INIT being
358 * backloged to the socket at the same time as the user isses close().
359 * Since the socket and all its associations are going away, we
360 * can treat this OOTB
362 if (sctp_sstate(ep->base.sk, CLOSING))
363 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
365 /* Verify the INIT chunk before processing it. */
366 err_chunk = NULL;
367 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
368 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
369 &err_chunk)) {
370 /* This chunk contains fatal error. It is to be discarded.
371 * Send an ABORT, with causes if there is any.
373 if (err_chunk) {
374 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
375 (__u8 *)(err_chunk->chunk_hdr) +
376 sizeof(sctp_chunkhdr_t),
377 ntohs(err_chunk->chunk_hdr->length) -
378 sizeof(sctp_chunkhdr_t));
380 sctp_chunk_free(err_chunk);
382 if (packet) {
383 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
384 SCTP_PACKET(packet));
385 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
386 return SCTP_DISPOSITION_CONSUME;
387 } else {
388 return SCTP_DISPOSITION_NOMEM;
390 } else {
391 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
392 commands);
396 /* Grab the INIT header. */
397 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
399 /* Tag the variable length parameters. */
400 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
402 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
403 if (!new_asoc)
404 goto nomem;
406 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
407 sctp_scope(sctp_source(chunk)),
408 GFP_ATOMIC) < 0)
409 goto nomem_init;
411 /* The call, sctp_process_init(), can fail on memory allocation. */
412 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
413 (sctp_init_chunk_t *)chunk->chunk_hdr,
414 GFP_ATOMIC))
415 goto nomem_init;
417 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
419 /* If there are errors need to be reported for unknown parameters,
420 * make sure to reserve enough room in the INIT ACK for them.
422 len = 0;
423 if (err_chunk)
424 len = ntohs(err_chunk->chunk_hdr->length) -
425 sizeof(sctp_chunkhdr_t);
427 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
428 if (!repl)
429 goto nomem_init;
431 /* If there are errors need to be reported for unknown parameters,
432 * include them in the outgoing INIT ACK as "Unrecognized parameter"
433 * parameter.
435 if (err_chunk) {
436 /* Get the "Unrecognized parameter" parameter(s) out of the
437 * ERROR chunk generated by sctp_verify_init(). Since the
438 * error cause code for "unknown parameter" and the
439 * "Unrecognized parameter" type is the same, we can
440 * construct the parameters in INIT ACK by copying the
441 * ERROR causes over.
443 unk_param = (sctp_unrecognized_param_t *)
444 ((__u8 *)(err_chunk->chunk_hdr) +
445 sizeof(sctp_chunkhdr_t));
446 /* Replace the cause code with the "Unrecognized parameter"
447 * parameter type.
449 sctp_addto_chunk(repl, len, unk_param);
450 sctp_chunk_free(err_chunk);
453 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
455 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
458 * Note: After sending out INIT ACK with the State Cookie parameter,
459 * "Z" MUST NOT allocate any resources, nor keep any states for the
460 * new association. Otherwise, "Z" will be vulnerable to resource
461 * attacks.
463 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
465 return SCTP_DISPOSITION_DELETE_TCB;
467 nomem_init:
468 sctp_association_free(new_asoc);
469 nomem:
470 if (err_chunk)
471 sctp_chunk_free(err_chunk);
472 return SCTP_DISPOSITION_NOMEM;
476 * Respond to a normal INIT ACK chunk.
477 * We are the side that is initiating the association.
479 * Section: 5.1 Normal Establishment of an Association, C
480 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
481 * timer and leave COOKIE-WAIT state. "A" shall then send the State
482 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
483 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
485 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
486 * DATA chunks, but it MUST be the first chunk in the packet and
487 * until the COOKIE ACK is returned the sender MUST NOT send any
488 * other packets to the peer.
490 * Verification Tag: 3.3.3
491 * If the value of the Initiate Tag in a received INIT ACK chunk is
492 * found to be 0, the receiver MUST treat it as an error and close the
493 * association by transmitting an ABORT.
495 * Inputs
496 * (endpoint, asoc, chunk)
498 * Outputs
499 * (asoc, reply_msg, msg_up, timers, counters)
501 * The return value is the disposition of the chunk.
503 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
504 const struct sctp_endpoint *ep,
505 const struct sctp_association *asoc,
506 const sctp_subtype_t type,
507 void *arg,
508 sctp_cmd_seq_t *commands)
510 struct sctp_chunk *chunk = arg;
511 sctp_init_chunk_t *initchunk;
512 struct sctp_chunk *err_chunk;
513 struct sctp_packet *packet;
515 if (!sctp_vtag_verify(chunk, asoc))
516 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
518 /* 6.10 Bundling
519 * An endpoint MUST NOT bundle INIT, INIT ACK or
520 * SHUTDOWN COMPLETE with any other chunks.
522 if (!chunk->singleton)
523 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
525 /* Make sure that the INIT-ACK chunk has a valid length */
526 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
527 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
528 commands);
529 /* Grab the INIT header. */
530 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
532 /* Verify the INIT chunk before processing it. */
533 err_chunk = NULL;
534 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
535 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
536 &err_chunk)) {
538 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
540 /* This chunk contains fatal error. It is to be discarded.
541 * Send an ABORT, with causes. If there are no causes,
542 * then there wasn't enough memory. Just terminate
543 * the association.
545 if (err_chunk) {
546 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
547 (__u8 *)(err_chunk->chunk_hdr) +
548 sizeof(sctp_chunkhdr_t),
549 ntohs(err_chunk->chunk_hdr->length) -
550 sizeof(sctp_chunkhdr_t));
552 sctp_chunk_free(err_chunk);
554 if (packet) {
555 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
556 SCTP_PACKET(packet));
557 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
558 error = SCTP_ERROR_INV_PARAM;
562 /* SCTP-AUTH, Section 6.3:
563 * It should be noted that if the receiver wants to tear
564 * down an association in an authenticated way only, the
565 * handling of malformed packets should not result in
566 * tearing down the association.
568 * This means that if we only want to abort associations
569 * in an authenticated way (i.e AUTH+ABORT), then we
570 * can't destroy this association just because the packet
571 * was malformed.
573 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
574 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
576 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
577 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
578 asoc, chunk->transport);
581 /* Tag the variable length parameters. Note that we never
582 * convert the parameters in an INIT chunk.
584 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
586 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
588 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
589 SCTP_PEER_INIT(initchunk));
591 /* Reset init error count upon receipt of INIT-ACK. */
592 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
594 /* 5.1 C) "A" shall stop the T1-init timer and leave
595 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
596 * timer, and enter the COOKIE-ECHOED state.
598 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
599 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
600 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
601 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
602 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
603 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
605 /* SCTP-AUTH: genereate the assocition shared keys so that
606 * we can potentially signe the COOKIE-ECHO.
608 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
610 /* 5.1 C) "A" shall then send the State Cookie received in the
611 * INIT ACK chunk in a COOKIE ECHO chunk, ...
613 /* If there is any errors to report, send the ERROR chunk generated
614 * for unknown parameters as well.
616 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
617 SCTP_CHUNK(err_chunk));
619 return SCTP_DISPOSITION_CONSUME;
623 * Respond to a normal COOKIE ECHO chunk.
624 * We are the side that is being asked for an association.
626 * Section: 5.1 Normal Establishment of an Association, D
627 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
628 * with a COOKIE ACK chunk after building a TCB and moving to
629 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
630 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
631 * chunk MUST be the first chunk in the packet.
633 * IMPLEMENTATION NOTE: An implementation may choose to send the
634 * Communication Up notification to the SCTP user upon reception
635 * of a valid COOKIE ECHO chunk.
637 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
638 * D) Rules for packet carrying a COOKIE ECHO
640 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
641 * Initial Tag received in the INIT ACK.
643 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
645 * Inputs
646 * (endpoint, asoc, chunk)
648 * Outputs
649 * (asoc, reply_msg, msg_up, timers, counters)
651 * The return value is the disposition of the chunk.
653 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
654 const struct sctp_endpoint *ep,
655 const struct sctp_association *asoc,
656 const sctp_subtype_t type, void *arg,
657 sctp_cmd_seq_t *commands)
659 struct sctp_chunk *chunk = arg;
660 struct sctp_association *new_asoc;
661 sctp_init_chunk_t *peer_init;
662 struct sctp_chunk *repl;
663 struct sctp_ulpevent *ev, *ai_ev = NULL;
664 int error = 0;
665 struct sctp_chunk *err_chk_p;
666 struct sock *sk;
668 /* If the packet is an OOTB packet which is temporarily on the
669 * control endpoint, respond with an ABORT.
671 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
672 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
673 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
676 /* Make sure that the COOKIE_ECHO chunk has a valid length.
677 * In this case, we check that we have enough for at least a
678 * chunk header. More detailed verification is done
679 * in sctp_unpack_cookie().
681 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
682 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
684 /* If the endpoint is not listening or if the number of associations
685 * on the TCP-style socket exceed the max backlog, respond with an
686 * ABORT.
688 sk = ep->base.sk;
689 if (!sctp_sstate(sk, LISTENING) ||
690 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
691 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
693 /* "Decode" the chunk. We have no optional parameters so we
694 * are in good shape.
696 chunk->subh.cookie_hdr =
697 (struct sctp_signed_cookie *)chunk->skb->data;
698 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
699 sizeof(sctp_chunkhdr_t)))
700 goto nomem;
702 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
703 * "Z" will reply with a COOKIE ACK chunk after building a TCB
704 * and moving to the ESTABLISHED state.
706 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
707 &err_chk_p);
709 /* FIXME:
710 * If the re-build failed, what is the proper error path
711 * from here?
713 * [We should abort the association. --piggy]
715 if (!new_asoc) {
716 /* FIXME: Several errors are possible. A bad cookie should
717 * be silently discarded, but think about logging it too.
719 switch (error) {
720 case -SCTP_IERROR_NOMEM:
721 goto nomem;
723 case -SCTP_IERROR_STALE_COOKIE:
724 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
725 err_chk_p);
726 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
728 case -SCTP_IERROR_BAD_SIG:
729 default:
730 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
735 /* Delay state machine commands until later.
737 * Re-build the bind address for the association is done in
738 * the sctp_unpack_cookie() already.
740 /* This is a brand-new association, so these are not yet side
741 * effects--it is safe to run them here.
743 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
745 if (!sctp_process_init(new_asoc, chunk,
746 &chunk->subh.cookie_hdr->c.peer_addr,
747 peer_init, GFP_ATOMIC))
748 goto nomem_init;
750 /* SCTP-AUTH: Now that we've populate required fields in
751 * sctp_process_init, set up the assocaition shared keys as
752 * necessary so that we can potentially authenticate the ACK
754 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
755 if (error)
756 goto nomem_init;
758 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
759 * is supposed to be authenticated and we have to do delayed
760 * authentication. We've just recreated the association using
761 * the information in the cookie and now it's much easier to
762 * do the authentication.
764 if (chunk->auth_chunk) {
765 struct sctp_chunk auth;
766 sctp_ierror_t ret;
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);
779 /* We can now safely free the auth_chunk clone */
780 kfree_skb(chunk->auth_chunk);
782 if (ret != SCTP_IERROR_NO_ERROR) {
783 sctp_association_free(new_asoc);
784 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
788 repl = sctp_make_cookie_ack(new_asoc, chunk);
789 if (!repl)
790 goto nomem_init;
792 /* RFC 2960 5.1 Normal Establishment of an Association
794 * D) IMPLEMENTATION NOTE: An implementation may choose to
795 * send the Communication Up notification to the SCTP user
796 * upon reception of a valid COOKIE ECHO chunk.
798 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
799 new_asoc->c.sinit_num_ostreams,
800 new_asoc->c.sinit_max_instreams,
801 NULL, GFP_ATOMIC);
802 if (!ev)
803 goto nomem_ev;
805 /* Sockets API Draft Section 5.3.1.6
806 * When a peer sends a Adaptation Layer Indication parameter , SCTP
807 * delivers this notification to inform the application that of the
808 * peers requested adaptation layer.
810 if (new_asoc->peer.adaptation_ind) {
811 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
812 GFP_ATOMIC);
813 if (!ai_ev)
814 goto nomem_aiev;
817 /* Add all the state machine commands now since we've created
818 * everything. This way we don't introduce memory corruptions
819 * during side-effect processing and correclty count established
820 * associations.
822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
823 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
824 SCTP_STATE(SCTP_STATE_ESTABLISHED));
825 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
826 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
829 if (new_asoc->autoclose)
830 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
831 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
833 /* This will send the COOKIE ACK */
834 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
836 /* Queue the ASSOC_CHANGE event */
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
839 /* Send up the Adaptation Layer Indication event */
840 if (ai_ev)
841 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
842 SCTP_ULPEVENT(ai_ev));
844 return SCTP_DISPOSITION_CONSUME;
846 nomem_aiev:
847 sctp_ulpevent_free(ev);
848 nomem_ev:
849 sctp_chunk_free(repl);
850 nomem_init:
851 sctp_association_free(new_asoc);
852 nomem:
853 return SCTP_DISPOSITION_NOMEM;
857 * Respond to a normal COOKIE ACK chunk.
858 * We are the side that is being asked for an association.
860 * RFC 2960 5.1 Normal Establishment of an Association
862 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
863 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
864 * timer. It may also notify its ULP about the successful
865 * establishment of the association with a Communication Up
866 * notification (see Section 10).
868 * Verification Tag:
869 * Inputs
870 * (endpoint, asoc, chunk)
872 * Outputs
873 * (asoc, reply_msg, msg_up, timers, counters)
875 * The return value is the disposition of the chunk.
877 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
878 const struct sctp_endpoint *ep,
879 const struct sctp_association *asoc,
880 const sctp_subtype_t type, void *arg,
881 sctp_cmd_seq_t *commands)
883 struct sctp_chunk *chunk = arg;
884 struct sctp_ulpevent *ev;
886 if (!sctp_vtag_verify(chunk, asoc))
887 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
889 /* Verify that the chunk length for the COOKIE-ACK is OK.
890 * If we don't do this, any bundled chunks may be junked.
892 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
893 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
894 commands);
896 /* Reset init error count upon receipt of COOKIE-ACK,
897 * to avoid problems with the managemement of this
898 * counter in stale cookie situations when a transition back
899 * from the COOKIE-ECHOED state to the COOKIE-WAIT
900 * state is performed.
902 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
904 /* RFC 2960 5.1 Normal Establishment of an Association
906 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
907 * from the COOKIE-ECHOED state to the ESTABLISHED state,
908 * stopping the T1-cookie timer.
910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
911 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
912 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
913 SCTP_STATE(SCTP_STATE_ESTABLISHED));
914 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
915 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
916 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
917 if (asoc->autoclose)
918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
919 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
921 /* It may also notify its ULP about the successful
922 * establishment of the association with a Communication Up
923 * notification (see Section 10).
925 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
926 0, asoc->c.sinit_num_ostreams,
927 asoc->c.sinit_max_instreams,
928 NULL, GFP_ATOMIC);
930 if (!ev)
931 goto nomem;
933 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
935 /* Sockets API Draft Section 5.3.1.6
936 * When a peer sends a Adaptation Layer Indication parameter , SCTP
937 * delivers this notification to inform the application that of the
938 * peers requested adaptation layer.
940 if (asoc->peer.adaptation_ind) {
941 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
942 if (!ev)
943 goto nomem;
945 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
946 SCTP_ULPEVENT(ev));
949 return SCTP_DISPOSITION_CONSUME;
950 nomem:
951 return SCTP_DISPOSITION_NOMEM;
954 /* Generate and sendout a heartbeat packet. */
955 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
956 const struct sctp_association *asoc,
957 const sctp_subtype_t type,
958 void *arg,
959 sctp_cmd_seq_t *commands)
961 struct sctp_transport *transport = (struct sctp_transport *) arg;
962 struct sctp_chunk *reply;
964 /* Send a heartbeat to our peer. */
965 reply = sctp_make_heartbeat(asoc, transport);
966 if (!reply)
967 return SCTP_DISPOSITION_NOMEM;
969 /* Set rto_pending indicating that an RTT measurement
970 * is started with this heartbeat chunk.
972 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
973 SCTP_TRANSPORT(transport));
975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
976 return SCTP_DISPOSITION_CONSUME;
979 /* Generate a HEARTBEAT packet on the given transport. */
980 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
981 const struct sctp_endpoint *ep,
982 const struct sctp_association *asoc,
983 const sctp_subtype_t type,
984 void *arg,
985 sctp_cmd_seq_t *commands)
987 struct sctp_transport *transport = (struct sctp_transport *) arg;
989 if (asoc->overall_error_count >= asoc->max_retrans) {
990 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
991 SCTP_ERROR(ETIMEDOUT));
992 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
993 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
994 SCTP_PERR(SCTP_ERROR_NO_ERROR));
995 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
996 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
997 return SCTP_DISPOSITION_DELETE_TCB;
1000 /* Section 3.3.5.
1001 * The Sender-specific Heartbeat Info field should normally include
1002 * information about the sender's current time when this HEARTBEAT
1003 * chunk is sent and the destination transport address to which this
1004 * HEARTBEAT is sent (see Section 8.3).
1007 if (transport->param_flags & SPP_HB_ENABLE) {
1008 if (SCTP_DISPOSITION_NOMEM ==
1009 sctp_sf_heartbeat(ep, asoc, type, arg,
1010 commands))
1011 return SCTP_DISPOSITION_NOMEM;
1013 /* Set transport error counter and association error counter
1014 * when sending heartbeat.
1016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1017 SCTP_TRANSPORT(transport));
1019 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1020 SCTP_TRANSPORT(transport));
1021 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1022 SCTP_TRANSPORT(transport));
1024 return SCTP_DISPOSITION_CONSUME;
1028 * Process an heartbeat request.
1030 * Section: 8.3 Path Heartbeat
1031 * The receiver of the HEARTBEAT should immediately respond with a
1032 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1033 * from the received HEARTBEAT chunk.
1035 * Verification Tag: 8.5 Verification Tag [Normal verification]
1036 * When receiving an SCTP packet, the endpoint MUST ensure that the
1037 * value in the Verification Tag field of the received SCTP packet
1038 * matches its own Tag. If the received Verification Tag value does not
1039 * match the receiver's own tag value, the receiver shall silently
1040 * discard the packet and shall not process it any further except for
1041 * those cases listed in Section 8.5.1 below.
1043 * Inputs
1044 * (endpoint, asoc, chunk)
1046 * Outputs
1047 * (asoc, reply_msg, msg_up, timers, counters)
1049 * The return value is the disposition of the chunk.
1051 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1052 const struct sctp_endpoint *ep,
1053 const struct sctp_association *asoc,
1054 const sctp_subtype_t type,
1055 void *arg,
1056 sctp_cmd_seq_t *commands)
1058 sctp_paramhdr_t *param_hdr;
1059 struct sctp_chunk *chunk = arg;
1060 struct sctp_chunk *reply;
1061 size_t paylen = 0;
1063 if (!sctp_vtag_verify(chunk, asoc))
1064 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1066 /* Make sure that the HEARTBEAT chunk has a valid length. */
1067 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1068 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1069 commands);
1071 /* 8.3 The receiver of the HEARTBEAT should immediately
1072 * respond with a HEARTBEAT ACK that contains the Heartbeat
1073 * Information field copied from the received HEARTBEAT chunk.
1075 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1076 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
1077 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1079 if (ntohs(param_hdr->length) > paylen)
1080 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1081 param_hdr, commands);
1083 if (!pskb_pull(chunk->skb, paylen))
1084 goto nomem;
1086 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1087 if (!reply)
1088 goto nomem;
1090 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1091 return SCTP_DISPOSITION_CONSUME;
1093 nomem:
1094 return SCTP_DISPOSITION_NOMEM;
1098 * Process the returning HEARTBEAT ACK.
1100 * Section: 8.3 Path Heartbeat
1101 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1102 * should clear the error counter of the destination transport
1103 * address to which the HEARTBEAT was sent, and mark the destination
1104 * transport address as active if it is not so marked. The endpoint may
1105 * optionally report to the upper layer when an inactive destination
1106 * address is marked as active due to the reception of the latest
1107 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1108 * clear the association overall error count as well (as defined
1109 * in section 8.1).
1111 * The receiver of the HEARTBEAT ACK should also perform an RTT
1112 * measurement for that destination transport address using the time
1113 * value carried in the HEARTBEAT ACK chunk.
1115 * Verification Tag: 8.5 Verification Tag [Normal verification]
1117 * Inputs
1118 * (endpoint, asoc, chunk)
1120 * Outputs
1121 * (asoc, reply_msg, msg_up, timers, counters)
1123 * The return value is the disposition of the chunk.
1125 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1126 const struct sctp_endpoint *ep,
1127 const struct sctp_association *asoc,
1128 const sctp_subtype_t type,
1129 void *arg,
1130 sctp_cmd_seq_t *commands)
1132 struct sctp_chunk *chunk = arg;
1133 union sctp_addr from_addr;
1134 struct sctp_transport *link;
1135 sctp_sender_hb_info_t *hbinfo;
1136 unsigned long max_interval;
1138 if (!sctp_vtag_verify(chunk, asoc))
1139 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1141 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1142 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1143 sizeof(sctp_sender_hb_info_t)))
1144 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1145 commands);
1147 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1148 /* Make sure that the length of the parameter is what we expect */
1149 if (ntohs(hbinfo->param_hdr.length) !=
1150 sizeof(sctp_sender_hb_info_t)) {
1151 return SCTP_DISPOSITION_DISCARD;
1154 from_addr = hbinfo->daddr;
1155 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1157 /* This should never happen, but lets log it if so. */
1158 if (unlikely(!link)) {
1159 if (from_addr.sa.sa_family == AF_INET6) {
1160 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1161 __func__,
1162 asoc,
1163 &from_addr.v6.sin6_addr);
1164 } else {
1165 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1166 __func__,
1167 asoc,
1168 &from_addr.v4.sin_addr.s_addr);
1170 return SCTP_DISPOSITION_DISCARD;
1173 /* Validate the 64-bit random nonce. */
1174 if (hbinfo->hb_nonce != link->hb_nonce)
1175 return SCTP_DISPOSITION_DISCARD;
1177 max_interval = link->hbinterval + link->rto;
1179 /* Check if the timestamp looks valid. */
1180 if (time_after(hbinfo->sent_at, jiffies) ||
1181 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1182 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1183 "for transport:%p\n", __func__, link);
1185 return SCTP_DISPOSITION_DISCARD;
1188 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1189 * the HEARTBEAT should clear the error counter of the
1190 * destination transport address to which the HEARTBEAT was
1191 * sent and mark the destination transport address as active if
1192 * it is not so marked.
1194 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1196 return SCTP_DISPOSITION_CONSUME;
1199 /* Helper function to send out an abort for the restart
1200 * condition.
1202 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1203 struct sctp_chunk *init,
1204 sctp_cmd_seq_t *commands)
1206 int len;
1207 struct sctp_packet *pkt;
1208 union sctp_addr_param *addrparm;
1209 struct sctp_errhdr *errhdr;
1210 struct sctp_endpoint *ep;
1211 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1212 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1214 /* Build the error on the stack. We are way to malloc crazy
1215 * throughout the code today.
1217 errhdr = (struct sctp_errhdr *)buffer;
1218 addrparm = (union sctp_addr_param *)errhdr->variable;
1220 /* Copy into a parm format. */
1221 len = af->to_addr_param(ssa, addrparm);
1222 len += sizeof(sctp_errhdr_t);
1224 errhdr->cause = SCTP_ERROR_RESTART;
1225 errhdr->length = htons(len);
1227 /* Assign to the control socket. */
1228 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1230 /* Association is NULL since this may be a restart attack and we
1231 * want to send back the attacker's vtag.
1233 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1235 if (!pkt)
1236 goto out;
1237 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1239 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1241 /* Discard the rest of the inbound packet. */
1242 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1244 out:
1245 /* Even if there is no memory, treat as a failure so
1246 * the packet will get dropped.
1248 return 0;
1251 static bool list_has_sctp_addr(const struct list_head *list,
1252 union sctp_addr *ipaddr)
1254 struct sctp_transport *addr;
1256 list_for_each_entry(addr, list, transports) {
1257 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1258 return true;
1261 return false;
1263 /* A restart is occurring, check to make sure no new addresses
1264 * are being added as we may be under a takeover attack.
1266 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1267 const struct sctp_association *asoc,
1268 struct sctp_chunk *init,
1269 sctp_cmd_seq_t *commands)
1271 struct net *net = sock_net(new_asoc->base.sk);
1272 struct sctp_transport *new_addr;
1273 int ret = 1;
1275 /* Implementor's Guide - Section 5.2.2
1276 * ...
1277 * Before responding the endpoint MUST check to see if the
1278 * unexpected INIT adds new addresses to the association. If new
1279 * addresses are added to the association, the endpoint MUST respond
1280 * with an ABORT..
1283 /* Search through all current addresses and make sure
1284 * we aren't adding any new ones.
1286 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1287 transports) {
1288 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1289 &new_addr->ipaddr)) {
1290 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1291 commands);
1292 ret = 0;
1293 break;
1297 /* Return success if all addresses were found. */
1298 return ret;
1301 /* Populate the verification/tie tags based on overlapping INIT
1302 * scenario.
1304 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1306 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1307 const struct sctp_association *asoc)
1309 switch (asoc->state) {
1311 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1313 case SCTP_STATE_COOKIE_WAIT:
1314 new_asoc->c.my_vtag = asoc->c.my_vtag;
1315 new_asoc->c.my_ttag = asoc->c.my_vtag;
1316 new_asoc->c.peer_ttag = 0;
1317 break;
1319 case SCTP_STATE_COOKIE_ECHOED:
1320 new_asoc->c.my_vtag = asoc->c.my_vtag;
1321 new_asoc->c.my_ttag = asoc->c.my_vtag;
1322 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1323 break;
1325 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1326 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1328 default:
1329 new_asoc->c.my_ttag = asoc->c.my_vtag;
1330 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1331 break;
1334 /* Other parameters for the endpoint SHOULD be copied from the
1335 * existing parameters of the association (e.g. number of
1336 * outbound streams) into the INIT ACK and cookie.
1338 new_asoc->rwnd = asoc->rwnd;
1339 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1340 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1341 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1345 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1346 * handling action.
1348 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1350 * Returns value representing action to be taken. These action values
1351 * correspond to Action/Description values in RFC 2960, Table 2.
1353 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1354 const struct sctp_association *asoc)
1356 /* In this case, the peer may have restarted. */
1357 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1358 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1359 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1360 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1361 return 'A';
1363 /* Collision case B. */
1364 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1365 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1366 (0 == asoc->c.peer_vtag))) {
1367 return 'B';
1370 /* Collision case D. */
1371 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1372 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1373 return 'D';
1375 /* Collision case C. */
1376 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1377 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1378 (0 == new_asoc->c.my_ttag) &&
1379 (0 == new_asoc->c.peer_ttag))
1380 return 'C';
1382 /* No match to any of the special cases; discard this packet. */
1383 return 'E';
1386 /* Common helper routine for both duplicate and simulataneous INIT
1387 * chunk handling.
1389 static sctp_disposition_t sctp_sf_do_unexpected_init(
1390 struct net *net,
1391 const struct sctp_endpoint *ep,
1392 const struct sctp_association *asoc,
1393 const sctp_subtype_t type,
1394 void *arg, sctp_cmd_seq_t *commands)
1396 sctp_disposition_t retval;
1397 struct sctp_chunk *chunk = arg;
1398 struct sctp_chunk *repl;
1399 struct sctp_association *new_asoc;
1400 struct sctp_chunk *err_chunk;
1401 struct sctp_packet *packet;
1402 sctp_unrecognized_param_t *unk_param;
1403 int len;
1405 /* 6.10 Bundling
1406 * An endpoint MUST NOT bundle INIT, INIT ACK or
1407 * SHUTDOWN COMPLETE with any other chunks.
1409 * IG Section 2.11.2
1410 * Furthermore, we require that the receiver of an INIT chunk MUST
1411 * enforce these rules by silently discarding an arriving packet
1412 * with an INIT chunk that is bundled with other chunks.
1414 if (!chunk->singleton)
1415 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1417 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1418 * Tag.
1420 if (chunk->sctp_hdr->vtag != 0)
1421 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1423 /* Make sure that the INIT chunk has a valid length.
1424 * In this case, we generate a protocol violation since we have
1425 * an association established.
1427 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1429 commands);
1430 /* Grab the INIT header. */
1431 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1433 /* Tag the variable length parameters. */
1434 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1436 /* Verify the INIT chunk before processing it. */
1437 err_chunk = NULL;
1438 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
1439 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1440 &err_chunk)) {
1441 /* This chunk contains fatal error. It is to be discarded.
1442 * Send an ABORT, with causes if there is any.
1444 if (err_chunk) {
1445 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1446 (__u8 *)(err_chunk->chunk_hdr) +
1447 sizeof(sctp_chunkhdr_t),
1448 ntohs(err_chunk->chunk_hdr->length) -
1449 sizeof(sctp_chunkhdr_t));
1451 if (packet) {
1452 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1453 SCTP_PACKET(packet));
1454 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1455 retval = SCTP_DISPOSITION_CONSUME;
1456 } else {
1457 retval = SCTP_DISPOSITION_NOMEM;
1459 goto cleanup;
1460 } else {
1461 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1462 commands);
1467 * Other parameters for the endpoint SHOULD be copied from the
1468 * existing parameters of the association (e.g. number of
1469 * outbound streams) into the INIT ACK and cookie.
1470 * FIXME: We are copying parameters from the endpoint not the
1471 * association.
1473 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1474 if (!new_asoc)
1475 goto nomem;
1477 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1478 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1479 goto nomem;
1481 /* In the outbound INIT ACK the endpoint MUST copy its current
1482 * Verification Tag and Peers Verification tag into a reserved
1483 * place (local tie-tag and per tie-tag) within the state cookie.
1485 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1486 (sctp_init_chunk_t *)chunk->chunk_hdr,
1487 GFP_ATOMIC))
1488 goto nomem;
1490 /* Make sure no new addresses are being added during the
1491 * restart. Do not do this check for COOKIE-WAIT state,
1492 * since there are no peer addresses to check against.
1493 * Upon return an ABORT will have been sent if needed.
1495 if (!sctp_state(asoc, COOKIE_WAIT)) {
1496 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1497 commands)) {
1498 retval = SCTP_DISPOSITION_CONSUME;
1499 goto nomem_retval;
1503 sctp_tietags_populate(new_asoc, asoc);
1505 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1507 /* If there are errors need to be reported for unknown parameters,
1508 * make sure to reserve enough room in the INIT ACK for them.
1510 len = 0;
1511 if (err_chunk) {
1512 len = ntohs(err_chunk->chunk_hdr->length) -
1513 sizeof(sctp_chunkhdr_t);
1516 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1517 if (!repl)
1518 goto nomem;
1520 /* If there are errors need to be reported for unknown parameters,
1521 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1522 * parameter.
1524 if (err_chunk) {
1525 /* Get the "Unrecognized parameter" parameter(s) out of the
1526 * ERROR chunk generated by sctp_verify_init(). Since the
1527 * error cause code for "unknown parameter" and the
1528 * "Unrecognized parameter" type is the same, we can
1529 * construct the parameters in INIT ACK by copying the
1530 * ERROR causes over.
1532 unk_param = (sctp_unrecognized_param_t *)
1533 ((__u8 *)(err_chunk->chunk_hdr) +
1534 sizeof(sctp_chunkhdr_t));
1535 /* Replace the cause code with the "Unrecognized parameter"
1536 * parameter type.
1538 sctp_addto_chunk(repl, len, unk_param);
1541 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1542 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1545 * Note: After sending out INIT ACK with the State Cookie parameter,
1546 * "Z" MUST NOT allocate any resources for this new association.
1547 * Otherwise, "Z" will be vulnerable to resource attacks.
1549 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1550 retval = SCTP_DISPOSITION_CONSUME;
1552 return retval;
1554 nomem:
1555 retval = SCTP_DISPOSITION_NOMEM;
1556 nomem_retval:
1557 if (new_asoc)
1558 sctp_association_free(new_asoc);
1559 cleanup:
1560 if (err_chunk)
1561 sctp_chunk_free(err_chunk);
1562 return retval;
1566 * Handle simultaneous INIT.
1567 * This means we started an INIT and then we got an INIT request from
1568 * our peer.
1570 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1571 * This usually indicates an initialization collision, i.e., each
1572 * endpoint is attempting, at about the same time, to establish an
1573 * association with the other endpoint.
1575 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1576 * endpoint MUST respond with an INIT ACK using the same parameters it
1577 * sent in its original INIT chunk (including its Verification Tag,
1578 * unchanged). These original parameters are combined with those from the
1579 * newly received INIT chunk. The endpoint shall also generate a State
1580 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1581 * INIT to calculate the State Cookie.
1583 * After that, the endpoint MUST NOT change its state, the T1-init
1584 * timer shall be left running and the corresponding TCB MUST NOT be
1585 * destroyed. The normal procedures for handling State Cookies when
1586 * a TCB exists will resolve the duplicate INITs to a single association.
1588 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1589 * its Tie-Tags with the Tag information of itself and its peer (see
1590 * section 5.2.2 for a description of the Tie-Tags).
1592 * Verification Tag: Not explicit, but an INIT can not have a valid
1593 * verification tag, so we skip the check.
1595 * Inputs
1596 * (endpoint, asoc, chunk)
1598 * Outputs
1599 * (asoc, reply_msg, msg_up, timers, counters)
1601 * The return value is the disposition of the chunk.
1603 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1604 const struct sctp_endpoint *ep,
1605 const struct sctp_association *asoc,
1606 const sctp_subtype_t type,
1607 void *arg,
1608 sctp_cmd_seq_t *commands)
1610 /* Call helper to do the real work for both simulataneous and
1611 * duplicate INIT chunk handling.
1613 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1617 * Handle duplicated INIT messages. These are usually delayed
1618 * restransmissions.
1620 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1621 * COOKIE-ECHOED and COOKIE-WAIT
1623 * Unless otherwise stated, upon reception of an unexpected INIT for
1624 * this association, the endpoint shall generate an INIT ACK with a
1625 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1626 * current Verification Tag and peer's Verification Tag into a reserved
1627 * place within the state cookie. We shall refer to these locations as
1628 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1629 * containing this INIT ACK MUST carry a Verification Tag value equal to
1630 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1631 * MUST contain a new Initiation Tag (randomly generated see Section
1632 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1633 * existing parameters of the association (e.g. number of outbound
1634 * streams) into the INIT ACK and cookie.
1636 * After sending out the INIT ACK, the endpoint shall take no further
1637 * actions, i.e., the existing association, including its current state,
1638 * and the corresponding TCB MUST NOT be changed.
1640 * Note: Only when a TCB exists and the association is not in a COOKIE-
1641 * WAIT state are the Tie-Tags populated. For a normal association INIT
1642 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1643 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1644 * State Cookie are populated as specified in section 5.2.1.
1646 * Verification Tag: Not specified, but an INIT has no way of knowing
1647 * what the verification tag could be, so we ignore it.
1649 * Inputs
1650 * (endpoint, asoc, chunk)
1652 * Outputs
1653 * (asoc, reply_msg, msg_up, timers, counters)
1655 * The return value is the disposition of the chunk.
1657 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1658 const struct sctp_endpoint *ep,
1659 const struct sctp_association *asoc,
1660 const sctp_subtype_t type,
1661 void *arg,
1662 sctp_cmd_seq_t *commands)
1664 /* Call helper to do the real work for both simulataneous and
1665 * duplicate INIT chunk handling.
1667 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1672 * Unexpected INIT-ACK handler.
1674 * Section 5.2.3
1675 * If an INIT ACK received by an endpoint in any state other than the
1676 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1677 * An unexpected INIT ACK usually indicates the processing of an old or
1678 * duplicated INIT chunk.
1680 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1681 const struct sctp_endpoint *ep,
1682 const struct sctp_association *asoc,
1683 const sctp_subtype_t type,
1684 void *arg, sctp_cmd_seq_t *commands)
1686 /* Per the above section, we'll discard the chunk if we have an
1687 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1689 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1690 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1691 else
1692 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1695 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1697 * Section 5.2.4
1698 * A) In this case, the peer may have restarted.
1700 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1701 const struct sctp_endpoint *ep,
1702 const struct sctp_association *asoc,
1703 struct sctp_chunk *chunk,
1704 sctp_cmd_seq_t *commands,
1705 struct sctp_association *new_asoc)
1707 sctp_init_chunk_t *peer_init;
1708 struct sctp_ulpevent *ev;
1709 struct sctp_chunk *repl;
1710 struct sctp_chunk *err;
1711 sctp_disposition_t disposition;
1713 /* new_asoc is a brand-new association, so these are not yet
1714 * side effects--it is safe to run them here.
1716 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1718 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1719 GFP_ATOMIC))
1720 goto nomem;
1722 /* Make sure no new addresses are being added during the
1723 * restart. Though this is a pretty complicated attack
1724 * since you'd have to get inside the cookie.
1726 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1727 return SCTP_DISPOSITION_CONSUME;
1730 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1731 * the peer has restarted (Action A), it MUST NOT setup a new
1732 * association but instead resend the SHUTDOWN ACK and send an ERROR
1733 * chunk with a "Cookie Received while Shutting Down" error cause to
1734 * its peer.
1736 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1737 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1738 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1739 chunk, commands);
1740 if (SCTP_DISPOSITION_NOMEM == disposition)
1741 goto nomem;
1743 err = sctp_make_op_error(asoc, chunk,
1744 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1745 NULL, 0, 0);
1746 if (err)
1747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1748 SCTP_CHUNK(err));
1750 return SCTP_DISPOSITION_CONSUME;
1753 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1754 * data. Consider the optional choice of resending of this data.
1756 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1758 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1759 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1761 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1762 * and ASCONF-ACK cache.
1764 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1765 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1766 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1768 repl = sctp_make_cookie_ack(new_asoc, chunk);
1769 if (!repl)
1770 goto nomem;
1772 /* Report association restart to upper layer. */
1773 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1774 new_asoc->c.sinit_num_ostreams,
1775 new_asoc->c.sinit_max_instreams,
1776 NULL, GFP_ATOMIC);
1777 if (!ev)
1778 goto nomem_ev;
1780 /* Update the content of current association. */
1781 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1782 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1783 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1784 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1786 return SCTP_DISPOSITION_CONSUME;
1788 nomem_ev:
1789 sctp_chunk_free(repl);
1790 nomem:
1791 return SCTP_DISPOSITION_NOMEM;
1794 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1796 * Section 5.2.4
1797 * B) In this case, both sides may be attempting to start an association
1798 * at about the same time but the peer endpoint started its INIT
1799 * after responding to the local endpoint's INIT
1801 /* This case represents an initialization collision. */
1802 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1803 const struct sctp_endpoint *ep,
1804 const struct sctp_association *asoc,
1805 struct sctp_chunk *chunk,
1806 sctp_cmd_seq_t *commands,
1807 struct sctp_association *new_asoc)
1809 sctp_init_chunk_t *peer_init;
1810 struct sctp_chunk *repl;
1812 /* new_asoc is a brand-new association, so these are not yet
1813 * side effects--it is safe to run them here.
1815 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1816 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1817 GFP_ATOMIC))
1818 goto nomem;
1820 /* Update the content of current association. */
1821 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1823 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1824 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1825 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1827 repl = sctp_make_cookie_ack(new_asoc, chunk);
1828 if (!repl)
1829 goto nomem;
1831 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1833 /* RFC 2960 5.1 Normal Establishment of an Association
1835 * D) IMPLEMENTATION NOTE: An implementation may choose to
1836 * send the Communication Up notification to the SCTP user
1837 * upon reception of a valid COOKIE ECHO chunk.
1839 * Sadly, this needs to be implemented as a side-effect, because
1840 * we are not guaranteed to have set the association id of the real
1841 * association and so these notifications need to be delayed until
1842 * the association id is allocated.
1845 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1847 /* Sockets API Draft Section 5.3.1.6
1848 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1849 * delivers this notification to inform the application that of the
1850 * peers requested adaptation layer.
1852 * This also needs to be done as a side effect for the same reason as
1853 * above.
1855 if (asoc->peer.adaptation_ind)
1856 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1858 return SCTP_DISPOSITION_CONSUME;
1860 nomem:
1861 return SCTP_DISPOSITION_NOMEM;
1864 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1866 * Section 5.2.4
1867 * C) In this case, the local endpoint's cookie has arrived late.
1868 * Before it arrived, the local endpoint sent an INIT and received an
1869 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1870 * but a new tag of its own.
1872 /* This case represents an initialization collision. */
1873 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1874 const struct sctp_endpoint *ep,
1875 const struct sctp_association *asoc,
1876 struct sctp_chunk *chunk,
1877 sctp_cmd_seq_t *commands,
1878 struct sctp_association *new_asoc)
1880 /* The cookie should be silently discarded.
1881 * The endpoint SHOULD NOT change states and should leave
1882 * any timers running.
1884 return SCTP_DISPOSITION_DISCARD;
1887 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1889 * Section 5.2.4
1891 * D) When both local and remote tags match the endpoint should always
1892 * enter the ESTABLISHED state, if it has not already done so.
1894 /* This case represents an initialization collision. */
1895 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1896 const struct sctp_endpoint *ep,
1897 const struct sctp_association *asoc,
1898 struct sctp_chunk *chunk,
1899 sctp_cmd_seq_t *commands,
1900 struct sctp_association *new_asoc)
1902 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1903 struct sctp_chunk *repl;
1905 /* Clarification from Implementor's Guide:
1906 * D) When both local and remote tags match the endpoint should
1907 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1908 * It should stop any cookie timer that may be running and send
1909 * a COOKIE ACK.
1912 /* Don't accidentally move back into established state. */
1913 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1915 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1916 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1917 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1918 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1919 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1920 SCTP_NULL());
1922 /* RFC 2960 5.1 Normal Establishment of an Association
1924 * D) IMPLEMENTATION NOTE: An implementation may choose
1925 * to send the Communication Up notification to the
1926 * SCTP user upon reception of a valid COOKIE
1927 * ECHO chunk.
1929 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1930 SCTP_COMM_UP, 0,
1931 asoc->c.sinit_num_ostreams,
1932 asoc->c.sinit_max_instreams,
1933 NULL, GFP_ATOMIC);
1934 if (!ev)
1935 goto nomem;
1937 /* Sockets API Draft Section 5.3.1.6
1938 * When a peer sends a Adaptation Layer Indication parameter,
1939 * SCTP delivers this notification to inform the application
1940 * that of the peers requested adaptation layer.
1942 if (asoc->peer.adaptation_ind) {
1943 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1944 GFP_ATOMIC);
1945 if (!ai_ev)
1946 goto nomem;
1951 repl = sctp_make_cookie_ack(new_asoc, chunk);
1952 if (!repl)
1953 goto nomem;
1955 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1957 if (ev)
1958 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1959 SCTP_ULPEVENT(ev));
1960 if (ai_ev)
1961 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1962 SCTP_ULPEVENT(ai_ev));
1964 return SCTP_DISPOSITION_CONSUME;
1966 nomem:
1967 if (ai_ev)
1968 sctp_ulpevent_free(ai_ev);
1969 if (ev)
1970 sctp_ulpevent_free(ev);
1971 return SCTP_DISPOSITION_NOMEM;
1975 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1976 * chunk was retransmitted and then delayed in the network.
1978 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1980 * Verification Tag: None. Do cookie validation.
1982 * Inputs
1983 * (endpoint, asoc, chunk)
1985 * Outputs
1986 * (asoc, reply_msg, msg_up, timers, counters)
1988 * The return value is the disposition of the chunk.
1990 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1991 const struct sctp_endpoint *ep,
1992 const struct sctp_association *asoc,
1993 const sctp_subtype_t type,
1994 void *arg,
1995 sctp_cmd_seq_t *commands)
1997 sctp_disposition_t retval;
1998 struct sctp_chunk *chunk = arg;
1999 struct sctp_association *new_asoc;
2000 int error = 0;
2001 char action;
2002 struct sctp_chunk *err_chk_p;
2004 /* Make sure that the chunk has a valid length from the protocol
2005 * perspective. In this case check to make sure we have at least
2006 * enough for the chunk header. Cookie length verification is
2007 * done later.
2009 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2010 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2011 commands);
2013 /* "Decode" the chunk. We have no optional parameters so we
2014 * are in good shape.
2016 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2017 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2018 sizeof(sctp_chunkhdr_t)))
2019 goto nomem;
2021 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2022 * of a duplicate COOKIE ECHO match the Verification Tags of the
2023 * current association, consider the State Cookie valid even if
2024 * the lifespan is exceeded.
2026 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2027 &err_chk_p);
2029 /* FIXME:
2030 * If the re-build failed, what is the proper error path
2031 * from here?
2033 * [We should abort the association. --piggy]
2035 if (!new_asoc) {
2036 /* FIXME: Several errors are possible. A bad cookie should
2037 * be silently discarded, but think about logging it too.
2039 switch (error) {
2040 case -SCTP_IERROR_NOMEM:
2041 goto nomem;
2043 case -SCTP_IERROR_STALE_COOKIE:
2044 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2045 err_chk_p);
2046 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2047 case -SCTP_IERROR_BAD_SIG:
2048 default:
2049 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2053 /* Compare the tie_tag in cookie with the verification tag of
2054 * current association.
2056 action = sctp_tietags_compare(new_asoc, asoc);
2058 switch (action) {
2059 case 'A': /* Association restart. */
2060 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2061 new_asoc);
2062 break;
2064 case 'B': /* Collision case B. */
2065 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2066 new_asoc);
2067 break;
2069 case 'C': /* Collision case C. */
2070 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2071 new_asoc);
2072 break;
2074 case 'D': /* Collision case D. */
2075 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2076 new_asoc);
2077 break;
2079 default: /* Discard packet for all others. */
2080 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2081 break;
2084 /* Delete the tempory new association. */
2085 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2086 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2088 /* Restore association pointer to provide SCTP command interpeter
2089 * with a valid context in case it needs to manipulate
2090 * the queues */
2091 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2092 SCTP_ASOC((struct sctp_association *)asoc));
2094 return retval;
2096 nomem:
2097 return SCTP_DISPOSITION_NOMEM;
2101 * Process an ABORT. (SHUTDOWN-PENDING state)
2103 * See sctp_sf_do_9_1_abort().
2105 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2106 struct net *net,
2107 const struct sctp_endpoint *ep,
2108 const struct sctp_association *asoc,
2109 const sctp_subtype_t type,
2110 void *arg,
2111 sctp_cmd_seq_t *commands)
2113 struct sctp_chunk *chunk = arg;
2115 if (!sctp_vtag_verify_either(chunk, asoc))
2116 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2118 /* Make sure that the ABORT chunk has a valid length.
2119 * Since this is an ABORT chunk, we have to discard it
2120 * because of the following text:
2121 * RFC 2960, Section 3.3.7
2122 * If an endpoint receives an ABORT with a format error or for an
2123 * association that doesn't exist, it MUST silently discard it.
2124 * Because the length is "invalid", we can't really discard just
2125 * as we do not know its true length. So, to be safe, discard the
2126 * packet.
2128 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2129 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2131 /* ADD-IP: Special case for ABORT chunks
2132 * F4) One special consideration is that ABORT Chunks arriving
2133 * destined to the IP address being deleted MUST be
2134 * ignored (see Section 5.3.1 for further details).
2136 if (SCTP_ADDR_DEL ==
2137 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2138 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2140 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2144 * Process an ABORT. (SHUTDOWN-SENT state)
2146 * See sctp_sf_do_9_1_abort().
2148 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2149 const struct sctp_endpoint *ep,
2150 const struct sctp_association *asoc,
2151 const sctp_subtype_t type,
2152 void *arg,
2153 sctp_cmd_seq_t *commands)
2155 struct sctp_chunk *chunk = arg;
2157 if (!sctp_vtag_verify_either(chunk, asoc))
2158 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2160 /* Make sure that the ABORT chunk has a valid length.
2161 * Since this is an ABORT chunk, we have to discard it
2162 * because of the following text:
2163 * RFC 2960, Section 3.3.7
2164 * If an endpoint receives an ABORT with a format error or for an
2165 * association that doesn't exist, it MUST silently discard it.
2166 * Because the length is "invalid", we can't really discard just
2167 * as we do not know its true length. So, to be safe, discard the
2168 * packet.
2170 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2171 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2173 /* ADD-IP: Special case for ABORT chunks
2174 * F4) One special consideration is that ABORT Chunks arriving
2175 * destined to the IP address being deleted MUST be
2176 * ignored (see Section 5.3.1 for further details).
2178 if (SCTP_ADDR_DEL ==
2179 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2180 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2182 /* Stop the T2-shutdown timer. */
2183 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2184 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2186 /* Stop the T5-shutdown guard timer. */
2187 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2188 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2190 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2194 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2196 * See sctp_sf_do_9_1_abort().
2198 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2199 struct net *net,
2200 const struct sctp_endpoint *ep,
2201 const struct sctp_association *asoc,
2202 const sctp_subtype_t type,
2203 void *arg,
2204 sctp_cmd_seq_t *commands)
2206 /* The same T2 timer, so we should be able to use
2207 * common function with the SHUTDOWN-SENT state.
2209 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2213 * Handle an Error received in COOKIE_ECHOED state.
2215 * Only handle the error type of stale COOKIE Error, the other errors will
2216 * be ignored.
2218 * Inputs
2219 * (endpoint, asoc, chunk)
2221 * Outputs
2222 * (asoc, reply_msg, msg_up, timers, counters)
2224 * The return value is the disposition of the chunk.
2226 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2227 const struct sctp_endpoint *ep,
2228 const struct sctp_association *asoc,
2229 const sctp_subtype_t type,
2230 void *arg,
2231 sctp_cmd_seq_t *commands)
2233 struct sctp_chunk *chunk = arg;
2234 sctp_errhdr_t *err;
2236 if (!sctp_vtag_verify(chunk, asoc))
2237 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2239 /* Make sure that the ERROR chunk has a valid length.
2240 * The parameter walking depends on this as well.
2242 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2243 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2244 commands);
2246 /* Process the error here */
2247 /* FUTURE FIXME: When PR-SCTP related and other optional
2248 * parms are emitted, this will have to change to handle multiple
2249 * errors.
2251 sctp_walk_errors(err, chunk->chunk_hdr) {
2252 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2253 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2254 arg, commands);
2257 /* It is possible to have malformed error causes, and that
2258 * will cause us to end the walk early. However, since
2259 * we are discarding the packet, there should be no adverse
2260 * affects.
2262 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2266 * Handle a Stale COOKIE Error
2268 * Section: 5.2.6 Handle Stale COOKIE Error
2269 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2270 * one of the following three alternatives.
2271 * ...
2272 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2273 * Preservative parameter requesting an extension to the lifetime of
2274 * the State Cookie. When calculating the time extension, an
2275 * implementation SHOULD use the RTT information measured based on the
2276 * previous COOKIE ECHO / ERROR exchange, and should add no more
2277 * than 1 second beyond the measured RTT, due to long State Cookie
2278 * lifetimes making the endpoint more subject to a replay attack.
2280 * Verification Tag: Not explicit, but safe to ignore.
2282 * Inputs
2283 * (endpoint, asoc, chunk)
2285 * Outputs
2286 * (asoc, reply_msg, msg_up, timers, counters)
2288 * The return value is the disposition of the chunk.
2290 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2291 const struct sctp_endpoint *ep,
2292 const struct sctp_association *asoc,
2293 const sctp_subtype_t type,
2294 void *arg,
2295 sctp_cmd_seq_t *commands)
2297 struct sctp_chunk *chunk = arg;
2298 time_t stale;
2299 sctp_cookie_preserve_param_t bht;
2300 sctp_errhdr_t *err;
2301 struct sctp_chunk *reply;
2302 struct sctp_bind_addr *bp;
2303 int attempts = asoc->init_err_counter + 1;
2305 if (attempts > asoc->max_init_attempts) {
2306 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2307 SCTP_ERROR(ETIMEDOUT));
2308 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2309 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2310 return SCTP_DISPOSITION_DELETE_TCB;
2313 err = (sctp_errhdr_t *)(chunk->skb->data);
2315 /* When calculating the time extension, an implementation
2316 * SHOULD use the RTT information measured based on the
2317 * previous COOKIE ECHO / ERROR exchange, and should add no
2318 * more than 1 second beyond the measured RTT, due to long
2319 * State Cookie lifetimes making the endpoint more subject to
2320 * a replay attack.
2321 * Measure of Staleness's unit is usec. (1/1000000 sec)
2322 * Suggested Cookie Life-span Increment's unit is msec.
2323 * (1/1000 sec)
2324 * In general, if you use the suggested cookie life, the value
2325 * found in the field of measure of staleness should be doubled
2326 * to give ample time to retransmit the new cookie and thus
2327 * yield a higher probability of success on the reattempt.
2329 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2330 stale = (stale * 2) / 1000;
2332 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2333 bht.param_hdr.length = htons(sizeof(bht));
2334 bht.lifespan_increment = htonl(stale);
2336 /* Build that new INIT chunk. */
2337 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2338 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2339 if (!reply)
2340 goto nomem;
2342 sctp_addto_chunk(reply, sizeof(bht), &bht);
2344 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2345 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2347 /* Stop pending T3-rtx and heartbeat timers */
2348 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2349 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2351 /* Delete non-primary peer ip addresses since we are transitioning
2352 * back to the COOKIE-WAIT state
2354 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2356 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2357 * resend
2359 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2360 SCTP_TRANSPORT(asoc->peer.primary_path));
2362 /* Cast away the const modifier, as we want to just
2363 * rerun it through as a sideffect.
2365 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2367 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2368 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2369 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2370 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2371 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2372 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2374 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2376 return SCTP_DISPOSITION_CONSUME;
2378 nomem:
2379 return SCTP_DISPOSITION_NOMEM;
2383 * Process an ABORT.
2385 * Section: 9.1
2386 * After checking the Verification Tag, the receiving endpoint shall
2387 * remove the association from its record, and shall report the
2388 * termination to its upper layer.
2390 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2391 * B) Rules for packet carrying ABORT:
2393 * - The endpoint shall always fill in the Verification Tag field of the
2394 * outbound packet with the destination endpoint's tag value if it
2395 * is known.
2397 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2398 * MUST follow the procedure described in Section 8.4.
2400 * - The receiver MUST accept the packet if the Verification Tag
2401 * matches either its own tag, OR the tag of its peer. Otherwise, the
2402 * receiver MUST silently discard the packet and take no further
2403 * action.
2405 * Inputs
2406 * (endpoint, asoc, chunk)
2408 * Outputs
2409 * (asoc, reply_msg, msg_up, timers, counters)
2411 * The return value is the disposition of the chunk.
2413 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2414 const struct sctp_endpoint *ep,
2415 const struct sctp_association *asoc,
2416 const sctp_subtype_t type,
2417 void *arg,
2418 sctp_cmd_seq_t *commands)
2420 struct sctp_chunk *chunk = arg;
2422 if (!sctp_vtag_verify_either(chunk, asoc))
2423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2425 /* Make sure that the ABORT chunk has a valid length.
2426 * Since this is an ABORT chunk, we have to discard it
2427 * because of the following text:
2428 * RFC 2960, Section 3.3.7
2429 * If an endpoint receives an ABORT with a format error or for an
2430 * association that doesn't exist, it MUST silently discard it.
2431 * Because the length is "invalid", we can't really discard just
2432 * as we do not know its true length. So, to be safe, discard the
2433 * packet.
2435 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2436 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2438 /* ADD-IP: Special case for ABORT chunks
2439 * F4) One special consideration is that ABORT Chunks arriving
2440 * destined to the IP address being deleted MUST be
2441 * ignored (see Section 5.3.1 for further details).
2443 if (SCTP_ADDR_DEL ==
2444 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2445 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2447 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2450 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2451 const struct sctp_endpoint *ep,
2452 const struct sctp_association *asoc,
2453 const sctp_subtype_t type,
2454 void *arg,
2455 sctp_cmd_seq_t *commands)
2457 struct sctp_chunk *chunk = arg;
2458 unsigned int len;
2459 __be16 error = SCTP_ERROR_NO_ERROR;
2461 /* See if we have an error cause code in the chunk. */
2462 len = ntohs(chunk->chunk_hdr->length);
2463 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2465 sctp_errhdr_t *err;
2466 sctp_walk_errors(err, chunk->chunk_hdr);
2467 if ((void *)err != (void *)chunk->chunk_end)
2468 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2470 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2473 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2474 /* ASSOC_FAILED will DELETE_TCB. */
2475 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2476 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2477 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2479 return SCTP_DISPOSITION_ABORT;
2483 * Process an ABORT. (COOKIE-WAIT state)
2485 * See sctp_sf_do_9_1_abort() above.
2487 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2488 const struct sctp_endpoint *ep,
2489 const struct sctp_association *asoc,
2490 const sctp_subtype_t type,
2491 void *arg,
2492 sctp_cmd_seq_t *commands)
2494 struct sctp_chunk *chunk = arg;
2495 unsigned int len;
2496 __be16 error = SCTP_ERROR_NO_ERROR;
2498 if (!sctp_vtag_verify_either(chunk, asoc))
2499 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2501 /* Make sure that the ABORT chunk has a valid length.
2502 * Since this is an ABORT chunk, we have to discard it
2503 * because of the following text:
2504 * RFC 2960, Section 3.3.7
2505 * If an endpoint receives an ABORT with a format error or for an
2506 * association that doesn't exist, it MUST silently discard it.
2507 * Because the length is "invalid", we can't really discard just
2508 * as we do not know its true length. So, to be safe, discard the
2509 * packet.
2511 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2512 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2514 /* See if we have an error cause code in the chunk. */
2515 len = ntohs(chunk->chunk_hdr->length);
2516 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2517 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2519 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2520 chunk->transport);
2524 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2526 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2527 const struct sctp_endpoint *ep,
2528 const struct sctp_association *asoc,
2529 const sctp_subtype_t type,
2530 void *arg,
2531 sctp_cmd_seq_t *commands)
2533 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2534 ENOPROTOOPT, asoc,
2535 (struct sctp_transport *)arg);
2539 * Process an ABORT. (COOKIE-ECHOED state)
2541 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2542 const struct sctp_endpoint *ep,
2543 const struct sctp_association *asoc,
2544 const sctp_subtype_t type,
2545 void *arg,
2546 sctp_cmd_seq_t *commands)
2548 /* There is a single T1 timer, so we should be able to use
2549 * common function with the COOKIE-WAIT state.
2551 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2555 * Stop T1 timer and abort association with "INIT failed".
2557 * This is common code called by several sctp_sf_*_abort() functions above.
2559 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2560 sctp_cmd_seq_t *commands,
2561 __be16 error, int sk_err,
2562 const struct sctp_association *asoc,
2563 struct sctp_transport *transport)
2565 pr_debug("%s: ABORT received (INIT)\n", __func__);
2567 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2568 SCTP_STATE(SCTP_STATE_CLOSED));
2569 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2570 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2571 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2572 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2573 /* CMD_INIT_FAILED will DELETE_TCB. */
2574 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2575 SCTP_PERR(error));
2577 return SCTP_DISPOSITION_ABORT;
2581 * sctp_sf_do_9_2_shut
2583 * Section: 9.2
2584 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2585 * - enter the SHUTDOWN-RECEIVED state,
2587 * - stop accepting new data from its SCTP user
2589 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2590 * that all its outstanding DATA chunks have been received by the
2591 * SHUTDOWN sender.
2593 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2594 * send a SHUTDOWN in response to a ULP request. And should discard
2595 * subsequent SHUTDOWN chunks.
2597 * If there are still outstanding DATA chunks left, the SHUTDOWN
2598 * receiver shall continue to follow normal data transmission
2599 * procedures defined in Section 6 until all outstanding DATA chunks
2600 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2601 * new data from its SCTP user.
2603 * Verification Tag: 8.5 Verification Tag [Normal verification]
2605 * Inputs
2606 * (endpoint, asoc, chunk)
2608 * Outputs
2609 * (asoc, reply_msg, msg_up, timers, counters)
2611 * The return value is the disposition of the chunk.
2613 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2614 const struct sctp_endpoint *ep,
2615 const struct sctp_association *asoc,
2616 const sctp_subtype_t type,
2617 void *arg,
2618 sctp_cmd_seq_t *commands)
2620 struct sctp_chunk *chunk = arg;
2621 sctp_shutdownhdr_t *sdh;
2622 sctp_disposition_t disposition;
2623 struct sctp_ulpevent *ev;
2624 __u32 ctsn;
2626 if (!sctp_vtag_verify(chunk, asoc))
2627 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2629 /* Make sure that the SHUTDOWN chunk has a valid length. */
2630 if (!sctp_chunk_length_valid(chunk,
2631 sizeof(struct sctp_shutdown_chunk_t)))
2632 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2633 commands);
2635 /* Convert the elaborate header. */
2636 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2637 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2638 chunk->subh.shutdown_hdr = sdh;
2639 ctsn = ntohl(sdh->cum_tsn_ack);
2641 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2642 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2643 asoc->ctsn_ack_point);
2645 return SCTP_DISPOSITION_DISCARD;
2648 /* If Cumulative TSN Ack beyond the max tsn currently
2649 * send, terminating the association and respond to the
2650 * sender with an ABORT.
2652 if (!TSN_lt(ctsn, asoc->next_tsn))
2653 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2655 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2656 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2657 * inform the application that it should cease sending data.
2659 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2660 if (!ev) {
2661 disposition = SCTP_DISPOSITION_NOMEM;
2662 goto out;
2664 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2666 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2667 * - enter the SHUTDOWN-RECEIVED state,
2668 * - stop accepting new data from its SCTP user
2670 * [This is implicit in the new state.]
2672 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2673 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2674 disposition = SCTP_DISPOSITION_CONSUME;
2676 if (sctp_outq_is_empty(&asoc->outqueue)) {
2677 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2678 arg, commands);
2681 if (SCTP_DISPOSITION_NOMEM == disposition)
2682 goto out;
2684 /* - verify, by checking the Cumulative TSN Ack field of the
2685 * chunk, that all its outstanding DATA chunks have been
2686 * received by the SHUTDOWN sender.
2688 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2689 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2691 out:
2692 return disposition;
2696 * sctp_sf_do_9_2_shut_ctsn
2698 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2699 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2700 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2701 * MUST be processed.
2703 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2704 const struct sctp_endpoint *ep,
2705 const struct sctp_association *asoc,
2706 const sctp_subtype_t type,
2707 void *arg,
2708 sctp_cmd_seq_t *commands)
2710 struct sctp_chunk *chunk = arg;
2711 sctp_shutdownhdr_t *sdh;
2712 __u32 ctsn;
2714 if (!sctp_vtag_verify(chunk, asoc))
2715 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2717 /* Make sure that the SHUTDOWN chunk has a valid length. */
2718 if (!sctp_chunk_length_valid(chunk,
2719 sizeof(struct sctp_shutdown_chunk_t)))
2720 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2721 commands);
2723 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2724 ctsn = ntohl(sdh->cum_tsn_ack);
2726 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2727 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2728 asoc->ctsn_ack_point);
2730 return SCTP_DISPOSITION_DISCARD;
2733 /* If Cumulative TSN Ack beyond the max tsn currently
2734 * send, terminating the association and respond to the
2735 * sender with an ABORT.
2737 if (!TSN_lt(ctsn, asoc->next_tsn))
2738 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2740 /* verify, by checking the Cumulative TSN Ack field of the
2741 * chunk, that all its outstanding DATA chunks have been
2742 * received by the SHUTDOWN sender.
2744 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2745 SCTP_BE32(sdh->cum_tsn_ack));
2747 return SCTP_DISPOSITION_CONSUME;
2750 /* RFC 2960 9.2
2751 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2752 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2753 * transport addresses (either in the IP addresses or in the INIT chunk)
2754 * that belong to this association, it should discard the INIT chunk and
2755 * retransmit the SHUTDOWN ACK chunk.
2757 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2758 const struct sctp_endpoint *ep,
2759 const struct sctp_association *asoc,
2760 const sctp_subtype_t type,
2761 void *arg,
2762 sctp_cmd_seq_t *commands)
2764 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2765 struct sctp_chunk *reply;
2767 /* Make sure that the chunk has a valid length */
2768 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2769 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2770 commands);
2772 /* Since we are not going to really process this INIT, there
2773 * is no point in verifying chunk boundries. Just generate
2774 * the SHUTDOWN ACK.
2776 reply = sctp_make_shutdown_ack(asoc, chunk);
2777 if (NULL == reply)
2778 goto nomem;
2780 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2781 * the T2-SHUTDOWN timer.
2783 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2785 /* and restart the T2-shutdown timer. */
2786 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2787 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2791 return SCTP_DISPOSITION_CONSUME;
2792 nomem:
2793 return SCTP_DISPOSITION_NOMEM;
2797 * sctp_sf_do_ecn_cwr
2799 * Section: Appendix A: Explicit Congestion Notification
2801 * CWR:
2803 * RFC 2481 details a specific bit for a sender to send in the header of
2804 * its next outbound TCP segment to indicate to its peer that it has
2805 * reduced its congestion window. This is termed the CWR bit. For
2806 * SCTP the same indication is made by including the CWR chunk.
2807 * This chunk contains one data element, i.e. the TSN number that
2808 * was sent in the ECNE chunk. This element represents the lowest
2809 * TSN number in the datagram that was originally marked with the
2810 * CE bit.
2812 * Verification Tag: 8.5 Verification Tag [Normal verification]
2813 * Inputs
2814 * (endpoint, asoc, chunk)
2816 * Outputs
2817 * (asoc, reply_msg, msg_up, timers, counters)
2819 * The return value is the disposition of the chunk.
2821 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2822 const struct sctp_endpoint *ep,
2823 const struct sctp_association *asoc,
2824 const sctp_subtype_t type,
2825 void *arg,
2826 sctp_cmd_seq_t *commands)
2828 sctp_cwrhdr_t *cwr;
2829 struct sctp_chunk *chunk = arg;
2830 u32 lowest_tsn;
2832 if (!sctp_vtag_verify(chunk, asoc))
2833 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2835 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2836 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2837 commands);
2839 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2840 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2842 lowest_tsn = ntohl(cwr->lowest_tsn);
2844 /* Does this CWR ack the last sent congestion notification? */
2845 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2846 /* Stop sending ECNE. */
2847 sctp_add_cmd_sf(commands,
2848 SCTP_CMD_ECN_CWR,
2849 SCTP_U32(lowest_tsn));
2851 return SCTP_DISPOSITION_CONSUME;
2855 * sctp_sf_do_ecne
2857 * Section: Appendix A: Explicit Congestion Notification
2859 * ECN-Echo
2861 * RFC 2481 details a specific bit for a receiver to send back in its
2862 * TCP acknowledgements to notify the sender of the Congestion
2863 * Experienced (CE) bit having arrived from the network. For SCTP this
2864 * same indication is made by including the ECNE chunk. This chunk
2865 * contains one data element, i.e. the lowest TSN associated with the IP
2866 * datagram marked with the CE bit.....
2868 * Verification Tag: 8.5 Verification Tag [Normal verification]
2869 * Inputs
2870 * (endpoint, asoc, chunk)
2872 * Outputs
2873 * (asoc, reply_msg, msg_up, timers, counters)
2875 * The return value is the disposition of the chunk.
2877 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2878 const struct sctp_endpoint *ep,
2879 const struct sctp_association *asoc,
2880 const sctp_subtype_t type,
2881 void *arg,
2882 sctp_cmd_seq_t *commands)
2884 sctp_ecnehdr_t *ecne;
2885 struct sctp_chunk *chunk = arg;
2887 if (!sctp_vtag_verify(chunk, asoc))
2888 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2890 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2891 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2892 commands);
2894 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2895 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2897 /* If this is a newer ECNE than the last CWR packet we sent out */
2898 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2899 SCTP_U32(ntohl(ecne->lowest_tsn)));
2901 return SCTP_DISPOSITION_CONSUME;
2905 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2907 * The SCTP endpoint MUST always acknowledge the reception of each valid
2908 * DATA chunk.
2910 * The guidelines on delayed acknowledgement algorithm specified in
2911 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2912 * acknowledgement SHOULD be generated for at least every second packet
2913 * (not every second DATA chunk) received, and SHOULD be generated within
2914 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2915 * situations it may be beneficial for an SCTP transmitter to be more
2916 * conservative than the algorithms detailed in this document allow.
2917 * However, an SCTP transmitter MUST NOT be more aggressive than the
2918 * following algorithms allow.
2920 * A SCTP receiver MUST NOT generate more than one SACK for every
2921 * incoming packet, other than to update the offered window as the
2922 * receiving application consumes new data.
2924 * Verification Tag: 8.5 Verification Tag [Normal verification]
2926 * Inputs
2927 * (endpoint, asoc, chunk)
2929 * Outputs
2930 * (asoc, reply_msg, msg_up, timers, counters)
2932 * The return value is the disposition of the chunk.
2934 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2935 const struct sctp_endpoint *ep,
2936 const struct sctp_association *asoc,
2937 const sctp_subtype_t type,
2938 void *arg,
2939 sctp_cmd_seq_t *commands)
2941 struct sctp_chunk *chunk = arg;
2942 sctp_arg_t force = SCTP_NOFORCE();
2943 int error;
2945 if (!sctp_vtag_verify(chunk, asoc)) {
2946 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2947 SCTP_NULL());
2948 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2951 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2952 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2953 commands);
2955 error = sctp_eat_data(asoc, chunk, commands );
2956 switch (error) {
2957 case SCTP_IERROR_NO_ERROR:
2958 break;
2959 case SCTP_IERROR_HIGH_TSN:
2960 case SCTP_IERROR_BAD_STREAM:
2961 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2962 goto discard_noforce;
2963 case SCTP_IERROR_DUP_TSN:
2964 case SCTP_IERROR_IGNORE_TSN:
2965 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2966 goto discard_force;
2967 case SCTP_IERROR_NO_DATA:
2968 goto consume;
2969 case SCTP_IERROR_PROTO_VIOLATION:
2970 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
2971 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2972 default:
2973 BUG();
2976 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2977 force = SCTP_FORCE();
2979 if (asoc->autoclose) {
2980 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2981 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2984 /* If this is the last chunk in a packet, we need to count it
2985 * toward sack generation. Note that we need to SACK every
2986 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2987 * THEM. We elect to NOT generate SACK's if the chunk fails
2988 * the verification tag test.
2990 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2992 * The SCTP endpoint MUST always acknowledge the reception of
2993 * each valid DATA chunk.
2995 * The guidelines on delayed acknowledgement algorithm
2996 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2997 * Specifically, an acknowledgement SHOULD be generated for at
2998 * least every second packet (not every second DATA chunk)
2999 * received, and SHOULD be generated within 200 ms of the
3000 * arrival of any unacknowledged DATA chunk. In some
3001 * situations it may be beneficial for an SCTP transmitter to
3002 * be more conservative than the algorithms detailed in this
3003 * document allow. However, an SCTP transmitter MUST NOT be
3004 * more aggressive than the following algorithms allow.
3006 if (chunk->end_of_packet)
3007 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3009 return SCTP_DISPOSITION_CONSUME;
3011 discard_force:
3012 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3014 * When a packet arrives with duplicate DATA chunk(s) and with
3015 * no new DATA chunk(s), the endpoint MUST immediately send a
3016 * SACK with no delay. If a packet arrives with duplicate
3017 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3018 * MAY immediately send a SACK. Normally receipt of duplicate
3019 * DATA chunks will occur when the original SACK chunk was lost
3020 * and the peer's RTO has expired. The duplicate TSN number(s)
3021 * SHOULD be reported in the SACK as duplicate.
3023 /* In our case, we split the MAY SACK advice up whether or not
3024 * the last chunk is a duplicate.'
3026 if (chunk->end_of_packet)
3027 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3028 return SCTP_DISPOSITION_DISCARD;
3030 discard_noforce:
3031 if (chunk->end_of_packet)
3032 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3034 return SCTP_DISPOSITION_DISCARD;
3035 consume:
3036 return SCTP_DISPOSITION_CONSUME;
3041 * sctp_sf_eat_data_fast_4_4
3043 * Section: 4 (4)
3044 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3045 * DATA chunks without delay.
3047 * Verification Tag: 8.5 Verification Tag [Normal verification]
3048 * Inputs
3049 * (endpoint, asoc, chunk)
3051 * Outputs
3052 * (asoc, reply_msg, msg_up, timers, counters)
3054 * The return value is the disposition of the chunk.
3056 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3057 const struct sctp_endpoint *ep,
3058 const struct sctp_association *asoc,
3059 const sctp_subtype_t type,
3060 void *arg,
3061 sctp_cmd_seq_t *commands)
3063 struct sctp_chunk *chunk = arg;
3064 int error;
3066 if (!sctp_vtag_verify(chunk, asoc)) {
3067 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3068 SCTP_NULL());
3069 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3072 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3073 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3074 commands);
3076 error = sctp_eat_data(asoc, chunk, commands );
3077 switch (error) {
3078 case SCTP_IERROR_NO_ERROR:
3079 case SCTP_IERROR_HIGH_TSN:
3080 case SCTP_IERROR_DUP_TSN:
3081 case SCTP_IERROR_IGNORE_TSN:
3082 case SCTP_IERROR_BAD_STREAM:
3083 break;
3084 case SCTP_IERROR_NO_DATA:
3085 goto consume;
3086 case SCTP_IERROR_PROTO_VIOLATION:
3087 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3088 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3089 default:
3090 BUG();
3093 /* Go a head and force a SACK, since we are shutting down. */
3095 /* Implementor's Guide.
3097 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3098 * respond to each received packet containing one or more DATA chunk(s)
3099 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3101 if (chunk->end_of_packet) {
3102 /* We must delay the chunk creation since the cumulative
3103 * TSN has not been updated yet.
3105 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3106 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3107 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3108 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3111 consume:
3112 return SCTP_DISPOSITION_CONSUME;
3116 * Section: 6.2 Processing a Received SACK
3117 * D) Any time a SACK arrives, the endpoint performs the following:
3119 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3120 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3121 * increasing, a SACK whose Cumulative TSN Ack is less than the
3122 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3124 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3125 * of bytes still outstanding after processing the Cumulative TSN Ack
3126 * and the Gap Ack Blocks.
3128 * iii) If the SACK is missing a TSN that was previously
3129 * acknowledged via a Gap Ack Block (e.g., the data receiver
3130 * reneged on the data), then mark the corresponding DATA chunk
3131 * as available for retransmit: Mark it as missing for fast
3132 * retransmit as described in Section 7.2.4 and if no retransmit
3133 * timer is running for the destination address to which the DATA
3134 * chunk was originally transmitted, then T3-rtx is started for
3135 * that destination address.
3137 * Verification Tag: 8.5 Verification Tag [Normal verification]
3139 * Inputs
3140 * (endpoint, asoc, chunk)
3142 * Outputs
3143 * (asoc, reply_msg, msg_up, timers, counters)
3145 * The return value is the disposition of the chunk.
3147 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3148 const struct sctp_endpoint *ep,
3149 const struct sctp_association *asoc,
3150 const sctp_subtype_t type,
3151 void *arg,
3152 sctp_cmd_seq_t *commands)
3154 struct sctp_chunk *chunk = arg;
3155 sctp_sackhdr_t *sackh;
3156 __u32 ctsn;
3158 if (!sctp_vtag_verify(chunk, asoc))
3159 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3161 /* Make sure that the SACK chunk has a valid length. */
3162 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3163 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3164 commands);
3166 /* Pull the SACK chunk from the data buffer */
3167 sackh = sctp_sm_pull_sack(chunk);
3168 /* Was this a bogus SACK? */
3169 if (!sackh)
3170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3171 chunk->subh.sack_hdr = sackh;
3172 ctsn = ntohl(sackh->cum_tsn_ack);
3174 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3175 * Ack Point, then drop the SACK. Since Cumulative TSN
3176 * Ack is monotonically increasing, a SACK whose
3177 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3178 * Point indicates an out-of-order SACK.
3180 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3181 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3182 asoc->ctsn_ack_point);
3184 return SCTP_DISPOSITION_DISCARD;
3187 /* If Cumulative TSN Ack beyond the max tsn currently
3188 * send, terminating the association and respond to the
3189 * sender with an ABORT.
3191 if (!TSN_lt(ctsn, asoc->next_tsn))
3192 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3194 /* Return this SACK for further processing. */
3195 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3197 /* Note: We do the rest of the work on the PROCESS_SACK
3198 * sideeffect.
3200 return SCTP_DISPOSITION_CONSUME;
3204 * Generate an ABORT in response to a packet.
3206 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3208 * 8) The receiver should respond to the sender of the OOTB packet with
3209 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3210 * MUST fill in the Verification Tag field of the outbound packet
3211 * with the value found in the Verification Tag field of the OOTB
3212 * packet and set the T-bit in the Chunk Flags to indicate that the
3213 * Verification Tag is reflected. After sending this ABORT, the
3214 * receiver of the OOTB packet shall discard the OOTB packet and take
3215 * no further action.
3217 * Verification Tag:
3219 * The return value is the disposition of the chunk.
3221 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3222 const struct sctp_endpoint *ep,
3223 const struct sctp_association *asoc,
3224 const sctp_subtype_t type,
3225 void *arg,
3226 sctp_cmd_seq_t *commands)
3228 struct sctp_packet *packet = NULL;
3229 struct sctp_chunk *chunk = arg;
3230 struct sctp_chunk *abort;
3232 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3234 if (packet) {
3235 /* Make an ABORT. The T bit will be set if the asoc
3236 * is NULL.
3238 abort = sctp_make_abort(asoc, chunk, 0);
3239 if (!abort) {
3240 sctp_ootb_pkt_free(packet);
3241 return SCTP_DISPOSITION_NOMEM;
3244 /* Reflect vtag if T-Bit is set */
3245 if (sctp_test_T_bit(abort))
3246 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3248 /* Set the skb to the belonging sock for accounting. */
3249 abort->skb->sk = ep->base.sk;
3251 sctp_packet_append_chunk(packet, abort);
3253 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3254 SCTP_PACKET(packet));
3256 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3258 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3259 return SCTP_DISPOSITION_CONSUME;
3262 return SCTP_DISPOSITION_NOMEM;
3266 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3267 * event as ULP notification for each cause included in the chunk.
3269 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3271 * The return value is the disposition of the chunk.
3273 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3274 const struct sctp_endpoint *ep,
3275 const struct sctp_association *asoc,
3276 const sctp_subtype_t type,
3277 void *arg,
3278 sctp_cmd_seq_t *commands)
3280 struct sctp_chunk *chunk = arg;
3281 sctp_errhdr_t *err;
3283 if (!sctp_vtag_verify(chunk, asoc))
3284 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3286 /* Make sure that the ERROR chunk has a valid length. */
3287 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3288 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3289 commands);
3290 sctp_walk_errors(err, chunk->chunk_hdr);
3291 if ((void *)err != (void *)chunk->chunk_end)
3292 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3293 (void *)err, commands);
3295 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3296 SCTP_CHUNK(chunk));
3298 return SCTP_DISPOSITION_CONSUME;
3302 * Process an inbound SHUTDOWN ACK.
3304 * From Section 9.2:
3305 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3306 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3307 * peer, and remove all record of the association.
3309 * The return value is the disposition.
3311 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3312 const struct sctp_endpoint *ep,
3313 const struct sctp_association *asoc,
3314 const sctp_subtype_t type,
3315 void *arg,
3316 sctp_cmd_seq_t *commands)
3318 struct sctp_chunk *chunk = arg;
3319 struct sctp_chunk *reply;
3320 struct sctp_ulpevent *ev;
3322 if (!sctp_vtag_verify(chunk, asoc))
3323 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3325 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3326 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3327 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3328 commands);
3329 /* 10.2 H) SHUTDOWN COMPLETE notification
3331 * When SCTP completes the shutdown procedures (section 9.2) this
3332 * notification is passed to the upper layer.
3334 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3335 0, 0, 0, NULL, GFP_ATOMIC);
3336 if (!ev)
3337 goto nomem;
3339 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3340 reply = sctp_make_shutdown_complete(asoc, chunk);
3341 if (!reply)
3342 goto nomem_chunk;
3344 /* Do all the commands now (after allocation), so that we
3345 * have consistent state if memory allocation failes
3347 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3349 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3350 * stop the T2-shutdown timer,
3352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3353 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3355 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3356 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3358 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3359 SCTP_STATE(SCTP_STATE_CLOSED));
3360 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3361 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3362 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3364 /* ...and remove all record of the association. */
3365 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3366 return SCTP_DISPOSITION_DELETE_TCB;
3368 nomem_chunk:
3369 sctp_ulpevent_free(ev);
3370 nomem:
3371 return SCTP_DISPOSITION_NOMEM;
3375 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3377 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3378 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3379 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3380 * packet must fill in the Verification Tag field of the outbound
3381 * packet with the Verification Tag received in the SHUTDOWN ACK and
3382 * set the T-bit in the Chunk Flags to indicate that the Verification
3383 * Tag is reflected.
3385 * 8) The receiver should respond to the sender of the OOTB packet with
3386 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3387 * MUST fill in the Verification Tag field of the outbound packet
3388 * with the value found in the Verification Tag field of the OOTB
3389 * packet and set the T-bit in the Chunk Flags to indicate that the
3390 * Verification Tag is reflected. After sending this ABORT, the
3391 * receiver of the OOTB packet shall discard the OOTB packet and take
3392 * no further action.
3394 sctp_disposition_t sctp_sf_ootb(struct net *net,
3395 const struct sctp_endpoint *ep,
3396 const struct sctp_association *asoc,
3397 const sctp_subtype_t type,
3398 void *arg,
3399 sctp_cmd_seq_t *commands)
3401 struct sctp_chunk *chunk = arg;
3402 struct sk_buff *skb = chunk->skb;
3403 sctp_chunkhdr_t *ch;
3404 sctp_errhdr_t *err;
3405 __u8 *ch_end;
3406 int ootb_shut_ack = 0;
3407 int ootb_cookie_ack = 0;
3409 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3411 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3412 do {
3413 /* Report violation if the chunk is less then minimal */
3414 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3415 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3416 commands);
3418 /* Now that we know we at least have a chunk header,
3419 * do things that are type appropriate.
3421 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3422 ootb_shut_ack = 1;
3424 /* RFC 2960, Section 3.3.7
3425 * Moreover, under any circumstances, an endpoint that
3426 * receives an ABORT MUST NOT respond to that ABORT by
3427 * sending an ABORT of its own.
3429 if (SCTP_CID_ABORT == ch->type)
3430 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3432 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3433 * or a COOKIE ACK the SCTP Packet should be silently
3434 * discarded.
3437 if (SCTP_CID_COOKIE_ACK == ch->type)
3438 ootb_cookie_ack = 1;
3440 if (SCTP_CID_ERROR == ch->type) {
3441 sctp_walk_errors(err, ch) {
3442 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3443 ootb_cookie_ack = 1;
3444 break;
3449 /* Report violation if chunk len overflows */
3450 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3451 if (ch_end > skb_tail_pointer(skb))
3452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3453 commands);
3455 ch = (sctp_chunkhdr_t *) ch_end;
3456 } while (ch_end < skb_tail_pointer(skb));
3458 if (ootb_shut_ack)
3459 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3460 else if (ootb_cookie_ack)
3461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3462 else
3463 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3467 * Handle an "Out of the blue" SHUTDOWN ACK.
3469 * Section: 8.4 5, sctpimpguide 2.41.
3471 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3472 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3473 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3474 * packet must fill in the Verification Tag field of the outbound
3475 * packet with the Verification Tag received in the SHUTDOWN ACK and
3476 * set the T-bit in the Chunk Flags to indicate that the Verification
3477 * Tag is reflected.
3479 * Inputs
3480 * (endpoint, asoc, type, arg, commands)
3482 * Outputs
3483 * (sctp_disposition_t)
3485 * The return value is the disposition of the chunk.
3487 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3488 const struct sctp_endpoint *ep,
3489 const struct sctp_association *asoc,
3490 const sctp_subtype_t type,
3491 void *arg,
3492 sctp_cmd_seq_t *commands)
3494 struct sctp_packet *packet = NULL;
3495 struct sctp_chunk *chunk = arg;
3496 struct sctp_chunk *shut;
3498 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3500 if (packet) {
3501 /* Make an SHUTDOWN_COMPLETE.
3502 * The T bit will be set if the asoc is NULL.
3504 shut = sctp_make_shutdown_complete(asoc, chunk);
3505 if (!shut) {
3506 sctp_ootb_pkt_free(packet);
3507 return SCTP_DISPOSITION_NOMEM;
3510 /* Reflect vtag if T-Bit is set */
3511 if (sctp_test_T_bit(shut))
3512 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3514 /* Set the skb to the belonging sock for accounting. */
3515 shut->skb->sk = ep->base.sk;
3517 sctp_packet_append_chunk(packet, shut);
3519 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3520 SCTP_PACKET(packet));
3522 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3524 /* If the chunk length is invalid, we don't want to process
3525 * the reset of the packet.
3527 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3528 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3530 /* We need to discard the rest of the packet to prevent
3531 * potential bomming attacks from additional bundled chunks.
3532 * This is documented in SCTP Threats ID.
3534 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3537 return SCTP_DISPOSITION_NOMEM;
3541 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3543 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3544 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3545 * procedures in section 8.4 SHOULD be followed, in other words it
3546 * should be treated as an Out Of The Blue packet.
3547 * [This means that we do NOT check the Verification Tag on these
3548 * chunks. --piggy ]
3551 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3552 const struct sctp_endpoint *ep,
3553 const struct sctp_association *asoc,
3554 const sctp_subtype_t type,
3555 void *arg,
3556 sctp_cmd_seq_t *commands)
3558 struct sctp_chunk *chunk = arg;
3560 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3561 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3562 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3563 commands);
3565 /* Although we do have an association in this case, it corresponds
3566 * to a restarted association. So the packet is treated as an OOTB
3567 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3568 * called with a NULL association.
3570 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3572 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3575 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3576 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3577 const struct sctp_endpoint *ep,
3578 const struct sctp_association *asoc,
3579 const sctp_subtype_t type, void *arg,
3580 sctp_cmd_seq_t *commands)
3582 struct sctp_chunk *chunk = arg;
3583 struct sctp_chunk *asconf_ack = NULL;
3584 struct sctp_paramhdr *err_param = NULL;
3585 sctp_addiphdr_t *hdr;
3586 union sctp_addr_param *addr_param;
3587 __u32 serial;
3588 int length;
3590 if (!sctp_vtag_verify(chunk, asoc)) {
3591 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3592 SCTP_NULL());
3593 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3596 /* ADD-IP: Section 4.1.1
3597 * This chunk MUST be sent in an authenticated way by using
3598 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3599 * is received unauthenticated it MUST be silently discarded as
3600 * described in [I-D.ietf-tsvwg-sctp-auth].
3602 if (!net->sctp.addip_noauth && !chunk->auth)
3603 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3605 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3606 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3607 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3608 commands);
3610 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3611 serial = ntohl(hdr->serial);
3613 addr_param = (union sctp_addr_param *)hdr->params;
3614 length = ntohs(addr_param->p.length);
3615 if (length < sizeof(sctp_paramhdr_t))
3616 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3617 (void *)addr_param, commands);
3619 /* Verify the ASCONF chunk before processing it. */
3620 if (!sctp_verify_asconf(asoc,
3621 (sctp_paramhdr_t *)((void *)addr_param + length),
3622 (void *)chunk->chunk_end,
3623 &err_param))
3624 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3625 (void *)err_param, commands);
3627 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3628 * the endpoint stored in a new association variable
3629 * 'Peer-Serial-Number'.
3631 if (serial == asoc->peer.addip_serial + 1) {
3632 /* If this is the first instance of ASCONF in the packet,
3633 * we can clean our old ASCONF-ACKs.
3635 if (!chunk->has_asconf)
3636 sctp_assoc_clean_asconf_ack_cache(asoc);
3638 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3639 * expected, process the ASCONF as described below and after
3640 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3641 * the response packet and cache a copy of it (in the event it
3642 * later needs to be retransmitted).
3644 * Essentially, do V1-V5.
3646 asconf_ack = sctp_process_asconf((struct sctp_association *)
3647 asoc, chunk);
3648 if (!asconf_ack)
3649 return SCTP_DISPOSITION_NOMEM;
3650 } else if (serial < asoc->peer.addip_serial + 1) {
3651 /* ADDIP 5.2 E2)
3652 * If the value found in the Sequence Number is less than the
3653 * ('Peer- Sequence-Number' + 1), simply skip to the next
3654 * ASCONF, and include in the outbound response packet
3655 * any previously cached ASCONF-ACK response that was
3656 * sent and saved that matches the Sequence Number of the
3657 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3658 * Chunk exists. This will occur when an older ASCONF
3659 * arrives out of order. In such a case, the receiver
3660 * should skip the ASCONF Chunk and not include ASCONF-ACK
3661 * Chunk for that chunk.
3663 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3664 if (!asconf_ack)
3665 return SCTP_DISPOSITION_DISCARD;
3667 /* Reset the transport so that we select the correct one
3668 * this time around. This is to make sure that we don't
3669 * accidentally use a stale transport that's been removed.
3671 asconf_ack->transport = NULL;
3672 } else {
3673 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3674 * it must be either a stale packet or from an attacker.
3676 return SCTP_DISPOSITION_DISCARD;
3679 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3680 * containing the ASCONF-ACK Chunks MUST be the source address of
3681 * the SCTP packet that held the ASCONF Chunks.
3683 * To do this properly, we'll set the destination address of the chunk
3684 * and at the transmit time, will try look up the transport to use.
3685 * Since ASCONFs may be bundled, the correct transport may not be
3686 * created until we process the entire packet, thus this workaround.
3688 asconf_ack->dest = chunk->source;
3689 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3690 if (asoc->new_transport) {
3691 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3692 commands);
3693 ((struct sctp_association *)asoc)->new_transport = NULL;
3696 return SCTP_DISPOSITION_CONSUME;
3700 * ADDIP Section 4.3 General rules for address manipulation
3701 * When building TLV parameters for the ASCONF Chunk that will add or
3702 * delete IP addresses the D0 to D13 rules should be applied:
3704 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3705 const struct sctp_endpoint *ep,
3706 const struct sctp_association *asoc,
3707 const sctp_subtype_t type, void *arg,
3708 sctp_cmd_seq_t *commands)
3710 struct sctp_chunk *asconf_ack = arg;
3711 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3712 struct sctp_chunk *abort;
3713 struct sctp_paramhdr *err_param = NULL;
3714 sctp_addiphdr_t *addip_hdr;
3715 __u32 sent_serial, rcvd_serial;
3717 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3718 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3719 SCTP_NULL());
3720 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3723 /* ADD-IP, Section 4.1.2:
3724 * This chunk MUST be sent in an authenticated way by using
3725 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3726 * is received unauthenticated it MUST be silently discarded as
3727 * described in [I-D.ietf-tsvwg-sctp-auth].
3729 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3730 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3732 /* Make sure that the ADDIP chunk has a valid length. */
3733 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3734 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3735 commands);
3737 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3738 rcvd_serial = ntohl(addip_hdr->serial);
3740 /* Verify the ASCONF-ACK chunk before processing it. */
3741 if (!sctp_verify_asconf(asoc,
3742 (sctp_paramhdr_t *)addip_hdr->params,
3743 (void *)asconf_ack->chunk_end,
3744 &err_param))
3745 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3746 (void *)err_param, commands);
3748 if (last_asconf) {
3749 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3750 sent_serial = ntohl(addip_hdr->serial);
3751 } else {
3752 sent_serial = asoc->addip_serial - 1;
3755 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3756 * equal to the next serial number to be used but no ASCONF chunk is
3757 * outstanding the endpoint MUST ABORT the association. Note that a
3758 * sequence number is greater than if it is no more than 2^^31-1
3759 * larger than the current sequence number (using serial arithmetic).
3761 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3762 !(asoc->addip_last_asconf)) {
3763 abort = sctp_make_abort(asoc, asconf_ack,
3764 sizeof(sctp_errhdr_t));
3765 if (abort) {
3766 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3767 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3768 SCTP_CHUNK(abort));
3770 /* We are going to ABORT, so we might as well stop
3771 * processing the rest of the chunks in the packet.
3773 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3774 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3775 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3776 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3777 SCTP_ERROR(ECONNABORTED));
3778 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3779 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3780 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3781 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3782 return SCTP_DISPOSITION_ABORT;
3785 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3786 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3787 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3789 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3790 asconf_ack)) {
3791 /* Successfully processed ASCONF_ACK. We can
3792 * release the next asconf if we have one.
3794 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3795 SCTP_NULL());
3796 return SCTP_DISPOSITION_CONSUME;
3799 abort = sctp_make_abort(asoc, asconf_ack,
3800 sizeof(sctp_errhdr_t));
3801 if (abort) {
3802 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3803 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3804 SCTP_CHUNK(abort));
3806 /* We are going to ABORT, so we might as well stop
3807 * processing the rest of the chunks in the packet.
3809 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3810 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3811 SCTP_ERROR(ECONNABORTED));
3812 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3813 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3814 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3815 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3816 return SCTP_DISPOSITION_ABORT;
3819 return SCTP_DISPOSITION_DISCARD;
3823 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3825 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3826 * its cumulative TSN point to the value carried in the FORWARD TSN
3827 * chunk, and then MUST further advance its cumulative TSN point locally
3828 * if possible.
3829 * After the above processing, the data receiver MUST stop reporting any
3830 * missing TSNs earlier than or equal to the new cumulative TSN point.
3832 * Verification Tag: 8.5 Verification Tag [Normal verification]
3834 * The return value is the disposition of the chunk.
3836 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3837 const struct sctp_endpoint *ep,
3838 const struct sctp_association *asoc,
3839 const sctp_subtype_t type,
3840 void *arg,
3841 sctp_cmd_seq_t *commands)
3843 struct sctp_chunk *chunk = arg;
3844 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3845 struct sctp_fwdtsn_skip *skip;
3846 __u16 len;
3847 __u32 tsn;
3849 if (!sctp_vtag_verify(chunk, asoc)) {
3850 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3851 SCTP_NULL());
3852 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3855 /* Make sure that the FORWARD_TSN chunk has valid length. */
3856 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3857 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3858 commands);
3860 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3861 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3862 len = ntohs(chunk->chunk_hdr->length);
3863 len -= sizeof(struct sctp_chunkhdr);
3864 skb_pull(chunk->skb, len);
3866 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3867 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3869 /* The TSN is too high--silently discard the chunk and count on it
3870 * getting retransmitted later.
3872 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3873 goto discard_noforce;
3875 /* Silently discard the chunk if stream-id is not valid */
3876 sctp_walk_fwdtsn(skip, chunk) {
3877 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3878 goto discard_noforce;
3881 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3882 if (len > sizeof(struct sctp_fwdtsn_hdr))
3883 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3884 SCTP_CHUNK(chunk));
3886 /* Count this as receiving DATA. */
3887 if (asoc->autoclose) {
3888 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3889 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3892 /* FIXME: For now send a SACK, but DATA processing may
3893 * send another.
3895 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3897 return SCTP_DISPOSITION_CONSUME;
3899 discard_noforce:
3900 return SCTP_DISPOSITION_DISCARD;
3903 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3904 struct net *net,
3905 const struct sctp_endpoint *ep,
3906 const struct sctp_association *asoc,
3907 const sctp_subtype_t type,
3908 void *arg,
3909 sctp_cmd_seq_t *commands)
3911 struct sctp_chunk *chunk = arg;
3912 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3913 struct sctp_fwdtsn_skip *skip;
3914 __u16 len;
3915 __u32 tsn;
3917 if (!sctp_vtag_verify(chunk, asoc)) {
3918 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3919 SCTP_NULL());
3920 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3923 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3924 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3925 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3926 commands);
3928 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3929 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3930 len = ntohs(chunk->chunk_hdr->length);
3931 len -= sizeof(struct sctp_chunkhdr);
3932 skb_pull(chunk->skb, len);
3934 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3935 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3937 /* The TSN is too high--silently discard the chunk and count on it
3938 * getting retransmitted later.
3940 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3941 goto gen_shutdown;
3943 /* Silently discard the chunk if stream-id is not valid */
3944 sctp_walk_fwdtsn(skip, chunk) {
3945 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3946 goto gen_shutdown;
3949 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3950 if (len > sizeof(struct sctp_fwdtsn_hdr))
3951 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3952 SCTP_CHUNK(chunk));
3954 /* Go a head and force a SACK, since we are shutting down. */
3955 gen_shutdown:
3956 /* Implementor's Guide.
3958 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3959 * respond to each received packet containing one or more DATA chunk(s)
3960 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3962 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3963 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3964 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3965 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3967 return SCTP_DISPOSITION_CONSUME;
3971 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3973 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3974 * Identifier field. If this algorithm was not specified by the
3975 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3976 * during association setup, the AUTH chunk and all chunks after it MUST
3977 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3978 * defined in Section 4.1.
3980 * If an endpoint with no shared key receives a Shared Key Identifier
3981 * other than 0, it MUST silently discard all authenticated chunks. If
3982 * the endpoint has at least one endpoint pair shared key for the peer,
3983 * it MUST use the key specified by the Shared Key Identifier if a
3984 * key has been configured for that Shared Key Identifier. If no
3985 * endpoint pair shared key has been configured for that Shared Key
3986 * Identifier, all authenticated chunks MUST be silently discarded.
3988 * Verification Tag: 8.5 Verification Tag [Normal verification]
3990 * The return value is the disposition of the chunk.
3992 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3993 const struct sctp_endpoint *ep,
3994 const struct sctp_association *asoc,
3995 const sctp_subtype_t type,
3996 struct sctp_chunk *chunk)
3998 struct sctp_authhdr *auth_hdr;
3999 struct sctp_hmac *hmac;
4000 unsigned int sig_len;
4001 __u16 key_id;
4002 __u8 *save_digest;
4003 __u8 *digest;
4005 /* Pull in the auth header, so we can do some more verification */
4006 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4007 chunk->subh.auth_hdr = auth_hdr;
4008 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4010 /* Make sure that we support the HMAC algorithm from the auth
4011 * chunk.
4013 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4014 return SCTP_IERROR_AUTH_BAD_HMAC;
4016 /* Make sure that the provided shared key identifier has been
4017 * configured
4019 key_id = ntohs(auth_hdr->shkey_id);
4020 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4021 return SCTP_IERROR_AUTH_BAD_KEYID;
4024 /* Make sure that the length of the signature matches what
4025 * we expect.
4027 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4028 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4029 if (sig_len != hmac->hmac_len)
4030 return SCTP_IERROR_PROTO_VIOLATION;
4032 /* Now that we've done validation checks, we can compute and
4033 * verify the hmac. The steps involved are:
4034 * 1. Save the digest from the chunk.
4035 * 2. Zero out the digest in the chunk.
4036 * 3. Compute the new digest
4037 * 4. Compare saved and new digests.
4039 digest = auth_hdr->hmac;
4040 skb_pull(chunk->skb, sig_len);
4042 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4043 if (!save_digest)
4044 goto nomem;
4046 memset(digest, 0, sig_len);
4048 sctp_auth_calculate_hmac(asoc, chunk->skb,
4049 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4050 GFP_ATOMIC);
4052 /* Discard the packet if the digests do not match */
4053 if (memcmp(save_digest, digest, sig_len)) {
4054 kfree(save_digest);
4055 return SCTP_IERROR_BAD_SIG;
4058 kfree(save_digest);
4059 chunk->auth = 1;
4061 return SCTP_IERROR_NO_ERROR;
4062 nomem:
4063 return SCTP_IERROR_NOMEM;
4066 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4067 const struct sctp_endpoint *ep,
4068 const struct sctp_association *asoc,
4069 const sctp_subtype_t type,
4070 void *arg,
4071 sctp_cmd_seq_t *commands)
4073 struct sctp_authhdr *auth_hdr;
4074 struct sctp_chunk *chunk = arg;
4075 struct sctp_chunk *err_chunk;
4076 sctp_ierror_t error;
4078 /* Make sure that the peer has AUTH capable */
4079 if (!asoc->peer.auth_capable)
4080 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4082 if (!sctp_vtag_verify(chunk, asoc)) {
4083 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4084 SCTP_NULL());
4085 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4088 /* Make sure that the AUTH chunk has valid length. */
4089 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4090 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4091 commands);
4093 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4094 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
4095 switch (error) {
4096 case SCTP_IERROR_AUTH_BAD_HMAC:
4097 /* Generate the ERROR chunk and discard the rest
4098 * of the packet
4100 err_chunk = sctp_make_op_error(asoc, chunk,
4101 SCTP_ERROR_UNSUP_HMAC,
4102 &auth_hdr->hmac_id,
4103 sizeof(__u16), 0);
4104 if (err_chunk) {
4105 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4106 SCTP_CHUNK(err_chunk));
4108 /* Fall Through */
4109 case SCTP_IERROR_AUTH_BAD_KEYID:
4110 case SCTP_IERROR_BAD_SIG:
4111 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4113 case SCTP_IERROR_PROTO_VIOLATION:
4114 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4115 commands);
4117 case SCTP_IERROR_NOMEM:
4118 return SCTP_DISPOSITION_NOMEM;
4120 default: /* Prevent gcc warnings */
4121 break;
4124 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4125 struct sctp_ulpevent *ev;
4127 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4128 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4130 if (!ev)
4131 return -ENOMEM;
4133 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4134 SCTP_ULPEVENT(ev));
4137 return SCTP_DISPOSITION_CONSUME;
4141 * Process an unknown chunk.
4143 * Section: 3.2. Also, 2.1 in the implementor's guide.
4145 * Chunk Types are encoded such that the highest-order two bits specify
4146 * the action that must be taken if the processing endpoint does not
4147 * recognize the Chunk Type.
4149 * 00 - Stop processing this SCTP packet and discard it, do not process
4150 * any further chunks within it.
4152 * 01 - Stop processing this SCTP packet and discard it, do not process
4153 * any further chunks within it, and report the unrecognized
4154 * chunk in an 'Unrecognized Chunk Type'.
4156 * 10 - Skip this chunk and continue processing.
4158 * 11 - Skip this chunk and continue processing, but report in an ERROR
4159 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4161 * The return value is the disposition of the chunk.
4163 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4164 const struct sctp_endpoint *ep,
4165 const struct sctp_association *asoc,
4166 const sctp_subtype_t type,
4167 void *arg,
4168 sctp_cmd_seq_t *commands)
4170 struct sctp_chunk *unk_chunk = arg;
4171 struct sctp_chunk *err_chunk;
4172 sctp_chunkhdr_t *hdr;
4174 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4176 if (!sctp_vtag_verify(unk_chunk, asoc))
4177 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4179 /* Make sure that the chunk has a valid length.
4180 * Since we don't know the chunk type, we use a general
4181 * chunkhdr structure to make a comparison.
4183 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4184 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4185 commands);
4187 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4188 case SCTP_CID_ACTION_DISCARD:
4189 /* Discard the packet. */
4190 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4191 break;
4192 case SCTP_CID_ACTION_DISCARD_ERR:
4193 /* Generate an ERROR chunk as response. */
4194 hdr = unk_chunk->chunk_hdr;
4195 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4196 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4197 WORD_ROUND(ntohs(hdr->length)),
4199 if (err_chunk) {
4200 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4201 SCTP_CHUNK(err_chunk));
4204 /* Discard the packet. */
4205 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4206 return SCTP_DISPOSITION_CONSUME;
4207 break;
4208 case SCTP_CID_ACTION_SKIP:
4209 /* Skip the chunk. */
4210 return SCTP_DISPOSITION_DISCARD;
4211 break;
4212 case SCTP_CID_ACTION_SKIP_ERR:
4213 /* Generate an ERROR chunk as response. */
4214 hdr = unk_chunk->chunk_hdr;
4215 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4216 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4217 WORD_ROUND(ntohs(hdr->length)),
4219 if (err_chunk) {
4220 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4221 SCTP_CHUNK(err_chunk));
4223 /* Skip the chunk. */
4224 return SCTP_DISPOSITION_CONSUME;
4225 break;
4226 default:
4227 break;
4230 return SCTP_DISPOSITION_DISCARD;
4234 * Discard the chunk.
4236 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4237 * [Too numerous to mention...]
4238 * Verification Tag: No verification needed.
4239 * Inputs
4240 * (endpoint, asoc, chunk)
4242 * Outputs
4243 * (asoc, reply_msg, msg_up, timers, counters)
4245 * The return value is the disposition of the chunk.
4247 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4248 const struct sctp_endpoint *ep,
4249 const struct sctp_association *asoc,
4250 const sctp_subtype_t type,
4251 void *arg,
4252 sctp_cmd_seq_t *commands)
4254 struct sctp_chunk *chunk = arg;
4256 /* Make sure that the chunk has a valid length.
4257 * Since we don't know the chunk type, we use a general
4258 * chunkhdr structure to make a comparison.
4260 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4261 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4262 commands);
4264 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4266 return SCTP_DISPOSITION_DISCARD;
4270 * Discard the whole packet.
4272 * Section: 8.4 2)
4274 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4275 * silently discard the OOTB packet and take no further action.
4277 * Verification Tag: No verification necessary
4279 * Inputs
4280 * (endpoint, asoc, chunk)
4282 * Outputs
4283 * (asoc, reply_msg, msg_up, timers, counters)
4285 * The return value is the disposition of the chunk.
4287 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4288 const struct sctp_endpoint *ep,
4289 const struct sctp_association *asoc,
4290 const sctp_subtype_t type,
4291 void *arg,
4292 sctp_cmd_seq_t *commands)
4294 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4295 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4297 return SCTP_DISPOSITION_CONSUME;
4302 * The other end is violating protocol.
4304 * Section: Not specified
4305 * Verification Tag: Not specified
4306 * Inputs
4307 * (endpoint, asoc, chunk)
4309 * Outputs
4310 * (asoc, reply_msg, msg_up, timers, counters)
4312 * We simply tag the chunk as a violation. The state machine will log
4313 * the violation and continue.
4315 sctp_disposition_t sctp_sf_violation(struct net *net,
4316 const struct sctp_endpoint *ep,
4317 const struct sctp_association *asoc,
4318 const sctp_subtype_t type,
4319 void *arg,
4320 sctp_cmd_seq_t *commands)
4322 struct sctp_chunk *chunk = arg;
4324 /* Make sure that the chunk has a valid length. */
4325 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4326 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4327 commands);
4329 return SCTP_DISPOSITION_VIOLATION;
4333 * Common function to handle a protocol violation.
4335 static sctp_disposition_t sctp_sf_abort_violation(
4336 struct net *net,
4337 const struct sctp_endpoint *ep,
4338 const struct sctp_association *asoc,
4339 void *arg,
4340 sctp_cmd_seq_t *commands,
4341 const __u8 *payload,
4342 const size_t paylen)
4344 struct sctp_packet *packet = NULL;
4345 struct sctp_chunk *chunk = arg;
4346 struct sctp_chunk *abort = NULL;
4348 /* SCTP-AUTH, Section 6.3:
4349 * It should be noted that if the receiver wants to tear
4350 * down an association in an authenticated way only, the
4351 * handling of malformed packets should not result in
4352 * tearing down the association.
4354 * This means that if we only want to abort associations
4355 * in an authenticated way (i.e AUTH+ABORT), then we
4356 * can't destroy this association just because the packet
4357 * was malformed.
4359 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4360 goto discard;
4362 /* Make the abort chunk. */
4363 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4364 if (!abort)
4365 goto nomem;
4367 if (asoc) {
4368 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4369 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4370 !asoc->peer.i.init_tag) {
4371 sctp_initack_chunk_t *initack;
4373 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4374 if (!sctp_chunk_length_valid(chunk,
4375 sizeof(sctp_initack_chunk_t)))
4376 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4377 else {
4378 unsigned int inittag;
4380 inittag = ntohl(initack->init_hdr.init_tag);
4381 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4382 SCTP_U32(inittag));
4386 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4387 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4389 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4390 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4391 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4392 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4393 SCTP_ERROR(ECONNREFUSED));
4394 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4395 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4396 } else {
4397 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4398 SCTP_ERROR(ECONNABORTED));
4399 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4400 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4401 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4403 } else {
4404 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4406 if (!packet)
4407 goto nomem_pkt;
4409 if (sctp_test_T_bit(abort))
4410 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4412 abort->skb->sk = ep->base.sk;
4414 sctp_packet_append_chunk(packet, abort);
4416 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4417 SCTP_PACKET(packet));
4419 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4422 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4424 discard:
4425 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4426 return SCTP_DISPOSITION_ABORT;
4428 nomem_pkt:
4429 sctp_chunk_free(abort);
4430 nomem:
4431 return SCTP_DISPOSITION_NOMEM;
4435 * Handle a protocol violation when the chunk length is invalid.
4436 * "Invalid" length is identified as smaller than the minimal length a
4437 * given chunk can be. For example, a SACK chunk has invalid length
4438 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4440 * We inform the other end by sending an ABORT with a Protocol Violation
4441 * error code.
4443 * Section: Not specified
4444 * Verification Tag: Nothing to do
4445 * Inputs
4446 * (endpoint, asoc, chunk)
4448 * Outputs
4449 * (reply_msg, msg_up, counters)
4451 * Generate an ABORT chunk and terminate the association.
4453 static sctp_disposition_t sctp_sf_violation_chunklen(
4454 struct net *net,
4455 const struct sctp_endpoint *ep,
4456 const struct sctp_association *asoc,
4457 const sctp_subtype_t type,
4458 void *arg,
4459 sctp_cmd_seq_t *commands)
4461 static const char err_str[]="The following chunk had invalid length:";
4463 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4464 sizeof(err_str));
4468 * Handle a protocol violation when the parameter length is invalid.
4469 * If the length is smaller than the minimum length of a given parameter,
4470 * or accumulated length in multi parameters exceeds the end of the chunk,
4471 * the length is considered as invalid.
4473 static sctp_disposition_t sctp_sf_violation_paramlen(
4474 struct net *net,
4475 const struct sctp_endpoint *ep,
4476 const struct sctp_association *asoc,
4477 const sctp_subtype_t type,
4478 void *arg, void *ext,
4479 sctp_cmd_seq_t *commands)
4481 struct sctp_chunk *chunk = arg;
4482 struct sctp_paramhdr *param = ext;
4483 struct sctp_chunk *abort = NULL;
4485 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4486 goto discard;
4488 /* Make the abort chunk. */
4489 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4490 if (!abort)
4491 goto nomem;
4493 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4494 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4496 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4497 SCTP_ERROR(ECONNABORTED));
4498 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4499 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4500 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4501 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4503 discard:
4504 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4505 return SCTP_DISPOSITION_ABORT;
4506 nomem:
4507 return SCTP_DISPOSITION_NOMEM;
4510 /* Handle a protocol violation when the peer trying to advance the
4511 * cumulative tsn ack to a point beyond the max tsn currently sent.
4513 * We inform the other end by sending an ABORT with a Protocol Violation
4514 * error code.
4516 static sctp_disposition_t sctp_sf_violation_ctsn(
4517 struct net *net,
4518 const struct sctp_endpoint *ep,
4519 const struct sctp_association *asoc,
4520 const sctp_subtype_t type,
4521 void *arg,
4522 sctp_cmd_seq_t *commands)
4524 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4526 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4527 sizeof(err_str));
4530 /* Handle protocol violation of an invalid chunk bundling. For example,
4531 * when we have an association and we receive bundled INIT-ACK, or
4532 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4533 * statement from the specs. Additionally, there might be an attacker
4534 * on the path and we may not want to continue this communication.
4536 static sctp_disposition_t sctp_sf_violation_chunk(
4537 struct net *net,
4538 const struct sctp_endpoint *ep,
4539 const struct sctp_association *asoc,
4540 const sctp_subtype_t type,
4541 void *arg,
4542 sctp_cmd_seq_t *commands)
4544 static const char err_str[]="The following chunk violates protocol:";
4546 if (!asoc)
4547 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4549 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4550 sizeof(err_str));
4552 /***************************************************************************
4553 * These are the state functions for handling primitive (Section 10) events.
4554 ***************************************************************************/
4556 * sctp_sf_do_prm_asoc
4558 * Section: 10.1 ULP-to-SCTP
4559 * B) Associate
4561 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4562 * outbound stream count)
4563 * -> association id [,destination transport addr list] [,outbound stream
4564 * count]
4566 * This primitive allows the upper layer to initiate an association to a
4567 * specific peer endpoint.
4569 * The peer endpoint shall be specified by one of the transport addresses
4570 * which defines the endpoint (see Section 1.4). If the local SCTP
4571 * instance has not been initialized, the ASSOCIATE is considered an
4572 * error.
4573 * [This is not relevant for the kernel implementation since we do all
4574 * initialization at boot time. It we hadn't initialized we wouldn't
4575 * get anywhere near this code.]
4577 * An association id, which is a local handle to the SCTP association,
4578 * will be returned on successful establishment of the association. If
4579 * SCTP is not able to open an SCTP association with the peer endpoint,
4580 * an error is returned.
4581 * [In the kernel implementation, the struct sctp_association needs to
4582 * be created BEFORE causing this primitive to run.]
4584 * Other association parameters may be returned, including the
4585 * complete destination transport addresses of the peer as well as the
4586 * outbound stream count of the local endpoint. One of the transport
4587 * address from the returned destination addresses will be selected by
4588 * the local endpoint as default primary path for sending SCTP packets
4589 * to this peer. The returned "destination transport addr list" can
4590 * be used by the ULP to change the default primary path or to force
4591 * sending a packet to a specific transport address. [All of this
4592 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4593 * function.]
4595 * Mandatory attributes:
4597 * o local SCTP instance name - obtained from the INITIALIZE operation.
4598 * [This is the argument asoc.]
4599 * o destination transport addr - specified as one of the transport
4600 * addresses of the peer endpoint with which the association is to be
4601 * established.
4602 * [This is asoc->peer.active_path.]
4603 * o outbound stream count - the number of outbound streams the ULP
4604 * would like to open towards this peer endpoint.
4605 * [BUG: This is not currently implemented.]
4606 * Optional attributes:
4608 * None.
4610 * The return value is a disposition.
4612 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4613 const struct sctp_endpoint *ep,
4614 const struct sctp_association *asoc,
4615 const sctp_subtype_t type,
4616 void *arg,
4617 sctp_cmd_seq_t *commands)
4619 struct sctp_chunk *repl;
4620 struct sctp_association* my_asoc;
4622 /* The comment below says that we enter COOKIE-WAIT AFTER
4623 * sending the INIT, but that doesn't actually work in our
4624 * implementation...
4626 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4627 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4629 /* RFC 2960 5.1 Normal Establishment of an Association
4631 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4632 * must provide its Verification Tag (Tag_A) in the Initiate
4633 * Tag field. Tag_A SHOULD be a random number in the range of
4634 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4637 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4638 if (!repl)
4639 goto nomem;
4641 /* Choose transport for INIT. */
4642 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4643 SCTP_CHUNK(repl));
4645 /* Cast away the const modifier, as we want to just
4646 * rerun it through as a sideffect.
4648 my_asoc = (struct sctp_association *)asoc;
4649 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4651 /* After sending the INIT, "A" starts the T1-init timer and
4652 * enters the COOKIE-WAIT state.
4654 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4655 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4656 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4657 return SCTP_DISPOSITION_CONSUME;
4659 nomem:
4660 return SCTP_DISPOSITION_NOMEM;
4664 * Process the SEND primitive.
4666 * Section: 10.1 ULP-to-SCTP
4667 * E) Send
4669 * Format: SEND(association id, buffer address, byte count [,context]
4670 * [,stream id] [,life time] [,destination transport address]
4671 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4672 * -> result
4674 * This is the main method to send user data via SCTP.
4676 * Mandatory attributes:
4678 * o association id - local handle to the SCTP association
4680 * o buffer address - the location where the user message to be
4681 * transmitted is stored;
4683 * o byte count - The size of the user data in number of bytes;
4685 * Optional attributes:
4687 * o context - an optional 32 bit integer that will be carried in the
4688 * sending failure notification to the ULP if the transportation of
4689 * this User Message fails.
4691 * o stream id - to indicate which stream to send the data on. If not
4692 * specified, stream 0 will be used.
4694 * o life time - specifies the life time of the user data. The user data
4695 * will not be sent by SCTP after the life time expires. This
4696 * parameter can be used to avoid efforts to transmit stale
4697 * user messages. SCTP notifies the ULP if the data cannot be
4698 * initiated to transport (i.e. sent to the destination via SCTP's
4699 * send primitive) within the life time variable. However, the
4700 * user data will be transmitted if SCTP has attempted to transmit a
4701 * chunk before the life time expired.
4703 * o destination transport address - specified as one of the destination
4704 * transport addresses of the peer endpoint to which this packet
4705 * should be sent. Whenever possible, SCTP should use this destination
4706 * transport address for sending the packets, instead of the current
4707 * primary path.
4709 * o unorder flag - this flag, if present, indicates that the user
4710 * would like the data delivered in an unordered fashion to the peer
4711 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4712 * message).
4714 * o no-bundle flag - instructs SCTP not to bundle this user data with
4715 * other outbound DATA chunks. SCTP MAY still bundle even when
4716 * this flag is present, when faced with network congestion.
4718 * o payload protocol-id - A 32 bit unsigned integer that is to be
4719 * passed to the peer indicating the type of payload protocol data
4720 * being transmitted. This value is passed as opaque data by SCTP.
4722 * The return value is the disposition.
4724 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4725 const struct sctp_endpoint *ep,
4726 const struct sctp_association *asoc,
4727 const sctp_subtype_t type,
4728 void *arg,
4729 sctp_cmd_seq_t *commands)
4731 struct sctp_datamsg *msg = arg;
4733 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4734 return SCTP_DISPOSITION_CONSUME;
4738 * Process the SHUTDOWN primitive.
4740 * Section: 10.1:
4741 * C) Shutdown
4743 * Format: SHUTDOWN(association id)
4744 * -> result
4746 * Gracefully closes an association. Any locally queued user data
4747 * will be delivered to the peer. The association will be terminated only
4748 * after the peer acknowledges all the SCTP packets sent. A success code
4749 * will be returned on successful termination of the association. If
4750 * attempting to terminate the association results in a failure, an error
4751 * code shall be returned.
4753 * Mandatory attributes:
4755 * o association id - local handle to the SCTP association
4757 * Optional attributes:
4759 * None.
4761 * The return value is the disposition.
4763 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4764 struct net *net,
4765 const struct sctp_endpoint *ep,
4766 const struct sctp_association *asoc,
4767 const sctp_subtype_t type,
4768 void *arg,
4769 sctp_cmd_seq_t *commands)
4771 int disposition;
4773 /* From 9.2 Shutdown of an Association
4774 * Upon receipt of the SHUTDOWN primitive from its upper
4775 * layer, the endpoint enters SHUTDOWN-PENDING state and
4776 * remains there until all outstanding data has been
4777 * acknowledged by its peer. The endpoint accepts no new data
4778 * from its upper layer, but retransmits data to the far end
4779 * if necessary to fill gaps.
4781 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4782 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4784 disposition = SCTP_DISPOSITION_CONSUME;
4785 if (sctp_outq_is_empty(&asoc->outqueue)) {
4786 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4787 arg, commands);
4789 return disposition;
4793 * Process the ABORT primitive.
4795 * Section: 10.1:
4796 * C) Abort
4798 * Format: Abort(association id [, cause code])
4799 * -> result
4801 * Ungracefully closes an association. Any locally queued user data
4802 * will be discarded and an ABORT chunk is sent to the peer. A success code
4803 * will be returned on successful abortion of the association. If
4804 * attempting to abort the association results in a failure, an error
4805 * code shall be returned.
4807 * Mandatory attributes:
4809 * o association id - local handle to the SCTP association
4811 * Optional attributes:
4813 * o cause code - reason of the abort to be passed to the peer
4815 * None.
4817 * The return value is the disposition.
4819 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4820 struct net *net,
4821 const struct sctp_endpoint *ep,
4822 const struct sctp_association *asoc,
4823 const sctp_subtype_t type,
4824 void *arg,
4825 sctp_cmd_seq_t *commands)
4827 /* From 9.1 Abort of an Association
4828 * Upon receipt of the ABORT primitive from its upper
4829 * layer, the endpoint enters CLOSED state and
4830 * discard all outstanding data has been
4831 * acknowledged by its peer. The endpoint accepts no new data
4832 * from its upper layer, but retransmits data to the far end
4833 * if necessary to fill gaps.
4835 struct sctp_chunk *abort = arg;
4836 sctp_disposition_t retval;
4838 retval = SCTP_DISPOSITION_CONSUME;
4840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4842 /* Even if we can't send the ABORT due to low memory delete the
4843 * TCB. This is a departure from our typical NOMEM handling.
4846 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4847 SCTP_ERROR(ECONNABORTED));
4848 /* Delete the established association. */
4849 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4850 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4852 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4853 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4855 return retval;
4858 /* We tried an illegal operation on an association which is closed. */
4859 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4860 const struct sctp_endpoint *ep,
4861 const struct sctp_association *asoc,
4862 const sctp_subtype_t type,
4863 void *arg,
4864 sctp_cmd_seq_t *commands)
4866 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4867 return SCTP_DISPOSITION_CONSUME;
4870 /* We tried an illegal operation on an association which is shutting
4871 * down.
4873 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4874 const struct sctp_endpoint *ep,
4875 const struct sctp_association *asoc,
4876 const sctp_subtype_t type,
4877 void *arg,
4878 sctp_cmd_seq_t *commands)
4880 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4881 SCTP_ERROR(-ESHUTDOWN));
4882 return SCTP_DISPOSITION_CONSUME;
4886 * sctp_cookie_wait_prm_shutdown
4888 * Section: 4 Note: 2
4889 * Verification Tag:
4890 * Inputs
4891 * (endpoint, asoc)
4893 * The RFC does not explicitly address this issue, but is the route through the
4894 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4896 * Outputs
4897 * (timers)
4899 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4900 struct net *net,
4901 const struct sctp_endpoint *ep,
4902 const struct sctp_association *asoc,
4903 const sctp_subtype_t type,
4904 void *arg,
4905 sctp_cmd_seq_t *commands)
4907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4908 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4910 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4911 SCTP_STATE(SCTP_STATE_CLOSED));
4913 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4915 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4917 return SCTP_DISPOSITION_DELETE_TCB;
4921 * sctp_cookie_echoed_prm_shutdown
4923 * Section: 4 Note: 2
4924 * Verification Tag:
4925 * Inputs
4926 * (endpoint, asoc)
4928 * The RFC does not explcitly address this issue, but is the route through the
4929 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4931 * Outputs
4932 * (timers)
4934 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4935 struct net *net,
4936 const struct sctp_endpoint *ep,
4937 const struct sctp_association *asoc,
4938 const sctp_subtype_t type,
4939 void *arg, sctp_cmd_seq_t *commands)
4941 /* There is a single T1 timer, so we should be able to use
4942 * common function with the COOKIE-WAIT state.
4944 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4948 * sctp_sf_cookie_wait_prm_abort
4950 * Section: 4 Note: 2
4951 * Verification Tag:
4952 * Inputs
4953 * (endpoint, asoc)
4955 * The RFC does not explicitly address this issue, but is the route through the
4956 * state table when someone issues an abort while in COOKIE_WAIT state.
4958 * Outputs
4959 * (timers)
4961 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4962 struct net *net,
4963 const struct sctp_endpoint *ep,
4964 const struct sctp_association *asoc,
4965 const sctp_subtype_t type,
4966 void *arg,
4967 sctp_cmd_seq_t *commands)
4969 struct sctp_chunk *abort = arg;
4970 sctp_disposition_t retval;
4972 /* Stop T1-init timer */
4973 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4974 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4975 retval = SCTP_DISPOSITION_CONSUME;
4977 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4979 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4980 SCTP_STATE(SCTP_STATE_CLOSED));
4982 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4984 /* Even if we can't send the ABORT due to low memory delete the
4985 * TCB. This is a departure from our typical NOMEM handling.
4988 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4989 SCTP_ERROR(ECONNREFUSED));
4990 /* Delete the established association. */
4991 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4992 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4994 return retval;
4998 * sctp_sf_cookie_echoed_prm_abort
5000 * Section: 4 Note: 3
5001 * Verification Tag:
5002 * Inputs
5003 * (endpoint, asoc)
5005 * The RFC does not explcitly address this issue, but is the route through the
5006 * state table when someone issues an abort while in COOKIE_ECHOED state.
5008 * Outputs
5009 * (timers)
5011 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5012 struct net *net,
5013 const struct sctp_endpoint *ep,
5014 const struct sctp_association *asoc,
5015 const sctp_subtype_t type,
5016 void *arg,
5017 sctp_cmd_seq_t *commands)
5019 /* There is a single T1 timer, so we should be able to use
5020 * common function with the COOKIE-WAIT state.
5022 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5026 * sctp_sf_shutdown_pending_prm_abort
5028 * Inputs
5029 * (endpoint, asoc)
5031 * The RFC does not explicitly address this issue, but is the route through the
5032 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5034 * Outputs
5035 * (timers)
5037 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5038 struct net *net,
5039 const struct sctp_endpoint *ep,
5040 const struct sctp_association *asoc,
5041 const sctp_subtype_t type,
5042 void *arg,
5043 sctp_cmd_seq_t *commands)
5045 /* Stop the T5-shutdown guard timer. */
5046 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5047 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5049 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5053 * sctp_sf_shutdown_sent_prm_abort
5055 * Inputs
5056 * (endpoint, asoc)
5058 * The RFC does not explicitly address this issue, but is the route through the
5059 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5061 * Outputs
5062 * (timers)
5064 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5065 struct net *net,
5066 const struct sctp_endpoint *ep,
5067 const struct sctp_association *asoc,
5068 const sctp_subtype_t type,
5069 void *arg,
5070 sctp_cmd_seq_t *commands)
5072 /* Stop the T2-shutdown timer. */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5076 /* Stop the T5-shutdown guard timer. */
5077 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5078 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5080 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5084 * sctp_sf_cookie_echoed_prm_abort
5086 * Inputs
5087 * (endpoint, asoc)
5089 * The RFC does not explcitly address this issue, but is the route through the
5090 * state table when someone issues an abort while in COOKIE_ECHOED state.
5092 * Outputs
5093 * (timers)
5095 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5096 struct net *net,
5097 const struct sctp_endpoint *ep,
5098 const struct sctp_association *asoc,
5099 const sctp_subtype_t type,
5100 void *arg,
5101 sctp_cmd_seq_t *commands)
5103 /* The same T2 timer, so we should be able to use
5104 * common function with the SHUTDOWN-SENT state.
5106 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5110 * Process the REQUESTHEARTBEAT primitive
5112 * 10.1 ULP-to-SCTP
5113 * J) Request Heartbeat
5115 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5117 * -> result
5119 * Instructs the local endpoint to perform a HeartBeat on the specified
5120 * destination transport address of the given association. The returned
5121 * result should indicate whether the transmission of the HEARTBEAT
5122 * chunk to the destination address is successful.
5124 * Mandatory attributes:
5126 * o association id - local handle to the SCTP association
5128 * o destination transport address - the transport address of the
5129 * association on which a heartbeat should be issued.
5131 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5132 struct net *net,
5133 const struct sctp_endpoint *ep,
5134 const struct sctp_association *asoc,
5135 const sctp_subtype_t type,
5136 void *arg,
5137 sctp_cmd_seq_t *commands)
5139 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5140 (struct sctp_transport *)arg, commands))
5141 return SCTP_DISPOSITION_NOMEM;
5144 * RFC 2960 (bis), section 8.3
5146 * D) Request an on-demand HEARTBEAT on a specific destination
5147 * transport address of a given association.
5149 * The endpoint should increment the respective error counter of
5150 * the destination transport address each time a HEARTBEAT is sent
5151 * to that address and not acknowledged within one RTO.
5154 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5155 SCTP_TRANSPORT(arg));
5156 return SCTP_DISPOSITION_CONSUME;
5160 * ADDIP Section 4.1 ASCONF Chunk Procedures
5161 * When an endpoint has an ASCONF signaled change to be sent to the
5162 * remote endpoint it should do A1 to A9
5164 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5165 const struct sctp_endpoint *ep,
5166 const struct sctp_association *asoc,
5167 const sctp_subtype_t type,
5168 void *arg,
5169 sctp_cmd_seq_t *commands)
5171 struct sctp_chunk *chunk = arg;
5173 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5174 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5175 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5176 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5177 return SCTP_DISPOSITION_CONSUME;
5181 * Ignore the primitive event
5183 * The return value is the disposition of the primitive.
5185 sctp_disposition_t sctp_sf_ignore_primitive(
5186 struct net *net,
5187 const struct sctp_endpoint *ep,
5188 const struct sctp_association *asoc,
5189 const sctp_subtype_t type,
5190 void *arg,
5191 sctp_cmd_seq_t *commands)
5193 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5194 type.primitive);
5196 return SCTP_DISPOSITION_DISCARD;
5199 /***************************************************************************
5200 * These are the state functions for the OTHER events.
5201 ***************************************************************************/
5204 * When the SCTP stack has no more user data to send or retransmit, this
5205 * notification is given to the user. Also, at the time when a user app
5206 * subscribes to this event, if there is no data to be sent or
5207 * retransmit, the stack will immediately send up this notification.
5209 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5210 struct net *net,
5211 const struct sctp_endpoint *ep,
5212 const struct sctp_association *asoc,
5213 const sctp_subtype_t type,
5214 void *arg,
5215 sctp_cmd_seq_t *commands)
5217 struct sctp_ulpevent *event;
5219 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5220 if (!event)
5221 return SCTP_DISPOSITION_NOMEM;
5223 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5225 return SCTP_DISPOSITION_CONSUME;
5229 * Start the shutdown negotiation.
5231 * From Section 9.2:
5232 * Once all its outstanding data has been acknowledged, the endpoint
5233 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5234 * TSN Ack field the last sequential TSN it has received from the peer.
5235 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5236 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5237 * with the updated last sequential TSN received from its peer.
5239 * The return value is the disposition.
5241 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5242 struct net *net,
5243 const struct sctp_endpoint *ep,
5244 const struct sctp_association *asoc,
5245 const sctp_subtype_t type,
5246 void *arg,
5247 sctp_cmd_seq_t *commands)
5249 struct sctp_chunk *reply;
5251 /* Once all its outstanding data has been acknowledged, the
5252 * endpoint shall send a SHUTDOWN chunk to its peer including
5253 * in the Cumulative TSN Ack field the last sequential TSN it
5254 * has received from the peer.
5256 reply = sctp_make_shutdown(asoc, NULL);
5257 if (!reply)
5258 goto nomem;
5260 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5261 * T2-shutdown timer.
5263 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5265 /* It shall then start the T2-shutdown timer */
5266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5267 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5269 /* RFC 4960 Section 9.2
5270 * The sender of the SHUTDOWN MAY also start an overall guard timer
5271 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5273 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5274 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5276 if (asoc->autoclose)
5277 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5278 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5280 /* and enter the SHUTDOWN-SENT state. */
5281 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5282 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5284 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5286 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5287 * or SHUTDOWN-ACK.
5289 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5291 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5293 return SCTP_DISPOSITION_CONSUME;
5295 nomem:
5296 return SCTP_DISPOSITION_NOMEM;
5300 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5302 * From Section 9.2:
5304 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5305 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5306 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5307 * endpoint must re-send the SHUTDOWN ACK.
5309 * The return value is the disposition.
5311 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5312 struct net *net,
5313 const struct sctp_endpoint *ep,
5314 const struct sctp_association *asoc,
5315 const sctp_subtype_t type,
5316 void *arg,
5317 sctp_cmd_seq_t *commands)
5319 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5320 struct sctp_chunk *reply;
5322 /* There are 2 ways of getting here:
5323 * 1) called in response to a SHUTDOWN chunk
5324 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5326 * For the case (2), the arg parameter is set to NULL. We need
5327 * to check that we have a chunk before accessing it's fields.
5329 if (chunk) {
5330 if (!sctp_vtag_verify(chunk, asoc))
5331 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5333 /* Make sure that the SHUTDOWN chunk has a valid length. */
5334 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5335 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5336 commands);
5339 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5340 * shall send a SHUTDOWN ACK ...
5342 reply = sctp_make_shutdown_ack(asoc, chunk);
5343 if (!reply)
5344 goto nomem;
5346 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5347 * the T2-shutdown timer.
5349 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5351 /* and start/restart a T2-shutdown timer of its own, */
5352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5353 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5355 if (asoc->autoclose)
5356 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5357 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5359 /* Enter the SHUTDOWN-ACK-SENT state. */
5360 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5361 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5363 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5365 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5366 * or SHUTDOWN-ACK.
5368 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5370 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5372 return SCTP_DISPOSITION_CONSUME;
5374 nomem:
5375 return SCTP_DISPOSITION_NOMEM;
5379 * Ignore the event defined as other
5381 * The return value is the disposition of the event.
5383 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5384 const struct sctp_endpoint *ep,
5385 const struct sctp_association *asoc,
5386 const sctp_subtype_t type,
5387 void *arg,
5388 sctp_cmd_seq_t *commands)
5390 pr_debug("%s: the event other type:%d is ignored\n",
5391 __func__, type.other);
5393 return SCTP_DISPOSITION_DISCARD;
5396 /************************************************************
5397 * These are the state functions for handling timeout events.
5398 ************************************************************/
5401 * RTX Timeout
5403 * Section: 6.3.3 Handle T3-rtx Expiration
5405 * Whenever the retransmission timer T3-rtx expires for a destination
5406 * address, do the following:
5407 * [See below]
5409 * The return value is the disposition of the chunk.
5411 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5412 const struct sctp_endpoint *ep,
5413 const struct sctp_association *asoc,
5414 const sctp_subtype_t type,
5415 void *arg,
5416 sctp_cmd_seq_t *commands)
5418 struct sctp_transport *transport = arg;
5420 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5422 if (asoc->overall_error_count >= asoc->max_retrans) {
5423 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5425 * We are here likely because the receiver had its rwnd
5426 * closed for a while and we have not been able to
5427 * transmit the locally queued data within the maximum
5428 * retransmission attempts limit. Start the T5
5429 * shutdown guard timer to give the receiver one last
5430 * chance and some additional time to recover before
5431 * aborting.
5433 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5434 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5435 } else {
5436 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5437 SCTP_ERROR(ETIMEDOUT));
5438 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5439 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5440 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5441 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5442 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5443 return SCTP_DISPOSITION_DELETE_TCB;
5447 /* E1) For the destination address for which the timer
5448 * expires, adjust its ssthresh with rules defined in Section
5449 * 7.2.3 and set the cwnd <- MTU.
5452 /* E2) For the destination address for which the timer
5453 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5454 * maximum value discussed in rule C7 above (RTO.max) may be
5455 * used to provide an upper bound to this doubling operation.
5458 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5459 * outstanding DATA chunks for the address for which the
5460 * T3-rtx has expired will fit into a single packet, subject
5461 * to the MTU constraint for the path corresponding to the
5462 * destination transport address to which the retransmission
5463 * is being sent (this may be different from the address for
5464 * which the timer expires [see Section 6.4]). Call this
5465 * value K. Bundle and retransmit those K DATA chunks in a
5466 * single packet to the destination endpoint.
5468 * Note: Any DATA chunks that were sent to the address for
5469 * which the T3-rtx timer expired but did not fit in one MTU
5470 * (rule E3 above), should be marked for retransmission and
5471 * sent as soon as cwnd allows (normally when a SACK arrives).
5474 /* Do some failure management (Section 8.2). */
5475 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5477 /* NB: Rules E4 and F1 are implicit in R1. */
5478 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5480 return SCTP_DISPOSITION_CONSUME;
5484 * Generate delayed SACK on timeout
5486 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5488 * The guidelines on delayed acknowledgement algorithm specified in
5489 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5490 * acknowledgement SHOULD be generated for at least every second packet
5491 * (not every second DATA chunk) received, and SHOULD be generated
5492 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5493 * some situations it may be beneficial for an SCTP transmitter to be
5494 * more conservative than the algorithms detailed in this document
5495 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5496 * the following algorithms allow.
5498 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5499 const struct sctp_endpoint *ep,
5500 const struct sctp_association *asoc,
5501 const sctp_subtype_t type,
5502 void *arg,
5503 sctp_cmd_seq_t *commands)
5505 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5506 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5507 return SCTP_DISPOSITION_CONSUME;
5511 * sctp_sf_t1_init_timer_expire
5513 * Section: 4 Note: 2
5514 * Verification Tag:
5515 * Inputs
5516 * (endpoint, asoc)
5518 * RFC 2960 Section 4 Notes
5519 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5520 * and re-start the T1-init timer without changing state. This MUST
5521 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5522 * endpoint MUST abort the initialization process and report the
5523 * error to SCTP user.
5525 * Outputs
5526 * (timers, events)
5529 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5530 const struct sctp_endpoint *ep,
5531 const struct sctp_association *asoc,
5532 const sctp_subtype_t type,
5533 void *arg,
5534 sctp_cmd_seq_t *commands)
5536 struct sctp_chunk *repl = NULL;
5537 struct sctp_bind_addr *bp;
5538 int attempts = asoc->init_err_counter + 1;
5540 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5542 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5544 if (attempts <= asoc->max_init_attempts) {
5545 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5546 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5547 if (!repl)
5548 return SCTP_DISPOSITION_NOMEM;
5550 /* Choose transport for INIT. */
5551 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5552 SCTP_CHUNK(repl));
5554 /* Issue a sideeffect to do the needed accounting. */
5555 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5556 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5558 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5559 } else {
5560 pr_debug("%s: giving up on INIT, attempts:%d "
5561 "max_init_attempts:%d\n", __func__, attempts,
5562 asoc->max_init_attempts);
5564 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5565 SCTP_ERROR(ETIMEDOUT));
5566 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5567 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5568 return SCTP_DISPOSITION_DELETE_TCB;
5571 return SCTP_DISPOSITION_CONSUME;
5575 * sctp_sf_t1_cookie_timer_expire
5577 * Section: 4 Note: 2
5578 * Verification Tag:
5579 * Inputs
5580 * (endpoint, asoc)
5582 * RFC 2960 Section 4 Notes
5583 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5584 * COOKIE ECHO and re-start the T1-cookie timer without changing
5585 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5586 * After that, the endpoint MUST abort the initialization process and
5587 * report the error to SCTP user.
5589 * Outputs
5590 * (timers, events)
5593 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5594 const struct sctp_endpoint *ep,
5595 const struct sctp_association *asoc,
5596 const sctp_subtype_t type,
5597 void *arg,
5598 sctp_cmd_seq_t *commands)
5600 struct sctp_chunk *repl = NULL;
5601 int attempts = asoc->init_err_counter + 1;
5603 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5605 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5607 if (attempts <= asoc->max_init_attempts) {
5608 repl = sctp_make_cookie_echo(asoc, NULL);
5609 if (!repl)
5610 return SCTP_DISPOSITION_NOMEM;
5612 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5613 SCTP_CHUNK(repl));
5614 /* Issue a sideeffect to do the needed accounting. */
5615 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5616 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5618 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5619 } else {
5620 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5621 SCTP_ERROR(ETIMEDOUT));
5622 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5623 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5624 return SCTP_DISPOSITION_DELETE_TCB;
5627 return SCTP_DISPOSITION_CONSUME;
5630 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5631 * with the updated last sequential TSN received from its peer.
5633 * An endpoint should limit the number of retransmissions of the
5634 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5635 * If this threshold is exceeded the endpoint should destroy the TCB and
5636 * MUST report the peer endpoint unreachable to the upper layer (and
5637 * thus the association enters the CLOSED state). The reception of any
5638 * packet from its peer (i.e. as the peer sends all of its queued DATA
5639 * chunks) should clear the endpoint's retransmission count and restart
5640 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5641 * all of its queued DATA chunks that have not yet been sent.
5643 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5644 const struct sctp_endpoint *ep,
5645 const struct sctp_association *asoc,
5646 const sctp_subtype_t type,
5647 void *arg,
5648 sctp_cmd_seq_t *commands)
5650 struct sctp_chunk *reply = NULL;
5652 pr_debug("%s: timer T2 expired\n", __func__);
5654 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5656 ((struct sctp_association *)asoc)->shutdown_retries++;
5658 if (asoc->overall_error_count >= asoc->max_retrans) {
5659 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5660 SCTP_ERROR(ETIMEDOUT));
5661 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5662 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5663 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5664 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5665 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5666 return SCTP_DISPOSITION_DELETE_TCB;
5669 switch (asoc->state) {
5670 case SCTP_STATE_SHUTDOWN_SENT:
5671 reply = sctp_make_shutdown(asoc, NULL);
5672 break;
5674 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5675 reply = sctp_make_shutdown_ack(asoc, NULL);
5676 break;
5678 default:
5679 BUG();
5680 break;
5683 if (!reply)
5684 goto nomem;
5686 /* Do some failure management (Section 8.2).
5687 * If we remove the transport an SHUTDOWN was last sent to, don't
5688 * do failure management.
5690 if (asoc->shutdown_last_sent_to)
5691 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5692 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5694 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5695 * the T2-shutdown timer.
5697 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5699 /* Restart the T2-shutdown timer. */
5700 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5701 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5702 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5703 return SCTP_DISPOSITION_CONSUME;
5705 nomem:
5706 return SCTP_DISPOSITION_NOMEM;
5710 * ADDIP Section 4.1 ASCONF CHunk Procedures
5711 * If the T4 RTO timer expires the endpoint should do B1 to B5
5713 sctp_disposition_t sctp_sf_t4_timer_expire(
5714 struct net *net,
5715 const struct sctp_endpoint *ep,
5716 const struct sctp_association *asoc,
5717 const sctp_subtype_t type,
5718 void *arg,
5719 sctp_cmd_seq_t *commands)
5721 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5722 struct sctp_transport *transport = chunk->transport;
5724 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5726 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5727 * detection on the appropriate destination address as defined in
5728 * RFC2960 [5] section 8.1 and 8.2.
5730 if (transport)
5731 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5732 SCTP_TRANSPORT(transport));
5734 /* Reconfig T4 timer and transport. */
5735 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5737 /* ADDIP 4.1 B2) Increment the association error counters and perform
5738 * endpoint failure detection on the association as defined in
5739 * RFC2960 [5] section 8.1 and 8.2.
5740 * association error counter is incremented in SCTP_CMD_STRIKE.
5742 if (asoc->overall_error_count >= asoc->max_retrans) {
5743 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5744 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5745 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5746 SCTP_ERROR(ETIMEDOUT));
5747 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5748 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5749 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5750 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5751 return SCTP_DISPOSITION_ABORT;
5754 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5755 * the ASCONF chunk was sent by doubling the RTO timer value.
5756 * This is done in SCTP_CMD_STRIKE.
5759 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5760 * choose an alternate destination address (please refer to RFC2960
5761 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5762 * chunk, it MUST be the same (including its serial number) as the last
5763 * ASCONF sent.
5765 sctp_chunk_hold(asoc->addip_last_asconf);
5766 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5767 SCTP_CHUNK(asoc->addip_last_asconf));
5769 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5770 * destination is selected, then the RTO used will be that of the new
5771 * destination address.
5773 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5774 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5776 return SCTP_DISPOSITION_CONSUME;
5779 /* sctpimpguide-05 Section 2.12.2
5780 * The sender of the SHUTDOWN MAY also start an overall guard timer
5781 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5782 * At the expiration of this timer the sender SHOULD abort the association
5783 * by sending an ABORT chunk.
5785 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5786 const struct sctp_endpoint *ep,
5787 const struct sctp_association *asoc,
5788 const sctp_subtype_t type,
5789 void *arg,
5790 sctp_cmd_seq_t *commands)
5792 struct sctp_chunk *reply = NULL;
5794 pr_debug("%s: timer T5 expired\n", __func__);
5796 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5798 reply = sctp_make_abort(asoc, NULL, 0);
5799 if (!reply)
5800 goto nomem;
5802 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5803 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5804 SCTP_ERROR(ETIMEDOUT));
5805 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5806 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5808 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5809 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5811 return SCTP_DISPOSITION_DELETE_TCB;
5812 nomem:
5813 return SCTP_DISPOSITION_NOMEM;
5816 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5817 * the association is automatically closed by starting the shutdown process.
5818 * The work that needs to be done is same as when SHUTDOWN is initiated by
5819 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5821 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5822 struct net *net,
5823 const struct sctp_endpoint *ep,
5824 const struct sctp_association *asoc,
5825 const sctp_subtype_t type,
5826 void *arg,
5827 sctp_cmd_seq_t *commands)
5829 int disposition;
5831 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5833 /* From 9.2 Shutdown of an Association
5834 * Upon receipt of the SHUTDOWN primitive from its upper
5835 * layer, the endpoint enters SHUTDOWN-PENDING state and
5836 * remains there until all outstanding data has been
5837 * acknowledged by its peer. The endpoint accepts no new data
5838 * from its upper layer, but retransmits data to the far end
5839 * if necessary to fill gaps.
5841 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5842 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5844 disposition = SCTP_DISPOSITION_CONSUME;
5845 if (sctp_outq_is_empty(&asoc->outqueue)) {
5846 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5847 arg, commands);
5849 return disposition;
5852 /*****************************************************************************
5853 * These are sa state functions which could apply to all types of events.
5854 ****************************************************************************/
5857 * This table entry is not implemented.
5859 * Inputs
5860 * (endpoint, asoc, chunk)
5862 * The return value is the disposition of the chunk.
5864 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5865 const struct sctp_endpoint *ep,
5866 const struct sctp_association *asoc,
5867 const sctp_subtype_t type,
5868 void *arg,
5869 sctp_cmd_seq_t *commands)
5871 return SCTP_DISPOSITION_NOT_IMPL;
5875 * This table entry represents a bug.
5877 * Inputs
5878 * (endpoint, asoc, chunk)
5880 * The return value is the disposition of the chunk.
5882 sctp_disposition_t sctp_sf_bug(struct net *net,
5883 const struct sctp_endpoint *ep,
5884 const struct sctp_association *asoc,
5885 const sctp_subtype_t type,
5886 void *arg,
5887 sctp_cmd_seq_t *commands)
5889 return SCTP_DISPOSITION_BUG;
5893 * This table entry represents the firing of a timer in the wrong state.
5894 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5895 * when the association is in the wrong state. This event should
5896 * be ignored, so as to prevent any rearming of the timer.
5898 * Inputs
5899 * (endpoint, asoc, chunk)
5901 * The return value is the disposition of the chunk.
5903 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5904 const struct sctp_endpoint *ep,
5905 const struct sctp_association *asoc,
5906 const sctp_subtype_t type,
5907 void *arg,
5908 sctp_cmd_seq_t *commands)
5910 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5912 return SCTP_DISPOSITION_CONSUME;
5915 /********************************************************************
5916 * 2nd Level Abstractions
5917 ********************************************************************/
5919 /* Pull the SACK chunk based on the SACK header. */
5920 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5922 struct sctp_sackhdr *sack;
5923 unsigned int len;
5924 __u16 num_blocks;
5925 __u16 num_dup_tsns;
5927 /* Protect ourselves from reading too far into
5928 * the skb from a bogus sender.
5930 sack = (struct sctp_sackhdr *) chunk->skb->data;
5932 num_blocks = ntohs(sack->num_gap_ack_blocks);
5933 num_dup_tsns = ntohs(sack->num_dup_tsns);
5934 len = sizeof(struct sctp_sackhdr);
5935 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5936 if (len > chunk->skb->len)
5937 return NULL;
5939 skb_pull(chunk->skb, len);
5941 return sack;
5944 /* Create an ABORT packet to be sent as a response, with the specified
5945 * error causes.
5947 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5948 const struct sctp_endpoint *ep,
5949 const struct sctp_association *asoc,
5950 struct sctp_chunk *chunk,
5951 const void *payload,
5952 size_t paylen)
5954 struct sctp_packet *packet;
5955 struct sctp_chunk *abort;
5957 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5959 if (packet) {
5960 /* Make an ABORT.
5961 * The T bit will be set if the asoc is NULL.
5963 abort = sctp_make_abort(asoc, chunk, paylen);
5964 if (!abort) {
5965 sctp_ootb_pkt_free(packet);
5966 return NULL;
5969 /* Reflect vtag if T-Bit is set */
5970 if (sctp_test_T_bit(abort))
5971 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5973 /* Add specified error causes, i.e., payload, to the
5974 * end of the chunk.
5976 sctp_addto_chunk(abort, paylen, payload);
5978 /* Set the skb to the belonging sock for accounting. */
5979 abort->skb->sk = ep->base.sk;
5981 sctp_packet_append_chunk(packet, abort);
5985 return packet;
5988 /* Allocate a packet for responding in the OOTB conditions. */
5989 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5990 const struct sctp_association *asoc,
5991 const struct sctp_chunk *chunk)
5993 struct sctp_packet *packet;
5994 struct sctp_transport *transport;
5995 __u16 sport;
5996 __u16 dport;
5997 __u32 vtag;
5999 /* Get the source and destination port from the inbound packet. */
6000 sport = ntohs(chunk->sctp_hdr->dest);
6001 dport = ntohs(chunk->sctp_hdr->source);
6003 /* The V-tag is going to be the same as the inbound packet if no
6004 * association exists, otherwise, use the peer's vtag.
6006 if (asoc) {
6007 /* Special case the INIT-ACK as there is no peer's vtag
6008 * yet.
6010 switch(chunk->chunk_hdr->type) {
6011 case SCTP_CID_INIT_ACK:
6013 sctp_initack_chunk_t *initack;
6015 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6016 vtag = ntohl(initack->init_hdr.init_tag);
6017 break;
6019 default:
6020 vtag = asoc->peer.i.init_tag;
6021 break;
6023 } else {
6024 /* Special case the INIT and stale COOKIE_ECHO as there is no
6025 * vtag yet.
6027 switch(chunk->chunk_hdr->type) {
6028 case SCTP_CID_INIT:
6030 sctp_init_chunk_t *init;
6032 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6033 vtag = ntohl(init->init_hdr.init_tag);
6034 break;
6036 default:
6037 vtag = ntohl(chunk->sctp_hdr->vtag);
6038 break;
6042 /* Make a transport for the bucket, Eliza... */
6043 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6044 if (!transport)
6045 goto nomem;
6047 /* Cache a route for the transport with the chunk's destination as
6048 * the source address.
6050 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6051 sctp_sk(net->sctp.ctl_sock));
6053 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6054 packet = sctp_packet_config(packet, vtag, 0);
6056 return packet;
6058 nomem:
6059 return NULL;
6062 /* Free the packet allocated earlier for responding in the OOTB condition. */
6063 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6065 sctp_transport_free(packet->transport);
6068 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6069 static void sctp_send_stale_cookie_err(struct net *net,
6070 const struct sctp_endpoint *ep,
6071 const struct sctp_association *asoc,
6072 const struct sctp_chunk *chunk,
6073 sctp_cmd_seq_t *commands,
6074 struct sctp_chunk *err_chunk)
6076 struct sctp_packet *packet;
6078 if (err_chunk) {
6079 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6080 if (packet) {
6081 struct sctp_signed_cookie *cookie;
6083 /* Override the OOTB vtag from the cookie. */
6084 cookie = chunk->subh.cookie_hdr;
6085 packet->vtag = cookie->c.peer_vtag;
6087 /* Set the skb to the belonging sock for accounting. */
6088 err_chunk->skb->sk = ep->base.sk;
6089 sctp_packet_append_chunk(packet, err_chunk);
6090 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6091 SCTP_PACKET(packet));
6092 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6093 } else
6094 sctp_chunk_free (err_chunk);
6099 /* Process a data chunk */
6100 static int sctp_eat_data(const struct sctp_association *asoc,
6101 struct sctp_chunk *chunk,
6102 sctp_cmd_seq_t *commands)
6104 sctp_datahdr_t *data_hdr;
6105 struct sctp_chunk *err;
6106 size_t datalen;
6107 sctp_verb_t deliver;
6108 int tmp;
6109 __u32 tsn;
6110 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6111 struct sock *sk = asoc->base.sk;
6112 struct net *net = sock_net(sk);
6113 u16 ssn;
6114 u16 sid;
6115 u8 ordered = 0;
6117 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6118 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6120 tsn = ntohl(data_hdr->tsn);
6121 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6123 /* ASSERT: Now skb->data is really the user data. */
6125 /* Process ECN based congestion.
6127 * Since the chunk structure is reused for all chunks within
6128 * a packet, we use ecn_ce_done to track if we've already
6129 * done CE processing for this packet.
6131 * We need to do ECN processing even if we plan to discard the
6132 * chunk later.
6135 if (!chunk->ecn_ce_done) {
6136 struct sctp_af *af;
6137 chunk->ecn_ce_done = 1;
6139 af = sctp_get_af_specific(
6140 ipver2af(ip_hdr(chunk->skb)->version));
6142 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6143 /* Do real work as sideffect. */
6144 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6145 SCTP_U32(tsn));
6149 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6150 if (tmp < 0) {
6151 /* The TSN is too high--silently discard the chunk and
6152 * count on it getting retransmitted later.
6154 if (chunk->asoc)
6155 chunk->asoc->stats.outofseqtsns++;
6156 return SCTP_IERROR_HIGH_TSN;
6157 } else if (tmp > 0) {
6158 /* This is a duplicate. Record it. */
6159 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6160 return SCTP_IERROR_DUP_TSN;
6163 /* This is a new TSN. */
6165 /* Discard if there is no room in the receive window.
6166 * Actually, allow a little bit of overflow (up to a MTU).
6168 datalen = ntohs(chunk->chunk_hdr->length);
6169 datalen -= sizeof(sctp_data_chunk_t);
6171 deliver = SCTP_CMD_CHUNK_ULP;
6173 /* Think about partial delivery. */
6174 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6176 /* Even if we don't accept this chunk there is
6177 * memory pressure.
6179 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6182 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6183 * seems a bit troublesome in that frag_point varies based on
6184 * PMTU. In cases, such as loopback, this might be a rather
6185 * large spill over.
6187 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6188 (datalen > asoc->rwnd + asoc->frag_point))) {
6190 /* If this is the next TSN, consider reneging to make
6191 * room. Note: Playing nice with a confused sender. A
6192 * malicious sender can still eat up all our buffer
6193 * space and in the future we may want to detect and
6194 * do more drastic reneging.
6196 if (sctp_tsnmap_has_gap(map) &&
6197 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6198 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6199 deliver = SCTP_CMD_RENEGE;
6200 } else {
6201 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6202 __func__, tsn, datalen, asoc->rwnd);
6204 return SCTP_IERROR_IGNORE_TSN;
6209 * Also try to renege to limit our memory usage in the event that
6210 * we are under memory pressure
6211 * If we can't renege, don't worry about it, the sk_rmem_schedule
6212 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6213 * memory usage too much
6215 if (*sk->sk_prot_creator->memory_pressure) {
6216 if (sctp_tsnmap_has_gap(map) &&
6217 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6218 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6219 __func__, tsn);
6220 deliver = SCTP_CMD_RENEGE;
6225 * Section 3.3.10.9 No User Data (9)
6227 * Cause of error
6228 * ---------------
6229 * No User Data: This error cause is returned to the originator of a
6230 * DATA chunk if a received DATA chunk has no user data.
6232 if (unlikely(0 == datalen)) {
6233 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6234 if (err) {
6235 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6236 SCTP_CHUNK(err));
6238 /* We are going to ABORT, so we might as well stop
6239 * processing the rest of the chunks in the packet.
6241 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6242 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6243 SCTP_ERROR(ECONNABORTED));
6244 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6245 SCTP_PERR(SCTP_ERROR_NO_DATA));
6246 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6247 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6248 return SCTP_IERROR_NO_DATA;
6251 chunk->data_accepted = 1;
6253 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6254 * if we renege and the chunk arrives again.
6256 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6257 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6258 if (chunk->asoc)
6259 chunk->asoc->stats.iuodchunks++;
6260 } else {
6261 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6262 if (chunk->asoc)
6263 chunk->asoc->stats.iodchunks++;
6264 ordered = 1;
6267 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6269 * If an endpoint receive a DATA chunk with an invalid stream
6270 * identifier, it shall acknowledge the reception of the DATA chunk
6271 * following the normal procedure, immediately send an ERROR chunk
6272 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6273 * and discard the DATA chunk.
6275 sid = ntohs(data_hdr->stream);
6276 if (sid >= asoc->c.sinit_max_instreams) {
6277 /* Mark tsn as received even though we drop it */
6278 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6280 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6281 &data_hdr->stream,
6282 sizeof(data_hdr->stream),
6283 sizeof(u16));
6284 if (err)
6285 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6286 SCTP_CHUNK(err));
6287 return SCTP_IERROR_BAD_STREAM;
6290 /* Check to see if the SSN is possible for this TSN.
6291 * The biggest gap we can record is 4K wide. Since SSNs wrap
6292 * at an unsigned short, there is no way that an SSN can
6293 * wrap and for a valid TSN. We can simply check if the current
6294 * SSN is smaller then the next expected one. If it is, it wrapped
6295 * and is invalid.
6297 ssn = ntohs(data_hdr->ssn);
6298 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6299 return SCTP_IERROR_PROTO_VIOLATION;
6302 /* Send the data up to the user. Note: Schedule the
6303 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6304 * chunk needs the updated rwnd.
6306 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6308 return SCTP_IERROR_NO_ERROR;