Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / sctp / sm_statefuns.c
blobaf2b7041fa4eba75b0098517c4a552c6f9864366
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2002 Intel Corp.
7 * Copyright (c) 2002 Nokia Corp.
9 * This is part of the SCTP Linux Kernel Implementation.
11 * These are the state functions for the state machine.
13 * Please send any bug reports or fixes you make to the
14 * email address(es):
15 * lksctp developers <linux-sctp@vger.kernel.org>
17 * Written or modified by:
18 * La Monte H.P. Yarroll <piggy@acm.org>
19 * Karl Knutson <karl@athena.chicago.il.us>
20 * Mathew Kotowsky <kotowsky@sctp.org>
21 * Sridhar Samudrala <samudrala@us.ibm.com>
22 * Jon Grimm <jgrimm@us.ibm.com>
23 * Hui Huang <hui.huang@nokia.com>
24 * Dajiang Zhang <dajiang.zhang@nokia.com>
25 * Daisy Chang <daisyc@us.ibm.com>
26 * Ardelle Fan <ardelle.fan@intel.com>
27 * Ryan Layer <rmlayer@us.ibm.com>
28 * Kevin Gao <kevin.gao@intel.com>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/net.h>
38 #include <linux/inet.h>
39 #include <linux/slab.h>
40 #include <net/sock.h>
41 #include <net/inet_ecn.h>
42 #include <linux/skbuff.h>
43 #include <net/sctp/sctp.h>
44 #include <net/sctp/sm.h>
45 #include <net/sctp/structs.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/sctp.h>
50 static struct sctp_packet *sctp_abort_pkt_new(
51 struct net *net,
52 const struct sctp_endpoint *ep,
53 const struct sctp_association *asoc,
54 struct sctp_chunk *chunk,
55 const void *payload, size_t paylen);
56 static int sctp_eat_data(const struct sctp_association *asoc,
57 struct sctp_chunk *chunk,
58 struct sctp_cmd_seq *commands);
59 static struct sctp_packet *sctp_ootb_pkt_new(
60 struct net *net,
61 const struct sctp_association *asoc,
62 const struct sctp_chunk *chunk);
63 static void sctp_send_stale_cookie_err(struct net *net,
64 const struct sctp_endpoint *ep,
65 const struct sctp_association *asoc,
66 const struct sctp_chunk *chunk,
67 struct sctp_cmd_seq *commands,
68 struct sctp_chunk *err_chunk);
69 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
70 struct net *net,
71 const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const union sctp_subtype type,
74 void *arg,
75 struct sctp_cmd_seq *commands);
76 static enum sctp_disposition sctp_sf_shut_8_4_5(
77 struct net *net,
78 const struct sctp_endpoint *ep,
79 const struct sctp_association *asoc,
80 const union sctp_subtype type,
81 void *arg,
82 struct sctp_cmd_seq *commands);
83 static enum sctp_disposition sctp_sf_tabort_8_4_8(
84 struct net *net,
85 const struct sctp_endpoint *ep,
86 const struct sctp_association *asoc,
87 const union sctp_subtype type,
88 void *arg,
89 struct sctp_cmd_seq *commands);
90 static enum sctp_disposition sctp_sf_new_encap_port(
91 struct net *net,
92 const struct sctp_endpoint *ep,
93 const struct sctp_association *asoc,
94 const union sctp_subtype type,
95 void *arg,
96 struct sctp_cmd_seq *commands);
97 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99 static enum sctp_disposition sctp_stop_t1_and_abort(
100 struct net *net,
101 struct sctp_cmd_seq *commands,
102 __be16 error, int sk_err,
103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
106 static enum sctp_disposition sctp_sf_abort_violation(
107 struct net *net,
108 const struct sctp_endpoint *ep,
109 const struct sctp_association *asoc,
110 void *arg,
111 struct sctp_cmd_seq *commands,
112 const __u8 *payload,
113 const size_t paylen);
115 static enum sctp_disposition sctp_sf_violation_chunklen(
116 struct net *net,
117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const union sctp_subtype type,
120 void *arg,
121 struct sctp_cmd_seq *commands);
123 static enum sctp_disposition sctp_sf_violation_paramlen(
124 struct net *net,
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const union sctp_subtype type,
128 void *arg, void *ext,
129 struct sctp_cmd_seq *commands);
131 static enum sctp_disposition sctp_sf_violation_ctsn(
132 struct net *net,
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const union sctp_subtype type,
136 void *arg,
137 struct sctp_cmd_seq *commands);
139 static enum sctp_disposition sctp_sf_violation_chunk(
140 struct net *net,
141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const union sctp_subtype type,
144 void *arg,
145 struct sctp_cmd_seq *commands);
147 static enum sctp_ierror sctp_sf_authenticate(
148 const struct sctp_association *asoc,
149 struct sctp_chunk *chunk);
151 static enum sctp_disposition __sctp_sf_do_9_1_abort(
152 struct net *net,
153 const struct sctp_endpoint *ep,
154 const struct sctp_association *asoc,
155 const union sctp_subtype type,
156 void *arg,
157 struct sctp_cmd_seq *commands);
159 /* Small helper function that checks if the chunk length
160 * is of the appropriate length. The 'required_length' argument
161 * is set to be the size of a specific chunk we are testing.
162 * Return Values: true = Valid length
163 * false = Invalid length
166 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
167 __u16 required_length)
169 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
171 /* Previously already marked? */
172 if (unlikely(chunk->pdiscard))
173 return false;
174 if (unlikely(chunk_length < required_length))
175 return false;
177 return true;
180 /* Check for format error in an ABORT chunk */
181 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
183 struct sctp_errhdr *err;
185 sctp_walk_errors(err, chunk->chunk_hdr);
187 return (void *)err == (void *)chunk->chunk_end;
190 /**********************************************************
191 * These are the state functions for handling chunk events.
192 **********************************************************/
195 * Process the final SHUTDOWN COMPLETE.
197 * Section: 4 (C) (diagram), 9.2
198 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
199 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
200 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
201 * should stop the T2-shutdown timer and remove all knowledge of the
202 * association (and thus the association enters the CLOSED state).
204 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
205 * C) Rules for packet carrying SHUTDOWN COMPLETE:
206 * ...
207 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
208 * if the Verification Tag field of the packet matches its own tag and
209 * the T bit is not set
210 * OR
211 * it is set to its peer's tag and the T bit is set in the Chunk
212 * Flags.
213 * Otherwise, the receiver MUST silently discard the packet
214 * and take no further action. An endpoint MUST ignore the
215 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
217 * Inputs
218 * (endpoint, asoc, chunk)
220 * Outputs
221 * (asoc, reply_msg, msg_up, timers, counters)
223 * The return value is the disposition of the chunk.
225 enum sctp_disposition sctp_sf_do_4_C(struct net *net,
226 const struct sctp_endpoint *ep,
227 const struct sctp_association *asoc,
228 const union sctp_subtype type,
229 void *arg, struct sctp_cmd_seq *commands)
231 struct sctp_chunk *chunk = arg;
232 struct sctp_ulpevent *ev;
234 if (!sctp_vtag_verify_either(chunk, asoc))
235 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
237 /* RFC 2960 6.10 Bundling
239 * An endpoint MUST NOT bundle INIT, INIT ACK or
240 * SHUTDOWN COMPLETE with any other chunks.
242 if (!chunk->singleton)
243 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
245 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
246 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
247 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
248 commands);
250 /* RFC 2960 10.2 SCTP-to-ULP
252 * H) SHUTDOWN COMPLETE notification
254 * When SCTP completes the shutdown procedures (section 9.2) this
255 * notification is passed to the upper layer.
257 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
258 0, 0, 0, NULL, GFP_ATOMIC);
259 if (ev)
260 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
261 SCTP_ULPEVENT(ev));
263 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
264 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
265 * not the chunk should be discarded. If the endpoint is in
266 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
267 * T2-shutdown timer and remove all knowledge of the
268 * association (and thus the association enters the CLOSED
269 * state).
271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
272 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
274 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
275 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
277 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
278 SCTP_STATE(SCTP_STATE_CLOSED));
280 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
281 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
283 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
285 return SCTP_DISPOSITION_DELETE_TCB;
289 * Respond to a normal INIT chunk.
290 * We are the side that is being asked for an association.
292 * Section: 5.1 Normal Establishment of an Association, B
293 * B) "Z" shall respond immediately with an INIT ACK chunk. The
294 * destination IP address of the INIT ACK MUST be set to the source
295 * IP address of the INIT to which this INIT ACK is responding. In
296 * the response, besides filling in other parameters, "Z" must set the
297 * Verification Tag field to Tag_A, and also provide its own
298 * Verification Tag (Tag_Z) in the Initiate Tag field.
300 * Verification Tag: Must be 0.
302 * Inputs
303 * (endpoint, asoc, chunk)
305 * Outputs
306 * (asoc, reply_msg, msg_up, timers, counters)
308 * The return value is the disposition of the chunk.
310 enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
311 const struct sctp_endpoint *ep,
312 const struct sctp_association *asoc,
313 const union sctp_subtype type,
314 void *arg,
315 struct sctp_cmd_seq *commands)
317 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
318 struct sctp_unrecognized_param *unk_param;
319 struct sctp_association *new_asoc;
320 struct sctp_packet *packet;
321 int len;
323 /* Update socket peer label if first association. */
324 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
325 chunk->skb))
326 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
328 /* 6.10 Bundling
329 * An endpoint MUST NOT bundle INIT, INIT ACK or
330 * SHUTDOWN COMPLETE with any other chunks.
332 * IG Section 2.11.2
333 * Furthermore, we require that the receiver of an INIT chunk MUST
334 * enforce these rules by silently discarding an arriving packet
335 * with an INIT chunk that is bundled with other chunks.
337 if (!chunk->singleton)
338 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
340 /* If the packet is an OOTB packet which is temporarily on the
341 * control endpoint, respond with an ABORT.
343 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
344 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
345 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
348 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
349 * Tag.
351 if (chunk->sctp_hdr->vtag != 0)
352 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
354 /* Make sure that the INIT chunk has a valid length.
355 * Normally, this would cause an ABORT with a Protocol Violation
356 * error, but since we don't have an association, we'll
357 * just discard the packet.
359 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
360 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
362 /* If the INIT is coming toward a closing socket, we'll send back
363 * and ABORT. Essentially, this catches the race of INIT being
364 * backloged to the socket at the same time as the user isses close().
365 * Since the socket and all its associations are going away, we
366 * can treat this OOTB
368 if (sctp_sstate(ep->base.sk, CLOSING))
369 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
371 /* Verify the INIT chunk before processing it. */
372 err_chunk = NULL;
373 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
374 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
375 &err_chunk)) {
376 /* This chunk contains fatal error. It is to be discarded.
377 * Send an ABORT, with causes if there is any.
379 if (err_chunk) {
380 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
381 (__u8 *)(err_chunk->chunk_hdr) +
382 sizeof(struct sctp_chunkhdr),
383 ntohs(err_chunk->chunk_hdr->length) -
384 sizeof(struct sctp_chunkhdr));
386 sctp_chunk_free(err_chunk);
388 if (packet) {
389 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
390 SCTP_PACKET(packet));
391 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
392 return SCTP_DISPOSITION_CONSUME;
393 } else {
394 return SCTP_DISPOSITION_NOMEM;
396 } else {
397 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
398 commands);
402 /* Grab the INIT header. */
403 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
405 /* Tag the variable length parameters. */
406 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
408 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
409 if (!new_asoc)
410 goto nomem;
412 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
413 sctp_scope(sctp_source(chunk)),
414 GFP_ATOMIC) < 0)
415 goto nomem_init;
417 /* The call, sctp_process_init(), can fail on memory allocation. */
418 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
419 (struct sctp_init_chunk *)chunk->chunk_hdr,
420 GFP_ATOMIC))
421 goto nomem_init;
423 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
425 /* If there are errors need to be reported for unknown parameters,
426 * make sure to reserve enough room in the INIT ACK for them.
428 len = 0;
429 if (err_chunk)
430 len = ntohs(err_chunk->chunk_hdr->length) -
431 sizeof(struct sctp_chunkhdr);
433 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
434 if (!repl)
435 goto nomem_init;
437 /* If there are errors need to be reported for unknown parameters,
438 * include them in the outgoing INIT ACK as "Unrecognized parameter"
439 * parameter.
441 if (err_chunk) {
442 /* Get the "Unrecognized parameter" parameter(s) out of the
443 * ERROR chunk generated by sctp_verify_init(). Since the
444 * error cause code for "unknown parameter" and the
445 * "Unrecognized parameter" type is the same, we can
446 * construct the parameters in INIT ACK by copying the
447 * ERROR causes over.
449 unk_param = (struct sctp_unrecognized_param *)
450 ((__u8 *)(err_chunk->chunk_hdr) +
451 sizeof(struct sctp_chunkhdr));
452 /* Replace the cause code with the "Unrecognized parameter"
453 * parameter type.
455 sctp_addto_chunk(repl, len, unk_param);
456 sctp_chunk_free(err_chunk);
459 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
461 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
464 * Note: After sending out INIT ACK with the State Cookie parameter,
465 * "Z" MUST NOT allocate any resources, nor keep any states for the
466 * new association. Otherwise, "Z" will be vulnerable to resource
467 * attacks.
469 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
471 return SCTP_DISPOSITION_DELETE_TCB;
473 nomem_init:
474 sctp_association_free(new_asoc);
475 nomem:
476 if (err_chunk)
477 sctp_chunk_free(err_chunk);
478 return SCTP_DISPOSITION_NOMEM;
482 * Respond to a normal INIT ACK chunk.
483 * We are the side that is initiating the association.
485 * Section: 5.1 Normal Establishment of an Association, C
486 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
487 * timer and leave COOKIE-WAIT state. "A" shall then send the State
488 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
489 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
491 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
492 * DATA chunks, but it MUST be the first chunk in the packet and
493 * until the COOKIE ACK is returned the sender MUST NOT send any
494 * other packets to the peer.
496 * Verification Tag: 3.3.3
497 * If the value of the Initiate Tag in a received INIT ACK chunk is
498 * found to be 0, the receiver MUST treat it as an error and close the
499 * association by transmitting an ABORT.
501 * Inputs
502 * (endpoint, asoc, chunk)
504 * Outputs
505 * (asoc, reply_msg, msg_up, timers, counters)
507 * The return value is the disposition of the chunk.
509 enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
510 const struct sctp_endpoint *ep,
511 const struct sctp_association *asoc,
512 const union sctp_subtype type,
513 void *arg,
514 struct sctp_cmd_seq *commands)
516 struct sctp_init_chunk *initchunk;
517 struct sctp_chunk *chunk = arg;
518 struct sctp_chunk *err_chunk;
519 struct sctp_packet *packet;
521 if (!sctp_vtag_verify(chunk, asoc))
522 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
524 /* 6.10 Bundling
525 * An endpoint MUST NOT bundle INIT, INIT ACK or
526 * SHUTDOWN COMPLETE with any other chunks.
528 if (!chunk->singleton)
529 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
531 /* Make sure that the INIT-ACK chunk has a valid length */
532 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
533 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
534 commands);
535 /* Grab the INIT header. */
536 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
538 /* Verify the INIT chunk before processing it. */
539 err_chunk = NULL;
540 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
541 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
542 &err_chunk)) {
544 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
546 /* This chunk contains fatal error. It is to be discarded.
547 * Send an ABORT, with causes. If there are no causes,
548 * then there wasn't enough memory. Just terminate
549 * the association.
551 if (err_chunk) {
552 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
553 (__u8 *)(err_chunk->chunk_hdr) +
554 sizeof(struct sctp_chunkhdr),
555 ntohs(err_chunk->chunk_hdr->length) -
556 sizeof(struct sctp_chunkhdr));
558 sctp_chunk_free(err_chunk);
560 if (packet) {
561 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
562 SCTP_PACKET(packet));
563 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
564 error = SCTP_ERROR_INV_PARAM;
568 /* SCTP-AUTH, Section 6.3:
569 * It should be noted that if the receiver wants to tear
570 * down an association in an authenticated way only, the
571 * handling of malformed packets should not result in
572 * tearing down the association.
574 * This means that if we only want to abort associations
575 * in an authenticated way (i.e AUTH+ABORT), then we
576 * can't destroy this association just because the packet
577 * was malformed.
579 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
580 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
582 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
583 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
584 asoc, chunk->transport);
587 /* Tag the variable length parameters. Note that we never
588 * convert the parameters in an INIT chunk.
590 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
592 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
594 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
595 SCTP_PEER_INIT(initchunk));
597 /* Reset init error count upon receipt of INIT-ACK. */
598 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
600 /* 5.1 C) "A" shall stop the T1-init timer and leave
601 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
602 * timer, and enter the COOKIE-ECHOED state.
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
606 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
607 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
608 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
609 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
611 /* SCTP-AUTH: genereate the assocition shared keys so that
612 * we can potentially signe the COOKIE-ECHO.
614 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
616 /* 5.1 C) "A" shall then send the State Cookie received in the
617 * INIT ACK chunk in a COOKIE ECHO chunk, ...
619 /* If there is any errors to report, send the ERROR chunk generated
620 * for unknown parameters as well.
622 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
623 SCTP_CHUNK(err_chunk));
625 return SCTP_DISPOSITION_CONSUME;
628 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
629 const struct sctp_association *asoc)
631 struct sctp_chunk auth;
633 if (!chunk->auth_chunk)
634 return true;
636 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
637 * is supposed to be authenticated and we have to do delayed
638 * authentication. We've just recreated the association using
639 * the information in the cookie and now it's much easier to
640 * do the authentication.
643 /* Make sure that we and the peer are AUTH capable */
644 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
645 return false;
647 /* set-up our fake chunk so that we can process it */
648 auth.skb = chunk->auth_chunk;
649 auth.asoc = chunk->asoc;
650 auth.sctp_hdr = chunk->sctp_hdr;
651 auth.chunk_hdr = (struct sctp_chunkhdr *)
652 skb_push(chunk->auth_chunk,
653 sizeof(struct sctp_chunkhdr));
654 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
655 auth.transport = chunk->transport;
657 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
661 * Respond to a normal COOKIE ECHO chunk.
662 * We are the side that is being asked for an association.
664 * Section: 5.1 Normal Establishment of an Association, D
665 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
666 * with a COOKIE ACK chunk after building a TCB and moving to
667 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
668 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
669 * chunk MUST be the first chunk in the packet.
671 * IMPLEMENTATION NOTE: An implementation may choose to send the
672 * Communication Up notification to the SCTP user upon reception
673 * of a valid COOKIE ECHO chunk.
675 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
676 * D) Rules for packet carrying a COOKIE ECHO
678 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
679 * Initial Tag received in the INIT ACK.
681 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
683 * Inputs
684 * (endpoint, asoc, chunk)
686 * Outputs
687 * (asoc, reply_msg, msg_up, timers, counters)
689 * The return value is the disposition of the chunk.
691 enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
692 const struct sctp_endpoint *ep,
693 const struct sctp_association *asoc,
694 const union sctp_subtype type,
695 void *arg,
696 struct sctp_cmd_seq *commands)
698 struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
699 struct sctp_association *new_asoc;
700 struct sctp_init_chunk *peer_init;
701 struct sctp_chunk *chunk = arg;
702 struct sctp_chunk *err_chk_p;
703 struct sctp_chunk *repl;
704 struct sock *sk;
705 int error = 0;
707 /* If the packet is an OOTB packet which is temporarily on the
708 * control endpoint, respond with an ABORT.
710 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
711 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
712 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
715 /* Make sure that the COOKIE_ECHO chunk has a valid length.
716 * In this case, we check that we have enough for at least a
717 * chunk header. More detailed verification is done
718 * in sctp_unpack_cookie().
720 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
721 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
723 /* If the endpoint is not listening or if the number of associations
724 * on the TCP-style socket exceed the max backlog, respond with an
725 * ABORT.
727 sk = ep->base.sk;
728 if (!sctp_sstate(sk, LISTENING) ||
729 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
730 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
732 /* "Decode" the chunk. We have no optional parameters so we
733 * are in good shape.
735 chunk->subh.cookie_hdr =
736 (struct sctp_signed_cookie *)chunk->skb->data;
737 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
738 sizeof(struct sctp_chunkhdr)))
739 goto nomem;
741 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
742 * "Z" will reply with a COOKIE ACK chunk after building a TCB
743 * and moving to the ESTABLISHED state.
745 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
746 &err_chk_p);
748 /* FIXME:
749 * If the re-build failed, what is the proper error path
750 * from here?
752 * [We should abort the association. --piggy]
754 if (!new_asoc) {
755 /* FIXME: Several errors are possible. A bad cookie should
756 * be silently discarded, but think about logging it too.
758 switch (error) {
759 case -SCTP_IERROR_NOMEM:
760 goto nomem;
762 case -SCTP_IERROR_STALE_COOKIE:
763 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
764 err_chk_p);
765 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
767 case -SCTP_IERROR_BAD_SIG:
768 default:
769 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
774 /* Delay state machine commands until later.
776 * Re-build the bind address for the association is done in
777 * the sctp_unpack_cookie() already.
779 /* This is a brand-new association, so these are not yet side
780 * effects--it is safe to run them here.
782 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
784 if (!sctp_process_init(new_asoc, chunk,
785 &chunk->subh.cookie_hdr->c.peer_addr,
786 peer_init, GFP_ATOMIC))
787 goto nomem_init;
789 /* SCTP-AUTH: Now that we've populate required fields in
790 * sctp_process_init, set up the assocaition shared keys as
791 * necessary so that we can potentially authenticate the ACK
793 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
794 if (error)
795 goto nomem_init;
797 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
798 sctp_association_free(new_asoc);
799 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
802 repl = sctp_make_cookie_ack(new_asoc, chunk);
803 if (!repl)
804 goto nomem_init;
806 /* RFC 2960 5.1 Normal Establishment of an Association
808 * D) IMPLEMENTATION NOTE: An implementation may choose to
809 * send the Communication Up notification to the SCTP user
810 * upon reception of a valid COOKIE ECHO chunk.
812 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
813 new_asoc->c.sinit_num_ostreams,
814 new_asoc->c.sinit_max_instreams,
815 NULL, GFP_ATOMIC);
816 if (!ev)
817 goto nomem_ev;
819 /* Sockets API Draft Section 5.3.1.6
820 * When a peer sends a Adaptation Layer Indication parameter , SCTP
821 * delivers this notification to inform the application that of the
822 * peers requested adaptation layer.
824 if (new_asoc->peer.adaptation_ind) {
825 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
826 GFP_ATOMIC);
827 if (!ai_ev)
828 goto nomem_aiev;
831 if (!new_asoc->peer.auth_capable) {
832 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
833 SCTP_AUTH_NO_AUTH,
834 GFP_ATOMIC);
835 if (!auth_ev)
836 goto nomem_authev;
839 /* Add all the state machine commands now since we've created
840 * everything. This way we don't introduce memory corruptions
841 * during side-effect processing and correclty count established
842 * associations.
844 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
845 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
846 SCTP_STATE(SCTP_STATE_ESTABLISHED));
847 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
848 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
849 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
851 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
852 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
853 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
855 /* This will send the COOKIE ACK */
856 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
858 /* Queue the ASSOC_CHANGE event */
859 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
861 /* Send up the Adaptation Layer Indication event */
862 if (ai_ev)
863 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
864 SCTP_ULPEVENT(ai_ev));
866 if (auth_ev)
867 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
868 SCTP_ULPEVENT(auth_ev));
870 return SCTP_DISPOSITION_CONSUME;
872 nomem_authev:
873 sctp_ulpevent_free(ai_ev);
874 nomem_aiev:
875 sctp_ulpevent_free(ev);
876 nomem_ev:
877 sctp_chunk_free(repl);
878 nomem_init:
879 sctp_association_free(new_asoc);
880 nomem:
881 return SCTP_DISPOSITION_NOMEM;
885 * Respond to a normal COOKIE ACK chunk.
886 * We are the side that is asking for an association.
888 * RFC 2960 5.1 Normal Establishment of an Association
890 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
891 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
892 * timer. It may also notify its ULP about the successful
893 * establishment of the association with a Communication Up
894 * notification (see Section 10).
896 * Verification Tag:
897 * Inputs
898 * (endpoint, asoc, chunk)
900 * Outputs
901 * (asoc, reply_msg, msg_up, timers, counters)
903 * The return value is the disposition of the chunk.
905 enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
906 const struct sctp_endpoint *ep,
907 const struct sctp_association *asoc,
908 const union sctp_subtype type,
909 void *arg,
910 struct sctp_cmd_seq *commands)
912 struct sctp_chunk *chunk = arg;
913 struct sctp_ulpevent *ev;
915 if (!sctp_vtag_verify(chunk, asoc))
916 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
918 /* Verify that the chunk length for the COOKIE-ACK is OK.
919 * If we don't do this, any bundled chunks may be junked.
921 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
922 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
923 commands);
925 /* Reset init error count upon receipt of COOKIE-ACK,
926 * to avoid problems with the managemement of this
927 * counter in stale cookie situations when a transition back
928 * from the COOKIE-ECHOED state to the COOKIE-WAIT
929 * state is performed.
931 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
933 /* Set peer label for connection. */
934 security_inet_conn_established(ep->base.sk, chunk->skb);
936 /* RFC 2960 5.1 Normal Establishment of an Association
938 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
939 * from the COOKIE-ECHOED state to the ESTABLISHED state,
940 * stopping the T1-cookie timer.
942 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
943 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
944 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
945 SCTP_STATE(SCTP_STATE_ESTABLISHED));
946 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
947 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
948 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
949 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
950 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
951 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
953 /* It may also notify its ULP about the successful
954 * establishment of the association with a Communication Up
955 * notification (see Section 10).
957 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
958 0, asoc->c.sinit_num_ostreams,
959 asoc->c.sinit_max_instreams,
960 NULL, GFP_ATOMIC);
962 if (!ev)
963 goto nomem;
965 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
967 /* Sockets API Draft Section 5.3.1.6
968 * When a peer sends a Adaptation Layer Indication parameter , SCTP
969 * delivers this notification to inform the application that of the
970 * peers requested adaptation layer.
972 if (asoc->peer.adaptation_ind) {
973 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
974 if (!ev)
975 goto nomem;
977 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
978 SCTP_ULPEVENT(ev));
981 if (!asoc->peer.auth_capable) {
982 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
983 GFP_ATOMIC);
984 if (!ev)
985 goto nomem;
986 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
987 SCTP_ULPEVENT(ev));
990 return SCTP_DISPOSITION_CONSUME;
991 nomem:
992 return SCTP_DISPOSITION_NOMEM;
995 /* Generate and sendout a heartbeat packet. */
996 static enum sctp_disposition sctp_sf_heartbeat(
997 const struct sctp_endpoint *ep,
998 const struct sctp_association *asoc,
999 const union sctp_subtype type,
1000 void *arg,
1001 struct sctp_cmd_seq *commands)
1003 struct sctp_transport *transport = (struct sctp_transport *) arg;
1004 struct sctp_chunk *reply;
1006 /* Send a heartbeat to our peer. */
1007 reply = sctp_make_heartbeat(asoc, transport);
1008 if (!reply)
1009 return SCTP_DISPOSITION_NOMEM;
1011 /* Set rto_pending indicating that an RTT measurement
1012 * is started with this heartbeat chunk.
1014 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1015 SCTP_TRANSPORT(transport));
1017 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1018 return SCTP_DISPOSITION_CONSUME;
1021 /* Generate a HEARTBEAT packet on the given transport. */
1022 enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1023 const struct sctp_endpoint *ep,
1024 const struct sctp_association *asoc,
1025 const union sctp_subtype type,
1026 void *arg,
1027 struct sctp_cmd_seq *commands)
1029 struct sctp_transport *transport = (struct sctp_transport *) arg;
1031 if (asoc->overall_error_count >= asoc->max_retrans) {
1032 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1033 SCTP_ERROR(ETIMEDOUT));
1034 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1035 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1036 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1037 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1038 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1039 return SCTP_DISPOSITION_DELETE_TCB;
1042 /* Section 3.3.5.
1043 * The Sender-specific Heartbeat Info field should normally include
1044 * information about the sender's current time when this HEARTBEAT
1045 * chunk is sent and the destination transport address to which this
1046 * HEARTBEAT is sent (see Section 8.3).
1049 if (transport->param_flags & SPP_HB_ENABLE) {
1050 if (SCTP_DISPOSITION_NOMEM ==
1051 sctp_sf_heartbeat(ep, asoc, type, arg,
1052 commands))
1053 return SCTP_DISPOSITION_NOMEM;
1055 /* Set transport error counter and association error counter
1056 * when sending heartbeat.
1058 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1059 SCTP_TRANSPORT(transport));
1061 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1062 SCTP_TRANSPORT(transport));
1063 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1064 SCTP_TRANSPORT(transport));
1066 return SCTP_DISPOSITION_CONSUME;
1069 /* resend asoc strreset_chunk. */
1070 enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1071 const struct sctp_endpoint *ep,
1072 const struct sctp_association *asoc,
1073 const union sctp_subtype type,
1074 void *arg,
1075 struct sctp_cmd_seq *commands)
1077 struct sctp_transport *transport = arg;
1079 if (asoc->overall_error_count >= asoc->max_retrans) {
1080 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1081 SCTP_ERROR(ETIMEDOUT));
1082 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1083 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1084 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1085 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1086 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1087 return SCTP_DISPOSITION_DELETE_TCB;
1090 sctp_chunk_hold(asoc->strreset_chunk);
1091 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1092 SCTP_CHUNK(asoc->strreset_chunk));
1093 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1095 return SCTP_DISPOSITION_CONSUME;
1099 * Process an heartbeat request.
1101 * Section: 8.3 Path Heartbeat
1102 * The receiver of the HEARTBEAT should immediately respond with a
1103 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1104 * from the received HEARTBEAT chunk.
1106 * Verification Tag: 8.5 Verification Tag [Normal verification]
1107 * When receiving an SCTP packet, the endpoint MUST ensure that the
1108 * value in the Verification Tag field of the received SCTP packet
1109 * matches its own Tag. If the received Verification Tag value does not
1110 * match the receiver's own tag value, the receiver shall silently
1111 * discard the packet and shall not process it any further except for
1112 * those cases listed in Section 8.5.1 below.
1114 * Inputs
1115 * (endpoint, asoc, chunk)
1117 * Outputs
1118 * (asoc, reply_msg, msg_up, timers, counters)
1120 * The return value is the disposition of the chunk.
1122 enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1123 const struct sctp_endpoint *ep,
1124 const struct sctp_association *asoc,
1125 const union sctp_subtype type,
1126 void *arg, struct sctp_cmd_seq *commands)
1128 struct sctp_paramhdr *param_hdr;
1129 struct sctp_chunk *chunk = arg;
1130 struct sctp_chunk *reply;
1131 size_t paylen = 0;
1133 if (!sctp_vtag_verify(chunk, asoc))
1134 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1136 /* Make sure that the HEARTBEAT chunk has a valid length. */
1137 if (!sctp_chunk_length_valid(chunk,
1138 sizeof(struct sctp_heartbeat_chunk)))
1139 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1140 commands);
1142 /* 8.3 The receiver of the HEARTBEAT should immediately
1143 * respond with a HEARTBEAT ACK that contains the Heartbeat
1144 * Information field copied from the received HEARTBEAT chunk.
1146 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1147 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1148 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1150 if (ntohs(param_hdr->length) > paylen)
1151 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1152 param_hdr, commands);
1154 if (!pskb_pull(chunk->skb, paylen))
1155 goto nomem;
1157 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1158 if (!reply)
1159 goto nomem;
1161 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1162 return SCTP_DISPOSITION_CONSUME;
1164 nomem:
1165 return SCTP_DISPOSITION_NOMEM;
1169 * Process the returning HEARTBEAT ACK.
1171 * Section: 8.3 Path Heartbeat
1172 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1173 * should clear the error counter of the destination transport
1174 * address to which the HEARTBEAT was sent, and mark the destination
1175 * transport address as active if it is not so marked. The endpoint may
1176 * optionally report to the upper layer when an inactive destination
1177 * address is marked as active due to the reception of the latest
1178 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1179 * clear the association overall error count as well (as defined
1180 * in section 8.1).
1182 * The receiver of the HEARTBEAT ACK should also perform an RTT
1183 * measurement for that destination transport address using the time
1184 * value carried in the HEARTBEAT ACK chunk.
1186 * Verification Tag: 8.5 Verification Tag [Normal verification]
1188 * Inputs
1189 * (endpoint, asoc, chunk)
1191 * Outputs
1192 * (asoc, reply_msg, msg_up, timers, counters)
1194 * The return value is the disposition of the chunk.
1196 enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1197 const struct sctp_endpoint *ep,
1198 const struct sctp_association *asoc,
1199 const union sctp_subtype type,
1200 void *arg,
1201 struct sctp_cmd_seq *commands)
1203 struct sctp_sender_hb_info *hbinfo;
1204 struct sctp_chunk *chunk = arg;
1205 struct sctp_transport *link;
1206 unsigned long max_interval;
1207 union sctp_addr from_addr;
1209 if (!sctp_vtag_verify(chunk, asoc))
1210 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1212 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1213 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1214 sizeof(*hbinfo)))
1215 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1216 commands);
1218 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1219 /* Make sure that the length of the parameter is what we expect */
1220 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1221 return SCTP_DISPOSITION_DISCARD;
1223 from_addr = hbinfo->daddr;
1224 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1226 /* This should never happen, but lets log it if so. */
1227 if (unlikely(!link)) {
1228 if (from_addr.sa.sa_family == AF_INET6) {
1229 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1230 __func__,
1231 asoc,
1232 &from_addr.v6.sin6_addr);
1233 } else {
1234 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1235 __func__,
1236 asoc,
1237 &from_addr.v4.sin_addr.s_addr);
1239 return SCTP_DISPOSITION_DISCARD;
1242 /* Validate the 64-bit random nonce. */
1243 if (hbinfo->hb_nonce != link->hb_nonce)
1244 return SCTP_DISPOSITION_DISCARD;
1246 max_interval = link->hbinterval + link->rto;
1248 /* Check if the timestamp looks valid. */
1249 if (time_after(hbinfo->sent_at, jiffies) ||
1250 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1251 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1252 "for transport:%p\n", __func__, link);
1254 return SCTP_DISPOSITION_DISCARD;
1257 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1258 * the HEARTBEAT should clear the error counter of the
1259 * destination transport address to which the HEARTBEAT was
1260 * sent and mark the destination transport address as active if
1261 * it is not so marked.
1263 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1265 return SCTP_DISPOSITION_CONSUME;
1268 /* Helper function to send out an abort for the restart
1269 * condition.
1271 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1272 struct sctp_chunk *init,
1273 struct sctp_cmd_seq *commands)
1275 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1276 union sctp_addr_param *addrparm;
1277 struct sctp_errhdr *errhdr;
1278 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1279 struct sctp_endpoint *ep;
1280 struct sctp_packet *pkt;
1281 int len;
1283 /* Build the error on the stack. We are way to malloc crazy
1284 * throughout the code today.
1286 errhdr = (struct sctp_errhdr *)buffer;
1287 addrparm = (union sctp_addr_param *)errhdr->variable;
1289 /* Copy into a parm format. */
1290 len = af->to_addr_param(ssa, addrparm);
1291 len += sizeof(*errhdr);
1293 errhdr->cause = SCTP_ERROR_RESTART;
1294 errhdr->length = htons(len);
1296 /* Assign to the control socket. */
1297 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1299 /* Association is NULL since this may be a restart attack and we
1300 * want to send back the attacker's vtag.
1302 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1304 if (!pkt)
1305 goto out;
1306 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1308 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1310 /* Discard the rest of the inbound packet. */
1311 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1313 out:
1314 /* Even if there is no memory, treat as a failure so
1315 * the packet will get dropped.
1317 return 0;
1320 static bool list_has_sctp_addr(const struct list_head *list,
1321 union sctp_addr *ipaddr)
1323 struct sctp_transport *addr;
1325 list_for_each_entry(addr, list, transports) {
1326 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1327 return true;
1330 return false;
1332 /* A restart is occurring, check to make sure no new addresses
1333 * are being added as we may be under a takeover attack.
1335 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1336 const struct sctp_association *asoc,
1337 struct sctp_chunk *init,
1338 struct sctp_cmd_seq *commands)
1340 struct net *net = new_asoc->base.net;
1341 struct sctp_transport *new_addr;
1342 int ret = 1;
1344 /* Implementor's Guide - Section 5.2.2
1345 * ...
1346 * Before responding the endpoint MUST check to see if the
1347 * unexpected INIT adds new addresses to the association. If new
1348 * addresses are added to the association, the endpoint MUST respond
1349 * with an ABORT..
1352 /* Search through all current addresses and make sure
1353 * we aren't adding any new ones.
1355 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1356 transports) {
1357 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1358 &new_addr->ipaddr)) {
1359 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1360 commands);
1361 ret = 0;
1362 break;
1366 /* Return success if all addresses were found. */
1367 return ret;
1370 /* Populate the verification/tie tags based on overlapping INIT
1371 * scenario.
1373 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1375 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1376 const struct sctp_association *asoc)
1378 switch (asoc->state) {
1380 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1382 case SCTP_STATE_COOKIE_WAIT:
1383 new_asoc->c.my_vtag = asoc->c.my_vtag;
1384 new_asoc->c.my_ttag = asoc->c.my_vtag;
1385 new_asoc->c.peer_ttag = 0;
1386 break;
1388 case SCTP_STATE_COOKIE_ECHOED:
1389 new_asoc->c.my_vtag = asoc->c.my_vtag;
1390 new_asoc->c.my_ttag = asoc->c.my_vtag;
1391 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1392 break;
1394 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1395 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1397 default:
1398 new_asoc->c.my_ttag = asoc->c.my_vtag;
1399 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1400 break;
1403 /* Other parameters for the endpoint SHOULD be copied from the
1404 * existing parameters of the association (e.g. number of
1405 * outbound streams) into the INIT ACK and cookie.
1407 new_asoc->rwnd = asoc->rwnd;
1408 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1409 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1410 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1414 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1415 * handling action.
1417 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1419 * Returns value representing action to be taken. These action values
1420 * correspond to Action/Description values in RFC 2960, Table 2.
1422 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1423 const struct sctp_association *asoc)
1425 /* In this case, the peer may have restarted. */
1426 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1427 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1428 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1429 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1430 return 'A';
1432 /* Collision case B. */
1433 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1434 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1435 (0 == asoc->c.peer_vtag))) {
1436 return 'B';
1439 /* Collision case D. */
1440 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1441 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1442 return 'D';
1444 /* Collision case C. */
1445 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1446 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1447 (0 == new_asoc->c.my_ttag) &&
1448 (0 == new_asoc->c.peer_ttag))
1449 return 'C';
1451 /* No match to any of the special cases; discard this packet. */
1452 return 'E';
1455 /* Common helper routine for both duplicate and simulataneous INIT
1456 * chunk handling.
1458 static enum sctp_disposition sctp_sf_do_unexpected_init(
1459 struct net *net,
1460 const struct sctp_endpoint *ep,
1461 const struct sctp_association *asoc,
1462 const union sctp_subtype type,
1463 void *arg,
1464 struct sctp_cmd_seq *commands)
1466 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1467 struct sctp_unrecognized_param *unk_param;
1468 struct sctp_association *new_asoc;
1469 enum sctp_disposition retval;
1470 struct sctp_packet *packet;
1471 int len;
1473 /* Update socket peer label if first association. */
1474 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
1475 chunk->skb))
1476 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1478 /* 6.10 Bundling
1479 * An endpoint MUST NOT bundle INIT, INIT ACK or
1480 * SHUTDOWN COMPLETE with any other chunks.
1482 * IG Section 2.11.2
1483 * Furthermore, we require that the receiver of an INIT chunk MUST
1484 * enforce these rules by silently discarding an arriving packet
1485 * with an INIT chunk that is bundled with other chunks.
1487 if (!chunk->singleton)
1488 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1490 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1491 * Tag.
1493 if (chunk->sctp_hdr->vtag != 0)
1494 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1496 /* Make sure that the INIT chunk has a valid length.
1497 * In this case, we generate a protocol violation since we have
1498 * an association established.
1500 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1501 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1502 commands);
1504 if (SCTP_INPUT_CB(chunk->skb)->encap_port != chunk->transport->encap_port)
1505 return sctp_sf_new_encap_port(net, ep, asoc, type, arg, commands);
1507 /* Grab the INIT header. */
1508 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1510 /* Tag the variable length parameters. */
1511 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1513 /* Verify the INIT chunk before processing it. */
1514 err_chunk = NULL;
1515 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1516 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1517 &err_chunk)) {
1518 /* This chunk contains fatal error. It is to be discarded.
1519 * Send an ABORT, with causes if there is any.
1521 if (err_chunk) {
1522 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1523 (__u8 *)(err_chunk->chunk_hdr) +
1524 sizeof(struct sctp_chunkhdr),
1525 ntohs(err_chunk->chunk_hdr->length) -
1526 sizeof(struct sctp_chunkhdr));
1528 if (packet) {
1529 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1530 SCTP_PACKET(packet));
1531 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1532 retval = SCTP_DISPOSITION_CONSUME;
1533 } else {
1534 retval = SCTP_DISPOSITION_NOMEM;
1536 goto cleanup;
1537 } else {
1538 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1539 commands);
1544 * Other parameters for the endpoint SHOULD be copied from the
1545 * existing parameters of the association (e.g. number of
1546 * outbound streams) into the INIT ACK and cookie.
1547 * FIXME: We are copying parameters from the endpoint not the
1548 * association.
1550 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1551 if (!new_asoc)
1552 goto nomem;
1554 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1555 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1556 goto nomem;
1558 /* In the outbound INIT ACK the endpoint MUST copy its current
1559 * Verification Tag and Peers Verification tag into a reserved
1560 * place (local tie-tag and per tie-tag) within the state cookie.
1562 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1563 (struct sctp_init_chunk *)chunk->chunk_hdr,
1564 GFP_ATOMIC))
1565 goto nomem;
1567 /* Make sure no new addresses are being added during the
1568 * restart. Do not do this check for COOKIE-WAIT state,
1569 * since there are no peer addresses to check against.
1570 * Upon return an ABORT will have been sent if needed.
1572 if (!sctp_state(asoc, COOKIE_WAIT)) {
1573 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1574 commands)) {
1575 retval = SCTP_DISPOSITION_CONSUME;
1576 goto nomem_retval;
1580 sctp_tietags_populate(new_asoc, asoc);
1582 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1584 /* If there are errors need to be reported for unknown parameters,
1585 * make sure to reserve enough room in the INIT ACK for them.
1587 len = 0;
1588 if (err_chunk) {
1589 len = ntohs(err_chunk->chunk_hdr->length) -
1590 sizeof(struct sctp_chunkhdr);
1593 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1594 if (!repl)
1595 goto nomem;
1597 /* If there are errors need to be reported for unknown parameters,
1598 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1599 * parameter.
1601 if (err_chunk) {
1602 /* Get the "Unrecognized parameter" parameter(s) out of the
1603 * ERROR chunk generated by sctp_verify_init(). Since the
1604 * error cause code for "unknown parameter" and the
1605 * "Unrecognized parameter" type is the same, we can
1606 * construct the parameters in INIT ACK by copying the
1607 * ERROR causes over.
1609 unk_param = (struct sctp_unrecognized_param *)
1610 ((__u8 *)(err_chunk->chunk_hdr) +
1611 sizeof(struct sctp_chunkhdr));
1612 /* Replace the cause code with the "Unrecognized parameter"
1613 * parameter type.
1615 sctp_addto_chunk(repl, len, unk_param);
1618 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1619 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1622 * Note: After sending out INIT ACK with the State Cookie parameter,
1623 * "Z" MUST NOT allocate any resources for this new association.
1624 * Otherwise, "Z" will be vulnerable to resource attacks.
1626 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1627 retval = SCTP_DISPOSITION_CONSUME;
1629 return retval;
1631 nomem:
1632 retval = SCTP_DISPOSITION_NOMEM;
1633 nomem_retval:
1634 if (new_asoc)
1635 sctp_association_free(new_asoc);
1636 cleanup:
1637 if (err_chunk)
1638 sctp_chunk_free(err_chunk);
1639 return retval;
1643 * Handle simultaneous INIT.
1644 * This means we started an INIT and then we got an INIT request from
1645 * our peer.
1647 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1648 * This usually indicates an initialization collision, i.e., each
1649 * endpoint is attempting, at about the same time, to establish an
1650 * association with the other endpoint.
1652 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1653 * endpoint MUST respond with an INIT ACK using the same parameters it
1654 * sent in its original INIT chunk (including its Verification Tag,
1655 * unchanged). These original parameters are combined with those from the
1656 * newly received INIT chunk. The endpoint shall also generate a State
1657 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1658 * INIT to calculate the State Cookie.
1660 * After that, the endpoint MUST NOT change its state, the T1-init
1661 * timer shall be left running and the corresponding TCB MUST NOT be
1662 * destroyed. The normal procedures for handling State Cookies when
1663 * a TCB exists will resolve the duplicate INITs to a single association.
1665 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1666 * its Tie-Tags with the Tag information of itself and its peer (see
1667 * section 5.2.2 for a description of the Tie-Tags).
1669 * Verification Tag: Not explicit, but an INIT can not have a valid
1670 * verification tag, so we skip the check.
1672 * Inputs
1673 * (endpoint, asoc, chunk)
1675 * Outputs
1676 * (asoc, reply_msg, msg_up, timers, counters)
1678 * The return value is the disposition of the chunk.
1680 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1681 struct net *net,
1682 const struct sctp_endpoint *ep,
1683 const struct sctp_association *asoc,
1684 const union sctp_subtype type,
1685 void *arg,
1686 struct sctp_cmd_seq *commands)
1688 /* Call helper to do the real work for both simulataneous and
1689 * duplicate INIT chunk handling.
1691 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1695 * Handle duplicated INIT messages. These are usually delayed
1696 * restransmissions.
1698 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1699 * COOKIE-ECHOED and COOKIE-WAIT
1701 * Unless otherwise stated, upon reception of an unexpected INIT for
1702 * this association, the endpoint shall generate an INIT ACK with a
1703 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1704 * current Verification Tag and peer's Verification Tag into a reserved
1705 * place within the state cookie. We shall refer to these locations as
1706 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1707 * containing this INIT ACK MUST carry a Verification Tag value equal to
1708 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1709 * MUST contain a new Initiation Tag (randomly generated see Section
1710 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1711 * existing parameters of the association (e.g. number of outbound
1712 * streams) into the INIT ACK and cookie.
1714 * After sending out the INIT ACK, the endpoint shall take no further
1715 * actions, i.e., the existing association, including its current state,
1716 * and the corresponding TCB MUST NOT be changed.
1718 * Note: Only when a TCB exists and the association is not in a COOKIE-
1719 * WAIT state are the Tie-Tags populated. For a normal association INIT
1720 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1721 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1722 * State Cookie are populated as specified in section 5.2.1.
1724 * Verification Tag: Not specified, but an INIT has no way of knowing
1725 * what the verification tag could be, so we ignore it.
1727 * Inputs
1728 * (endpoint, asoc, chunk)
1730 * Outputs
1731 * (asoc, reply_msg, msg_up, timers, counters)
1733 * The return value is the disposition of the chunk.
1735 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1736 struct net *net,
1737 const struct sctp_endpoint *ep,
1738 const struct sctp_association *asoc,
1739 const union sctp_subtype type,
1740 void *arg,
1741 struct sctp_cmd_seq *commands)
1743 /* Call helper to do the real work for both simulataneous and
1744 * duplicate INIT chunk handling.
1746 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1751 * Unexpected INIT-ACK handler.
1753 * Section 5.2.3
1754 * If an INIT ACK received by an endpoint in any state other than the
1755 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1756 * An unexpected INIT ACK usually indicates the processing of an old or
1757 * duplicated INIT chunk.
1759 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1760 struct net *net,
1761 const struct sctp_endpoint *ep,
1762 const struct sctp_association *asoc,
1763 const union sctp_subtype type,
1764 void *arg,
1765 struct sctp_cmd_seq *commands)
1767 /* Per the above section, we'll discard the chunk if we have an
1768 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1770 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1771 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1772 else
1773 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1776 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1778 * Section 5.2.4
1779 * A) In this case, the peer may have restarted.
1781 static enum sctp_disposition sctp_sf_do_dupcook_a(
1782 struct net *net,
1783 const struct sctp_endpoint *ep,
1784 const struct sctp_association *asoc,
1785 struct sctp_chunk *chunk,
1786 struct sctp_cmd_seq *commands,
1787 struct sctp_association *new_asoc)
1789 struct sctp_init_chunk *peer_init;
1790 enum sctp_disposition disposition;
1791 struct sctp_ulpevent *ev;
1792 struct sctp_chunk *repl;
1793 struct sctp_chunk *err;
1795 /* new_asoc is a brand-new association, so these are not yet
1796 * side effects--it is safe to run them here.
1798 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1800 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1801 GFP_ATOMIC))
1802 goto nomem;
1804 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1805 goto nomem;
1807 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1808 return SCTP_DISPOSITION_DISCARD;
1810 /* Make sure no new addresses are being added during the
1811 * restart. Though this is a pretty complicated attack
1812 * since you'd have to get inside the cookie.
1814 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1815 return SCTP_DISPOSITION_CONSUME;
1817 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1818 * the peer has restarted (Action A), it MUST NOT setup a new
1819 * association but instead resend the SHUTDOWN ACK and send an ERROR
1820 * chunk with a "Cookie Received while Shutting Down" error cause to
1821 * its peer.
1823 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1824 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1825 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1826 chunk, commands);
1827 if (SCTP_DISPOSITION_NOMEM == disposition)
1828 goto nomem;
1830 err = sctp_make_op_error(asoc, chunk,
1831 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1832 NULL, 0, 0);
1833 if (err)
1834 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1835 SCTP_CHUNK(err));
1837 return SCTP_DISPOSITION_CONSUME;
1840 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1841 * data. Consider the optional choice of resending of this data.
1843 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1844 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1845 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1846 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1848 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1849 * and ASCONF-ACK cache.
1851 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1852 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1853 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1855 repl = sctp_make_cookie_ack(new_asoc, chunk);
1856 if (!repl)
1857 goto nomem;
1859 /* Report association restart to upper layer. */
1860 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1861 new_asoc->c.sinit_num_ostreams,
1862 new_asoc->c.sinit_max_instreams,
1863 NULL, GFP_ATOMIC);
1864 if (!ev)
1865 goto nomem_ev;
1867 /* Update the content of current association. */
1868 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1869 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1870 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1871 sctp_state(asoc, SHUTDOWN_SENT)) &&
1872 (sctp_sstate(asoc->base.sk, CLOSING) ||
1873 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1874 /* If the socket has been closed by user, don't
1875 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1876 * bundled with COOKIE_ACK.
1878 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1879 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1880 SCTP_ST_CHUNK(0), repl,
1881 commands);
1882 } else {
1883 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1884 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1885 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1887 return SCTP_DISPOSITION_CONSUME;
1889 nomem_ev:
1890 sctp_chunk_free(repl);
1891 nomem:
1892 return SCTP_DISPOSITION_NOMEM;
1895 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1897 * Section 5.2.4
1898 * B) In this case, both sides may be attempting to start an association
1899 * at about the same time but the peer endpoint started its INIT
1900 * after responding to the local endpoint's INIT
1902 /* This case represents an initialization collision. */
1903 static enum sctp_disposition sctp_sf_do_dupcook_b(
1904 struct net *net,
1905 const struct sctp_endpoint *ep,
1906 const struct sctp_association *asoc,
1907 struct sctp_chunk *chunk,
1908 struct sctp_cmd_seq *commands,
1909 struct sctp_association *new_asoc)
1911 struct sctp_init_chunk *peer_init;
1912 struct sctp_chunk *repl;
1914 /* new_asoc is a brand-new association, so these are not yet
1915 * side effects--it is safe to run them here.
1917 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1918 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1919 GFP_ATOMIC))
1920 goto nomem;
1922 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1923 goto nomem;
1925 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1926 return SCTP_DISPOSITION_DISCARD;
1928 /* Update the content of current association. */
1929 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1930 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1931 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1932 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1933 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1935 repl = sctp_make_cookie_ack(new_asoc, chunk);
1936 if (!repl)
1937 goto nomem;
1939 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1941 /* RFC 2960 5.1 Normal Establishment of an Association
1943 * D) IMPLEMENTATION NOTE: An implementation may choose to
1944 * send the Communication Up notification to the SCTP user
1945 * upon reception of a valid COOKIE ECHO chunk.
1947 * Sadly, this needs to be implemented as a side-effect, because
1948 * we are not guaranteed to have set the association id of the real
1949 * association and so these notifications need to be delayed until
1950 * the association id is allocated.
1953 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1955 /* Sockets API Draft Section 5.3.1.6
1956 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1957 * delivers this notification to inform the application that of the
1958 * peers requested adaptation layer.
1960 * This also needs to be done as a side effect for the same reason as
1961 * above.
1963 if (asoc->peer.adaptation_ind)
1964 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1966 if (!asoc->peer.auth_capable)
1967 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1969 return SCTP_DISPOSITION_CONSUME;
1971 nomem:
1972 return SCTP_DISPOSITION_NOMEM;
1975 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1977 * Section 5.2.4
1978 * C) In this case, the local endpoint's cookie has arrived late.
1979 * Before it arrived, the local endpoint sent an INIT and received an
1980 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1981 * but a new tag of its own.
1983 /* This case represents an initialization collision. */
1984 static enum sctp_disposition sctp_sf_do_dupcook_c(
1985 struct net *net,
1986 const struct sctp_endpoint *ep,
1987 const struct sctp_association *asoc,
1988 struct sctp_chunk *chunk,
1989 struct sctp_cmd_seq *commands,
1990 struct sctp_association *new_asoc)
1992 /* The cookie should be silently discarded.
1993 * The endpoint SHOULD NOT change states and should leave
1994 * any timers running.
1996 return SCTP_DISPOSITION_DISCARD;
1999 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
2001 * Section 5.2.4
2003 * D) When both local and remote tags match the endpoint should always
2004 * enter the ESTABLISHED state, if it has not already done so.
2006 /* This case represents an initialization collision. */
2007 static enum sctp_disposition sctp_sf_do_dupcook_d(
2008 struct net *net,
2009 const struct sctp_endpoint *ep,
2010 const struct sctp_association *asoc,
2011 struct sctp_chunk *chunk,
2012 struct sctp_cmd_seq *commands,
2013 struct sctp_association *new_asoc)
2015 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
2016 struct sctp_chunk *repl;
2018 /* Clarification from Implementor's Guide:
2019 * D) When both local and remote tags match the endpoint should
2020 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2021 * It should stop any cookie timer that may be running and send
2022 * a COOKIE ACK.
2025 if (!sctp_auth_chunk_verify(net, chunk, asoc))
2026 return SCTP_DISPOSITION_DISCARD;
2028 /* Don't accidentally move back into established state. */
2029 if (asoc->state < SCTP_STATE_ESTABLISHED) {
2030 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2031 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2032 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2033 SCTP_STATE(SCTP_STATE_ESTABLISHED));
2034 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2035 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2036 SCTP_NULL());
2038 /* RFC 2960 5.1 Normal Establishment of an Association
2040 * D) IMPLEMENTATION NOTE: An implementation may choose
2041 * to send the Communication Up notification to the
2042 * SCTP user upon reception of a valid COOKIE
2043 * ECHO chunk.
2045 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2046 SCTP_COMM_UP, 0,
2047 asoc->c.sinit_num_ostreams,
2048 asoc->c.sinit_max_instreams,
2049 NULL, GFP_ATOMIC);
2050 if (!ev)
2051 goto nomem;
2053 /* Sockets API Draft Section 5.3.1.6
2054 * When a peer sends a Adaptation Layer Indication parameter,
2055 * SCTP delivers this notification to inform the application
2056 * that of the peers requested adaptation layer.
2058 if (asoc->peer.adaptation_ind) {
2059 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2060 GFP_ATOMIC);
2061 if (!ai_ev)
2062 goto nomem;
2066 if (!asoc->peer.auth_capable) {
2067 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2068 SCTP_AUTH_NO_AUTH,
2069 GFP_ATOMIC);
2070 if (!auth_ev)
2071 goto nomem;
2075 repl = sctp_make_cookie_ack(asoc, chunk);
2076 if (!repl)
2077 goto nomem;
2079 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2081 if (ev)
2082 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2083 SCTP_ULPEVENT(ev));
2084 if (ai_ev)
2085 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2086 SCTP_ULPEVENT(ai_ev));
2087 if (auth_ev)
2088 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2089 SCTP_ULPEVENT(auth_ev));
2091 return SCTP_DISPOSITION_CONSUME;
2093 nomem:
2094 if (auth_ev)
2095 sctp_ulpevent_free(auth_ev);
2096 if (ai_ev)
2097 sctp_ulpevent_free(ai_ev);
2098 if (ev)
2099 sctp_ulpevent_free(ev);
2100 return SCTP_DISPOSITION_NOMEM;
2104 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2105 * chunk was retransmitted and then delayed in the network.
2107 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2109 * Verification Tag: None. Do cookie validation.
2111 * Inputs
2112 * (endpoint, asoc, chunk)
2114 * Outputs
2115 * (asoc, reply_msg, msg_up, timers, counters)
2117 * The return value is the disposition of the chunk.
2119 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2120 struct net *net,
2121 const struct sctp_endpoint *ep,
2122 const struct sctp_association *asoc,
2123 const union sctp_subtype type,
2124 void *arg,
2125 struct sctp_cmd_seq *commands)
2127 struct sctp_association *new_asoc;
2128 struct sctp_chunk *chunk = arg;
2129 enum sctp_disposition retval;
2130 struct sctp_chunk *err_chk_p;
2131 int error = 0;
2132 char action;
2134 /* Make sure that the chunk has a valid length from the protocol
2135 * perspective. In this case check to make sure we have at least
2136 * enough for the chunk header. Cookie length verification is
2137 * done later.
2139 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2140 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2141 commands);
2143 /* "Decode" the chunk. We have no optional parameters so we
2144 * are in good shape.
2146 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2147 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2148 sizeof(struct sctp_chunkhdr)))
2149 goto nomem;
2151 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2152 * of a duplicate COOKIE ECHO match the Verification Tags of the
2153 * current association, consider the State Cookie valid even if
2154 * the lifespan is exceeded.
2156 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2157 &err_chk_p);
2159 /* FIXME:
2160 * If the re-build failed, what is the proper error path
2161 * from here?
2163 * [We should abort the association. --piggy]
2165 if (!new_asoc) {
2166 /* FIXME: Several errors are possible. A bad cookie should
2167 * be silently discarded, but think about logging it too.
2169 switch (error) {
2170 case -SCTP_IERROR_NOMEM:
2171 goto nomem;
2173 case -SCTP_IERROR_STALE_COOKIE:
2174 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2175 err_chk_p);
2176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2177 case -SCTP_IERROR_BAD_SIG:
2178 default:
2179 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2183 /* Update socket peer label if first association. */
2184 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
2185 chunk->skb)) {
2186 sctp_association_free(new_asoc);
2187 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2190 /* Set temp so that it won't be added into hashtable */
2191 new_asoc->temp = 1;
2193 /* Compare the tie_tag in cookie with the verification tag of
2194 * current association.
2196 action = sctp_tietags_compare(new_asoc, asoc);
2198 switch (action) {
2199 case 'A': /* Association restart. */
2200 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2201 new_asoc);
2202 break;
2204 case 'B': /* Collision case B. */
2205 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2206 new_asoc);
2207 break;
2209 case 'C': /* Collision case C. */
2210 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2211 new_asoc);
2212 break;
2214 case 'D': /* Collision case D. */
2215 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2216 new_asoc);
2217 break;
2219 default: /* Discard packet for all others. */
2220 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2221 break;
2224 /* Delete the tempory new association. */
2225 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2226 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2228 /* Restore association pointer to provide SCTP command interpeter
2229 * with a valid context in case it needs to manipulate
2230 * the queues */
2231 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2232 SCTP_ASOC((struct sctp_association *)asoc));
2234 return retval;
2236 nomem:
2237 return SCTP_DISPOSITION_NOMEM;
2241 * Process an ABORT. (SHUTDOWN-PENDING state)
2243 * See sctp_sf_do_9_1_abort().
2245 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2246 struct net *net,
2247 const struct sctp_endpoint *ep,
2248 const struct sctp_association *asoc,
2249 const union sctp_subtype type,
2250 void *arg,
2251 struct sctp_cmd_seq *commands)
2253 struct sctp_chunk *chunk = arg;
2255 if (!sctp_vtag_verify_either(chunk, asoc))
2256 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2258 /* Make sure that the ABORT chunk has a valid length.
2259 * Since this is an ABORT chunk, we have to discard it
2260 * because of the following text:
2261 * RFC 2960, Section 3.3.7
2262 * If an endpoint receives an ABORT with a format error or for an
2263 * association that doesn't exist, it MUST silently discard it.
2264 * Because the length is "invalid", we can't really discard just
2265 * as we do not know its true length. So, to be safe, discard the
2266 * packet.
2268 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2269 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2271 /* ADD-IP: Special case for ABORT chunks
2272 * F4) One special consideration is that ABORT Chunks arriving
2273 * destined to the IP address being deleted MUST be
2274 * ignored (see Section 5.3.1 for further details).
2276 if (SCTP_ADDR_DEL ==
2277 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2278 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2280 if (!sctp_err_chunk_valid(chunk))
2281 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2283 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2287 * Process an ABORT. (SHUTDOWN-SENT state)
2289 * See sctp_sf_do_9_1_abort().
2291 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2292 struct net *net,
2293 const struct sctp_endpoint *ep,
2294 const struct sctp_association *asoc,
2295 const union sctp_subtype type,
2296 void *arg,
2297 struct sctp_cmd_seq *commands)
2299 struct sctp_chunk *chunk = arg;
2301 if (!sctp_vtag_verify_either(chunk, asoc))
2302 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2304 /* Make sure that the ABORT chunk has a valid length.
2305 * Since this is an ABORT chunk, we have to discard it
2306 * because of the following text:
2307 * RFC 2960, Section 3.3.7
2308 * If an endpoint receives an ABORT with a format error or for an
2309 * association that doesn't exist, it MUST silently discard it.
2310 * Because the length is "invalid", we can't really discard just
2311 * as we do not know its true length. So, to be safe, discard the
2312 * packet.
2314 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2315 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2317 /* ADD-IP: Special case for ABORT chunks
2318 * F4) One special consideration is that ABORT Chunks arriving
2319 * destined to the IP address being deleted MUST be
2320 * ignored (see Section 5.3.1 for further details).
2322 if (SCTP_ADDR_DEL ==
2323 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2324 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2326 if (!sctp_err_chunk_valid(chunk))
2327 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2329 /* Stop the T2-shutdown timer. */
2330 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2331 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2333 /* Stop the T5-shutdown guard timer. */
2334 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2335 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2337 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2341 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2343 * See sctp_sf_do_9_1_abort().
2345 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2346 struct net *net,
2347 const struct sctp_endpoint *ep,
2348 const struct sctp_association *asoc,
2349 const union sctp_subtype type,
2350 void *arg,
2351 struct sctp_cmd_seq *commands)
2353 /* The same T2 timer, so we should be able to use
2354 * common function with the SHUTDOWN-SENT state.
2356 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2360 * Handle an Error received in COOKIE_ECHOED state.
2362 * Only handle the error type of stale COOKIE Error, the other errors will
2363 * be ignored.
2365 * Inputs
2366 * (endpoint, asoc, chunk)
2368 * Outputs
2369 * (asoc, reply_msg, msg_up, timers, counters)
2371 * The return value is the disposition of the chunk.
2373 enum sctp_disposition sctp_sf_cookie_echoed_err(
2374 struct net *net,
2375 const struct sctp_endpoint *ep,
2376 const struct sctp_association *asoc,
2377 const union sctp_subtype type,
2378 void *arg,
2379 struct sctp_cmd_seq *commands)
2381 struct sctp_chunk *chunk = arg;
2382 struct sctp_errhdr *err;
2384 if (!sctp_vtag_verify(chunk, asoc))
2385 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2387 /* Make sure that the ERROR chunk has a valid length.
2388 * The parameter walking depends on this as well.
2390 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2391 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2392 commands);
2394 /* Process the error here */
2395 /* FUTURE FIXME: When PR-SCTP related and other optional
2396 * parms are emitted, this will have to change to handle multiple
2397 * errors.
2399 sctp_walk_errors(err, chunk->chunk_hdr) {
2400 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2401 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2402 arg, commands);
2405 /* It is possible to have malformed error causes, and that
2406 * will cause us to end the walk early. However, since
2407 * we are discarding the packet, there should be no adverse
2408 * affects.
2410 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2414 * Handle a Stale COOKIE Error
2416 * Section: 5.2.6 Handle Stale COOKIE Error
2417 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2418 * one of the following three alternatives.
2419 * ...
2420 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2421 * Preservative parameter requesting an extension to the lifetime of
2422 * the State Cookie. When calculating the time extension, an
2423 * implementation SHOULD use the RTT information measured based on the
2424 * previous COOKIE ECHO / ERROR exchange, and should add no more
2425 * than 1 second beyond the measured RTT, due to long State Cookie
2426 * lifetimes making the endpoint more subject to a replay attack.
2428 * Verification Tag: Not explicit, but safe to ignore.
2430 * Inputs
2431 * (endpoint, asoc, chunk)
2433 * Outputs
2434 * (asoc, reply_msg, msg_up, timers, counters)
2436 * The return value is the disposition of the chunk.
2438 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2439 struct net *net,
2440 const struct sctp_endpoint *ep,
2441 const struct sctp_association *asoc,
2442 const union sctp_subtype type,
2443 void *arg,
2444 struct sctp_cmd_seq *commands)
2446 int attempts = asoc->init_err_counter + 1;
2447 struct sctp_chunk *chunk = arg, *reply;
2448 struct sctp_cookie_preserve_param bht;
2449 struct sctp_bind_addr *bp;
2450 struct sctp_errhdr *err;
2451 u32 stale;
2453 if (attempts > asoc->max_init_attempts) {
2454 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2455 SCTP_ERROR(ETIMEDOUT));
2456 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2457 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2458 return SCTP_DISPOSITION_DELETE_TCB;
2461 err = (struct sctp_errhdr *)(chunk->skb->data);
2463 /* When calculating the time extension, an implementation
2464 * SHOULD use the RTT information measured based on the
2465 * previous COOKIE ECHO / ERROR exchange, and should add no
2466 * more than 1 second beyond the measured RTT, due to long
2467 * State Cookie lifetimes making the endpoint more subject to
2468 * a replay attack.
2469 * Measure of Staleness's unit is usec. (1/1000000 sec)
2470 * Suggested Cookie Life-span Increment's unit is msec.
2471 * (1/1000 sec)
2472 * In general, if you use the suggested cookie life, the value
2473 * found in the field of measure of staleness should be doubled
2474 * to give ample time to retransmit the new cookie and thus
2475 * yield a higher probability of success on the reattempt.
2477 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2478 stale = (stale * 2) / 1000;
2480 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2481 bht.param_hdr.length = htons(sizeof(bht));
2482 bht.lifespan_increment = htonl(stale);
2484 /* Build that new INIT chunk. */
2485 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2486 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2487 if (!reply)
2488 goto nomem;
2490 sctp_addto_chunk(reply, sizeof(bht), &bht);
2492 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2493 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2495 /* Stop pending T3-rtx and heartbeat timers */
2496 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2497 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2499 /* Delete non-primary peer ip addresses since we are transitioning
2500 * back to the COOKIE-WAIT state
2502 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2504 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2505 * resend
2507 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2508 SCTP_TRANSPORT(asoc->peer.primary_path));
2510 /* Cast away the const modifier, as we want to just
2511 * rerun it through as a sideffect.
2513 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2515 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2516 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2517 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2518 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2519 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2520 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2522 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2524 return SCTP_DISPOSITION_CONSUME;
2526 nomem:
2527 return SCTP_DISPOSITION_NOMEM;
2531 * Process an ABORT.
2533 * Section: 9.1
2534 * After checking the Verification Tag, the receiving endpoint shall
2535 * remove the association from its record, and shall report the
2536 * termination to its upper layer.
2538 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2539 * B) Rules for packet carrying ABORT:
2541 * - The endpoint shall always fill in the Verification Tag field of the
2542 * outbound packet with the destination endpoint's tag value if it
2543 * is known.
2545 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2546 * MUST follow the procedure described in Section 8.4.
2548 * - The receiver MUST accept the packet if the Verification Tag
2549 * matches either its own tag, OR the tag of its peer. Otherwise, the
2550 * receiver MUST silently discard the packet and take no further
2551 * action.
2553 * Inputs
2554 * (endpoint, asoc, chunk)
2556 * Outputs
2557 * (asoc, reply_msg, msg_up, timers, counters)
2559 * The return value is the disposition of the chunk.
2561 enum sctp_disposition sctp_sf_do_9_1_abort(
2562 struct net *net,
2563 const struct sctp_endpoint *ep,
2564 const struct sctp_association *asoc,
2565 const union sctp_subtype type,
2566 void *arg,
2567 struct sctp_cmd_seq *commands)
2569 struct sctp_chunk *chunk = arg;
2571 if (!sctp_vtag_verify_either(chunk, asoc))
2572 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2574 /* Make sure that the ABORT chunk has a valid length.
2575 * Since this is an ABORT chunk, we have to discard it
2576 * because of the following text:
2577 * RFC 2960, Section 3.3.7
2578 * If an endpoint receives an ABORT with a format error or for an
2579 * association that doesn't exist, it MUST silently discard it.
2580 * Because the length is "invalid", we can't really discard just
2581 * as we do not know its true length. So, to be safe, discard the
2582 * packet.
2584 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2585 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2587 /* ADD-IP: Special case for ABORT chunks
2588 * F4) One special consideration is that ABORT Chunks arriving
2589 * destined to the IP address being deleted MUST be
2590 * ignored (see Section 5.3.1 for further details).
2592 if (SCTP_ADDR_DEL ==
2593 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2594 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2596 if (!sctp_err_chunk_valid(chunk))
2597 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2599 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2602 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2603 struct net *net,
2604 const struct sctp_endpoint *ep,
2605 const struct sctp_association *asoc,
2606 const union sctp_subtype type,
2607 void *arg,
2608 struct sctp_cmd_seq *commands)
2610 __be16 error = SCTP_ERROR_NO_ERROR;
2611 struct sctp_chunk *chunk = arg;
2612 unsigned int len;
2614 /* See if we have an error cause code in the chunk. */
2615 len = ntohs(chunk->chunk_hdr->length);
2616 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2617 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2619 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2620 /* ASSOC_FAILED will DELETE_TCB. */
2621 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2622 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2623 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2625 return SCTP_DISPOSITION_ABORT;
2629 * Process an ABORT. (COOKIE-WAIT state)
2631 * See sctp_sf_do_9_1_abort() above.
2633 enum sctp_disposition sctp_sf_cookie_wait_abort(
2634 struct net *net,
2635 const struct sctp_endpoint *ep,
2636 const struct sctp_association *asoc,
2637 const union sctp_subtype type,
2638 void *arg,
2639 struct sctp_cmd_seq *commands)
2641 __be16 error = SCTP_ERROR_NO_ERROR;
2642 struct sctp_chunk *chunk = arg;
2643 unsigned int len;
2645 if (!sctp_vtag_verify_either(chunk, asoc))
2646 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2648 /* Make sure that the ABORT chunk has a valid length.
2649 * Since this is an ABORT chunk, we have to discard it
2650 * because of the following text:
2651 * RFC 2960, Section 3.3.7
2652 * If an endpoint receives an ABORT with a format error or for an
2653 * association that doesn't exist, it MUST silently discard it.
2654 * Because the length is "invalid", we can't really discard just
2655 * as we do not know its true length. So, to be safe, discard the
2656 * packet.
2658 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2659 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2661 /* See if we have an error cause code in the chunk. */
2662 len = ntohs(chunk->chunk_hdr->length);
2663 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2664 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2666 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2667 chunk->transport);
2671 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2673 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2674 struct net *net,
2675 const struct sctp_endpoint *ep,
2676 const struct sctp_association *asoc,
2677 const union sctp_subtype type,
2678 void *arg,
2679 struct sctp_cmd_seq *commands)
2681 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2682 ENOPROTOOPT, asoc,
2683 (struct sctp_transport *)arg);
2687 * Process an ABORT. (COOKIE-ECHOED state)
2689 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2690 struct net *net,
2691 const struct sctp_endpoint *ep,
2692 const struct sctp_association *asoc,
2693 const union sctp_subtype type,
2694 void *arg,
2695 struct sctp_cmd_seq *commands)
2697 /* There is a single T1 timer, so we should be able to use
2698 * common function with the COOKIE-WAIT state.
2700 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2704 * Stop T1 timer and abort association with "INIT failed".
2706 * This is common code called by several sctp_sf_*_abort() functions above.
2708 static enum sctp_disposition sctp_stop_t1_and_abort(
2709 struct net *net,
2710 struct sctp_cmd_seq *commands,
2711 __be16 error, int sk_err,
2712 const struct sctp_association *asoc,
2713 struct sctp_transport *transport)
2715 pr_debug("%s: ABORT received (INIT)\n", __func__);
2717 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2718 SCTP_STATE(SCTP_STATE_CLOSED));
2719 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2720 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2721 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2722 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2723 /* CMD_INIT_FAILED will DELETE_TCB. */
2724 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2725 SCTP_PERR(error));
2727 return SCTP_DISPOSITION_ABORT;
2731 * sctp_sf_do_9_2_shut
2733 * Section: 9.2
2734 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2735 * - enter the SHUTDOWN-RECEIVED state,
2737 * - stop accepting new data from its SCTP user
2739 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2740 * that all its outstanding DATA chunks have been received by the
2741 * SHUTDOWN sender.
2743 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2744 * send a SHUTDOWN in response to a ULP request. And should discard
2745 * subsequent SHUTDOWN chunks.
2747 * If there are still outstanding DATA chunks left, the SHUTDOWN
2748 * receiver shall continue to follow normal data transmission
2749 * procedures defined in Section 6 until all outstanding DATA chunks
2750 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2751 * new data from its SCTP user.
2753 * Verification Tag: 8.5 Verification Tag [Normal verification]
2755 * Inputs
2756 * (endpoint, asoc, chunk)
2758 * Outputs
2759 * (asoc, reply_msg, msg_up, timers, counters)
2761 * The return value is the disposition of the chunk.
2763 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2764 struct net *net,
2765 const struct sctp_endpoint *ep,
2766 const struct sctp_association *asoc,
2767 const union sctp_subtype type,
2768 void *arg,
2769 struct sctp_cmd_seq *commands)
2771 enum sctp_disposition disposition;
2772 struct sctp_chunk *chunk = arg;
2773 struct sctp_shutdownhdr *sdh;
2774 struct sctp_ulpevent *ev;
2775 __u32 ctsn;
2777 if (!sctp_vtag_verify(chunk, asoc))
2778 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2780 /* Make sure that the SHUTDOWN chunk has a valid length. */
2781 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2782 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2783 commands);
2785 /* Convert the elaborate header. */
2786 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2787 skb_pull(chunk->skb, sizeof(*sdh));
2788 chunk->subh.shutdown_hdr = sdh;
2789 ctsn = ntohl(sdh->cum_tsn_ack);
2791 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2792 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2793 asoc->ctsn_ack_point);
2795 return SCTP_DISPOSITION_DISCARD;
2798 /* If Cumulative TSN Ack beyond the max tsn currently
2799 * send, terminating the association and respond to the
2800 * sender with an ABORT.
2802 if (!TSN_lt(ctsn, asoc->next_tsn))
2803 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2805 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2806 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2807 * inform the application that it should cease sending data.
2809 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2810 if (!ev) {
2811 disposition = SCTP_DISPOSITION_NOMEM;
2812 goto out;
2814 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2816 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2817 * - enter the SHUTDOWN-RECEIVED state,
2818 * - stop accepting new data from its SCTP user
2820 * [This is implicit in the new state.]
2822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2823 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2824 disposition = SCTP_DISPOSITION_CONSUME;
2826 if (sctp_outq_is_empty(&asoc->outqueue)) {
2827 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2828 arg, commands);
2831 if (SCTP_DISPOSITION_NOMEM == disposition)
2832 goto out;
2834 /* - verify, by checking the Cumulative TSN Ack field of the
2835 * chunk, that all its outstanding DATA chunks have been
2836 * received by the SHUTDOWN sender.
2838 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2839 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2841 out:
2842 return disposition;
2846 * sctp_sf_do_9_2_shut_ctsn
2848 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2849 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2850 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2851 * MUST be processed.
2853 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2854 struct net *net,
2855 const struct sctp_endpoint *ep,
2856 const struct sctp_association *asoc,
2857 const union sctp_subtype type,
2858 void *arg,
2859 struct sctp_cmd_seq *commands)
2861 struct sctp_chunk *chunk = arg;
2862 struct sctp_shutdownhdr *sdh;
2863 __u32 ctsn;
2865 if (!sctp_vtag_verify(chunk, asoc))
2866 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2868 /* Make sure that the SHUTDOWN chunk has a valid length. */
2869 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2870 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2871 commands);
2873 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2874 ctsn = ntohl(sdh->cum_tsn_ack);
2876 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2877 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2878 asoc->ctsn_ack_point);
2880 return SCTP_DISPOSITION_DISCARD;
2883 /* If Cumulative TSN Ack beyond the max tsn currently
2884 * send, terminating the association and respond to the
2885 * sender with an ABORT.
2887 if (!TSN_lt(ctsn, asoc->next_tsn))
2888 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2890 /* verify, by checking the Cumulative TSN Ack field of the
2891 * chunk, that all its outstanding DATA chunks have been
2892 * received by the SHUTDOWN sender.
2894 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2895 SCTP_BE32(sdh->cum_tsn_ack));
2897 return SCTP_DISPOSITION_CONSUME;
2900 /* RFC 2960 9.2
2901 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2902 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2903 * transport addresses (either in the IP addresses or in the INIT chunk)
2904 * that belong to this association, it should discard the INIT chunk and
2905 * retransmit the SHUTDOWN ACK chunk.
2907 enum sctp_disposition sctp_sf_do_9_2_reshutack(
2908 struct net *net,
2909 const struct sctp_endpoint *ep,
2910 const struct sctp_association *asoc,
2911 const union sctp_subtype type,
2912 void *arg,
2913 struct sctp_cmd_seq *commands)
2915 struct sctp_chunk *chunk = arg;
2916 struct sctp_chunk *reply;
2918 /* Make sure that the chunk has a valid length */
2919 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2920 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2921 commands);
2923 /* Since we are not going to really process this INIT, there
2924 * is no point in verifying chunk boundries. Just generate
2925 * the SHUTDOWN ACK.
2927 reply = sctp_make_shutdown_ack(asoc, chunk);
2928 if (NULL == reply)
2929 goto nomem;
2931 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2932 * the T2-SHUTDOWN timer.
2934 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2936 /* and restart the T2-shutdown timer. */
2937 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2938 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2940 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2942 return SCTP_DISPOSITION_CONSUME;
2943 nomem:
2944 return SCTP_DISPOSITION_NOMEM;
2948 * sctp_sf_do_ecn_cwr
2950 * Section: Appendix A: Explicit Congestion Notification
2952 * CWR:
2954 * RFC 2481 details a specific bit for a sender to send in the header of
2955 * its next outbound TCP segment to indicate to its peer that it has
2956 * reduced its congestion window. This is termed the CWR bit. For
2957 * SCTP the same indication is made by including the CWR chunk.
2958 * This chunk contains one data element, i.e. the TSN number that
2959 * was sent in the ECNE chunk. This element represents the lowest
2960 * TSN number in the datagram that was originally marked with the
2961 * CE bit.
2963 * Verification Tag: 8.5 Verification Tag [Normal verification]
2964 * Inputs
2965 * (endpoint, asoc, chunk)
2967 * Outputs
2968 * (asoc, reply_msg, msg_up, timers, counters)
2970 * The return value is the disposition of the chunk.
2972 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2973 const struct sctp_endpoint *ep,
2974 const struct sctp_association *asoc,
2975 const union sctp_subtype type,
2976 void *arg,
2977 struct sctp_cmd_seq *commands)
2979 struct sctp_chunk *chunk = arg;
2980 struct sctp_cwrhdr *cwr;
2981 u32 lowest_tsn;
2983 if (!sctp_vtag_verify(chunk, asoc))
2984 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2986 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
2987 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2988 commands);
2990 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2991 skb_pull(chunk->skb, sizeof(*cwr));
2993 lowest_tsn = ntohl(cwr->lowest_tsn);
2995 /* Does this CWR ack the last sent congestion notification? */
2996 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2997 /* Stop sending ECNE. */
2998 sctp_add_cmd_sf(commands,
2999 SCTP_CMD_ECN_CWR,
3000 SCTP_U32(lowest_tsn));
3002 return SCTP_DISPOSITION_CONSUME;
3006 * sctp_sf_do_ecne
3008 * Section: Appendix A: Explicit Congestion Notification
3010 * ECN-Echo
3012 * RFC 2481 details a specific bit for a receiver to send back in its
3013 * TCP acknowledgements to notify the sender of the Congestion
3014 * Experienced (CE) bit having arrived from the network. For SCTP this
3015 * same indication is made by including the ECNE chunk. This chunk
3016 * contains one data element, i.e. the lowest TSN associated with the IP
3017 * datagram marked with the CE bit.....
3019 * Verification Tag: 8.5 Verification Tag [Normal verification]
3020 * Inputs
3021 * (endpoint, asoc, chunk)
3023 * Outputs
3024 * (asoc, reply_msg, msg_up, timers, counters)
3026 * The return value is the disposition of the chunk.
3028 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3029 const struct sctp_endpoint *ep,
3030 const struct sctp_association *asoc,
3031 const union sctp_subtype type,
3032 void *arg, struct sctp_cmd_seq *commands)
3034 struct sctp_chunk *chunk = arg;
3035 struct sctp_ecnehdr *ecne;
3037 if (!sctp_vtag_verify(chunk, asoc))
3038 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3040 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3041 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3042 commands);
3044 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3045 skb_pull(chunk->skb, sizeof(*ecne));
3047 /* If this is a newer ECNE than the last CWR packet we sent out */
3048 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3049 SCTP_U32(ntohl(ecne->lowest_tsn)));
3051 return SCTP_DISPOSITION_CONSUME;
3055 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3057 * The SCTP endpoint MUST always acknowledge the reception of each valid
3058 * DATA chunk.
3060 * The guidelines on delayed acknowledgement algorithm specified in
3061 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3062 * acknowledgement SHOULD be generated for at least every second packet
3063 * (not every second DATA chunk) received, and SHOULD be generated within
3064 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3065 * situations it may be beneficial for an SCTP transmitter to be more
3066 * conservative than the algorithms detailed in this document allow.
3067 * However, an SCTP transmitter MUST NOT be more aggressive than the
3068 * following algorithms allow.
3070 * A SCTP receiver MUST NOT generate more than one SACK for every
3071 * incoming packet, other than to update the offered window as the
3072 * receiving application consumes new data.
3074 * Verification Tag: 8.5 Verification Tag [Normal verification]
3076 * Inputs
3077 * (endpoint, asoc, chunk)
3079 * Outputs
3080 * (asoc, reply_msg, msg_up, timers, counters)
3082 * The return value is the disposition of the chunk.
3084 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3085 const struct sctp_endpoint *ep,
3086 const struct sctp_association *asoc,
3087 const union sctp_subtype type,
3088 void *arg,
3089 struct sctp_cmd_seq *commands)
3091 union sctp_arg force = SCTP_NOFORCE();
3092 struct sctp_chunk *chunk = arg;
3093 int error;
3095 if (!sctp_vtag_verify(chunk, asoc)) {
3096 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3097 SCTP_NULL());
3098 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3101 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3102 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3103 commands);
3105 error = sctp_eat_data(asoc, chunk, commands);
3106 switch (error) {
3107 case SCTP_IERROR_NO_ERROR:
3108 break;
3109 case SCTP_IERROR_HIGH_TSN:
3110 case SCTP_IERROR_BAD_STREAM:
3111 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3112 goto discard_noforce;
3113 case SCTP_IERROR_DUP_TSN:
3114 case SCTP_IERROR_IGNORE_TSN:
3115 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3116 goto discard_force;
3117 case SCTP_IERROR_NO_DATA:
3118 return SCTP_DISPOSITION_ABORT;
3119 case SCTP_IERROR_PROTO_VIOLATION:
3120 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3121 (u8 *)chunk->subh.data_hdr,
3122 sctp_datahdr_len(&asoc->stream));
3123 default:
3124 BUG();
3127 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3128 force = SCTP_FORCE();
3130 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3131 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3132 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3135 /* If this is the last chunk in a packet, we need to count it
3136 * toward sack generation. Note that we need to SACK every
3137 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3138 * THEM. We elect to NOT generate SACK's if the chunk fails
3139 * the verification tag test.
3141 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3143 * The SCTP endpoint MUST always acknowledge the reception of
3144 * each valid DATA chunk.
3146 * The guidelines on delayed acknowledgement algorithm
3147 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3148 * Specifically, an acknowledgement SHOULD be generated for at
3149 * least every second packet (not every second DATA chunk)
3150 * received, and SHOULD be generated within 200 ms of the
3151 * arrival of any unacknowledged DATA chunk. In some
3152 * situations it may be beneficial for an SCTP transmitter to
3153 * be more conservative than the algorithms detailed in this
3154 * document allow. However, an SCTP transmitter MUST NOT be
3155 * more aggressive than the following algorithms allow.
3157 if (chunk->end_of_packet)
3158 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3160 return SCTP_DISPOSITION_CONSUME;
3162 discard_force:
3163 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3165 * When a packet arrives with duplicate DATA chunk(s) and with
3166 * no new DATA chunk(s), the endpoint MUST immediately send a
3167 * SACK with no delay. If a packet arrives with duplicate
3168 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3169 * MAY immediately send a SACK. Normally receipt of duplicate
3170 * DATA chunks will occur when the original SACK chunk was lost
3171 * and the peer's RTO has expired. The duplicate TSN number(s)
3172 * SHOULD be reported in the SACK as duplicate.
3174 /* In our case, we split the MAY SACK advice up whether or not
3175 * the last chunk is a duplicate.'
3177 if (chunk->end_of_packet)
3178 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3179 return SCTP_DISPOSITION_DISCARD;
3181 discard_noforce:
3182 if (chunk->end_of_packet)
3183 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3185 return SCTP_DISPOSITION_DISCARD;
3189 * sctp_sf_eat_data_fast_4_4
3191 * Section: 4 (4)
3192 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3193 * DATA chunks without delay.
3195 * Verification Tag: 8.5 Verification Tag [Normal verification]
3196 * Inputs
3197 * (endpoint, asoc, chunk)
3199 * Outputs
3200 * (asoc, reply_msg, msg_up, timers, counters)
3202 * The return value is the disposition of the chunk.
3204 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3205 struct net *net,
3206 const struct sctp_endpoint *ep,
3207 const struct sctp_association *asoc,
3208 const union sctp_subtype type,
3209 void *arg,
3210 struct sctp_cmd_seq *commands)
3212 struct sctp_chunk *chunk = arg;
3213 int error;
3215 if (!sctp_vtag_verify(chunk, asoc)) {
3216 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3217 SCTP_NULL());
3218 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3221 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3222 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3223 commands);
3225 error = sctp_eat_data(asoc, chunk, commands);
3226 switch (error) {
3227 case SCTP_IERROR_NO_ERROR:
3228 case SCTP_IERROR_HIGH_TSN:
3229 case SCTP_IERROR_DUP_TSN:
3230 case SCTP_IERROR_IGNORE_TSN:
3231 case SCTP_IERROR_BAD_STREAM:
3232 break;
3233 case SCTP_IERROR_NO_DATA:
3234 return SCTP_DISPOSITION_ABORT;
3235 case SCTP_IERROR_PROTO_VIOLATION:
3236 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3237 (u8 *)chunk->subh.data_hdr,
3238 sctp_datahdr_len(&asoc->stream));
3239 default:
3240 BUG();
3243 /* Go a head and force a SACK, since we are shutting down. */
3245 /* Implementor's Guide.
3247 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3248 * respond to each received packet containing one or more DATA chunk(s)
3249 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3251 if (chunk->end_of_packet) {
3252 /* We must delay the chunk creation since the cumulative
3253 * TSN has not been updated yet.
3255 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3256 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3258 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3261 return SCTP_DISPOSITION_CONSUME;
3265 * Section: 6.2 Processing a Received SACK
3266 * D) Any time a SACK arrives, the endpoint performs the following:
3268 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3269 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3270 * increasing, a SACK whose Cumulative TSN Ack is less than the
3271 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3273 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3274 * of bytes still outstanding after processing the Cumulative TSN Ack
3275 * and the Gap Ack Blocks.
3277 * iii) If the SACK is missing a TSN that was previously
3278 * acknowledged via a Gap Ack Block (e.g., the data receiver
3279 * reneged on the data), then mark the corresponding DATA chunk
3280 * as available for retransmit: Mark it as missing for fast
3281 * retransmit as described in Section 7.2.4 and if no retransmit
3282 * timer is running for the destination address to which the DATA
3283 * chunk was originally transmitted, then T3-rtx is started for
3284 * that destination address.
3286 * Verification Tag: 8.5 Verification Tag [Normal verification]
3288 * Inputs
3289 * (endpoint, asoc, chunk)
3291 * Outputs
3292 * (asoc, reply_msg, msg_up, timers, counters)
3294 * The return value is the disposition of the chunk.
3296 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3297 const struct sctp_endpoint *ep,
3298 const struct sctp_association *asoc,
3299 const union sctp_subtype type,
3300 void *arg,
3301 struct sctp_cmd_seq *commands)
3303 struct sctp_chunk *chunk = arg;
3304 struct sctp_sackhdr *sackh;
3305 __u32 ctsn;
3307 if (!sctp_vtag_verify(chunk, asoc))
3308 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3310 /* Make sure that the SACK chunk has a valid length. */
3311 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3312 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3313 commands);
3315 /* Pull the SACK chunk from the data buffer */
3316 sackh = sctp_sm_pull_sack(chunk);
3317 /* Was this a bogus SACK? */
3318 if (!sackh)
3319 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3320 chunk->subh.sack_hdr = sackh;
3321 ctsn = ntohl(sackh->cum_tsn_ack);
3323 /* If Cumulative TSN Ack beyond the max tsn currently
3324 * send, terminating the association and respond to the
3325 * sender with an ABORT.
3327 if (TSN_lte(asoc->next_tsn, ctsn))
3328 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3330 trace_sctp_probe(ep, asoc, chunk);
3332 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3333 * Ack Point, then drop the SACK. Since Cumulative TSN
3334 * Ack is monotonically increasing, a SACK whose
3335 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3336 * Point indicates an out-of-order SACK.
3338 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3339 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3340 asoc->ctsn_ack_point);
3342 return SCTP_DISPOSITION_DISCARD;
3345 /* Return this SACK for further processing. */
3346 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3348 /* Note: We do the rest of the work on the PROCESS_SACK
3349 * sideeffect.
3351 return SCTP_DISPOSITION_CONSUME;
3355 * Generate an ABORT in response to a packet.
3357 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3359 * 8) The receiver should respond to the sender of the OOTB packet with
3360 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3361 * MUST fill in the Verification Tag field of the outbound packet
3362 * with the value found in the Verification Tag field of the OOTB
3363 * packet and set the T-bit in the Chunk Flags to indicate that the
3364 * Verification Tag is reflected. After sending this ABORT, the
3365 * receiver of the OOTB packet shall discard the OOTB packet and take
3366 * no further action.
3368 * Verification Tag:
3370 * The return value is the disposition of the chunk.
3372 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3373 struct net *net,
3374 const struct sctp_endpoint *ep,
3375 const struct sctp_association *asoc,
3376 const union sctp_subtype type,
3377 void *arg,
3378 struct sctp_cmd_seq *commands)
3380 struct sctp_packet *packet = NULL;
3381 struct sctp_chunk *chunk = arg;
3382 struct sctp_chunk *abort;
3384 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3385 if (!packet)
3386 return SCTP_DISPOSITION_NOMEM;
3388 /* Make an ABORT. The T bit will be set if the asoc
3389 * is NULL.
3391 abort = sctp_make_abort(asoc, chunk, 0);
3392 if (!abort) {
3393 sctp_ootb_pkt_free(packet);
3394 return SCTP_DISPOSITION_NOMEM;
3397 /* Reflect vtag if T-Bit is set */
3398 if (sctp_test_T_bit(abort))
3399 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3401 /* Set the skb to the belonging sock for accounting. */
3402 abort->skb->sk = ep->base.sk;
3404 sctp_packet_append_chunk(packet, abort);
3406 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(packet));
3408 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3410 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3411 return SCTP_DISPOSITION_CONSUME;
3414 /* Handling of SCTP Packets Containing an INIT Chunk Matching an
3415 * Existing Associations when the UDP encap port is incorrect.
3417 * From Section 4 at draft-tuexen-tsvwg-sctp-udp-encaps-cons-03.
3419 static enum sctp_disposition sctp_sf_new_encap_port(
3420 struct net *net,
3421 const struct sctp_endpoint *ep,
3422 const struct sctp_association *asoc,
3423 const union sctp_subtype type,
3424 void *arg,
3425 struct sctp_cmd_seq *commands)
3427 struct sctp_packet *packet = NULL;
3428 struct sctp_chunk *chunk = arg;
3429 struct sctp_chunk *abort;
3431 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3432 if (!packet)
3433 return SCTP_DISPOSITION_NOMEM;
3435 abort = sctp_make_new_encap_port(asoc, chunk);
3436 if (!abort) {
3437 sctp_ootb_pkt_free(packet);
3438 return SCTP_DISPOSITION_NOMEM;
3441 abort->skb->sk = ep->base.sk;
3443 sctp_packet_append_chunk(packet, abort);
3445 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3446 SCTP_PACKET(packet));
3448 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3450 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3451 return SCTP_DISPOSITION_CONSUME;
3455 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3456 * event as ULP notification for each cause included in the chunk.
3458 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3460 * The return value is the disposition of the chunk.
3462 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3463 const struct sctp_endpoint *ep,
3464 const struct sctp_association *asoc,
3465 const union sctp_subtype type,
3466 void *arg,
3467 struct sctp_cmd_seq *commands)
3469 struct sctp_chunk *chunk = arg;
3470 struct sctp_errhdr *err;
3472 if (!sctp_vtag_verify(chunk, asoc))
3473 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3475 /* Make sure that the ERROR chunk has a valid length. */
3476 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3477 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3478 commands);
3479 sctp_walk_errors(err, chunk->chunk_hdr);
3480 if ((void *)err != (void *)chunk->chunk_end)
3481 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3482 (void *)err, commands);
3484 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3485 SCTP_CHUNK(chunk));
3487 return SCTP_DISPOSITION_CONSUME;
3491 * Process an inbound SHUTDOWN ACK.
3493 * From Section 9.2:
3494 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3495 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3496 * peer, and remove all record of the association.
3498 * The return value is the disposition.
3500 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3501 const struct sctp_endpoint *ep,
3502 const struct sctp_association *asoc,
3503 const union sctp_subtype type,
3504 void *arg,
3505 struct sctp_cmd_seq *commands)
3507 struct sctp_chunk *chunk = arg;
3508 struct sctp_chunk *reply;
3509 struct sctp_ulpevent *ev;
3511 if (!sctp_vtag_verify(chunk, asoc))
3512 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3514 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3515 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3516 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3517 commands);
3518 /* 10.2 H) SHUTDOWN COMPLETE notification
3520 * When SCTP completes the shutdown procedures (section 9.2) this
3521 * notification is passed to the upper layer.
3523 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3524 0, 0, 0, NULL, GFP_ATOMIC);
3525 if (!ev)
3526 goto nomem;
3528 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3529 reply = sctp_make_shutdown_complete(asoc, chunk);
3530 if (!reply)
3531 goto nomem_chunk;
3533 /* Do all the commands now (after allocation), so that we
3534 * have consistent state if memory allocation failes
3536 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3538 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3539 * stop the T2-shutdown timer,
3541 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3542 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3544 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3545 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3547 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3548 SCTP_STATE(SCTP_STATE_CLOSED));
3549 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3550 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3551 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3553 /* ...and remove all record of the association. */
3554 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3555 return SCTP_DISPOSITION_DELETE_TCB;
3557 nomem_chunk:
3558 sctp_ulpevent_free(ev);
3559 nomem:
3560 return SCTP_DISPOSITION_NOMEM;
3564 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3566 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3567 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3568 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3569 * packet must fill in the Verification Tag field of the outbound
3570 * packet with the Verification Tag received in the SHUTDOWN ACK and
3571 * set the T-bit in the Chunk Flags to indicate that the Verification
3572 * Tag is reflected.
3574 * 8) The receiver should respond to the sender of the OOTB packet with
3575 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3576 * MUST fill in the Verification Tag field of the outbound packet
3577 * with the value found in the Verification Tag field of the OOTB
3578 * packet and set the T-bit in the Chunk Flags to indicate that the
3579 * Verification Tag is reflected. After sending this ABORT, the
3580 * receiver of the OOTB packet shall discard the OOTB packet and take
3581 * no further action.
3583 enum sctp_disposition sctp_sf_ootb(struct net *net,
3584 const struct sctp_endpoint *ep,
3585 const struct sctp_association *asoc,
3586 const union sctp_subtype type,
3587 void *arg, struct sctp_cmd_seq *commands)
3589 struct sctp_chunk *chunk = arg;
3590 struct sk_buff *skb = chunk->skb;
3591 struct sctp_chunkhdr *ch;
3592 struct sctp_errhdr *err;
3593 int ootb_cookie_ack = 0;
3594 int ootb_shut_ack = 0;
3595 __u8 *ch_end;
3597 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3599 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3600 do {
3601 /* Report violation if the chunk is less then minimal */
3602 if (ntohs(ch->length) < sizeof(*ch))
3603 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3604 commands);
3606 /* Report violation if chunk len overflows */
3607 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3608 if (ch_end > skb_tail_pointer(skb))
3609 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3610 commands);
3612 /* Now that we know we at least have a chunk header,
3613 * do things that are type appropriate.
3615 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3616 ootb_shut_ack = 1;
3618 /* RFC 2960, Section 3.3.7
3619 * Moreover, under any circumstances, an endpoint that
3620 * receives an ABORT MUST NOT respond to that ABORT by
3621 * sending an ABORT of its own.
3623 if (SCTP_CID_ABORT == ch->type)
3624 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3626 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3627 * or a COOKIE ACK the SCTP Packet should be silently
3628 * discarded.
3631 if (SCTP_CID_COOKIE_ACK == ch->type)
3632 ootb_cookie_ack = 1;
3634 if (SCTP_CID_ERROR == ch->type) {
3635 sctp_walk_errors(err, ch) {
3636 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3637 ootb_cookie_ack = 1;
3638 break;
3643 ch = (struct sctp_chunkhdr *)ch_end;
3644 } while (ch_end < skb_tail_pointer(skb));
3646 if (ootb_shut_ack)
3647 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3648 else if (ootb_cookie_ack)
3649 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3650 else
3651 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3655 * Handle an "Out of the blue" SHUTDOWN ACK.
3657 * Section: 8.4 5, sctpimpguide 2.41.
3659 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3660 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3661 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3662 * packet must fill in the Verification Tag field of the outbound
3663 * packet with the Verification Tag received in the SHUTDOWN ACK and
3664 * set the T-bit in the Chunk Flags to indicate that the Verification
3665 * Tag is reflected.
3667 * Inputs
3668 * (endpoint, asoc, type, arg, commands)
3670 * Outputs
3671 * (enum sctp_disposition)
3673 * The return value is the disposition of the chunk.
3675 static enum sctp_disposition sctp_sf_shut_8_4_5(
3676 struct net *net,
3677 const struct sctp_endpoint *ep,
3678 const struct sctp_association *asoc,
3679 const union sctp_subtype type,
3680 void *arg,
3681 struct sctp_cmd_seq *commands)
3683 struct sctp_packet *packet = NULL;
3684 struct sctp_chunk *chunk = arg;
3685 struct sctp_chunk *shut;
3687 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3688 if (!packet)
3689 return SCTP_DISPOSITION_NOMEM;
3691 /* Make an SHUTDOWN_COMPLETE.
3692 * The T bit will be set if the asoc is NULL.
3694 shut = sctp_make_shutdown_complete(asoc, chunk);
3695 if (!shut) {
3696 sctp_ootb_pkt_free(packet);
3697 return SCTP_DISPOSITION_NOMEM;
3700 /* Reflect vtag if T-Bit is set */
3701 if (sctp_test_T_bit(shut))
3702 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3704 /* Set the skb to the belonging sock for accounting. */
3705 shut->skb->sk = ep->base.sk;
3707 sctp_packet_append_chunk(packet, shut);
3709 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3710 SCTP_PACKET(packet));
3712 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3714 /* If the chunk length is invalid, we don't want to process
3715 * the reset of the packet.
3717 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3718 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3720 /* We need to discard the rest of the packet to prevent
3721 * potential bomming attacks from additional bundled chunks.
3722 * This is documented in SCTP Threats ID.
3724 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3728 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3730 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3731 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3732 * procedures in section 8.4 SHOULD be followed, in other words it
3733 * should be treated as an Out Of The Blue packet.
3734 * [This means that we do NOT check the Verification Tag on these
3735 * chunks. --piggy ]
3738 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3739 const struct sctp_endpoint *ep,
3740 const struct sctp_association *asoc,
3741 const union sctp_subtype type,
3742 void *arg,
3743 struct sctp_cmd_seq *commands)
3745 struct sctp_chunk *chunk = arg;
3747 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3748 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3749 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3750 commands);
3752 /* Although we do have an association in this case, it corresponds
3753 * to a restarted association. So the packet is treated as an OOTB
3754 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3755 * called with a NULL association.
3757 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3759 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3762 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3763 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3764 const struct sctp_endpoint *ep,
3765 const struct sctp_association *asoc,
3766 const union sctp_subtype type,
3767 void *arg,
3768 struct sctp_cmd_seq *commands)
3770 struct sctp_paramhdr *err_param = NULL;
3771 struct sctp_chunk *asconf_ack = NULL;
3772 struct sctp_chunk *chunk = arg;
3773 struct sctp_addiphdr *hdr;
3774 __u32 serial;
3776 if (!sctp_vtag_verify(chunk, asoc)) {
3777 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3778 SCTP_NULL());
3779 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3782 /* ADD-IP: Section 4.1.1
3783 * This chunk MUST be sent in an authenticated way by using
3784 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3785 * is received unauthenticated it MUST be silently discarded as
3786 * described in [I-D.ietf-tsvwg-sctp-auth].
3788 if (!asoc->peer.asconf_capable ||
3789 (!net->sctp.addip_noauth && !chunk->auth))
3790 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3791 commands);
3793 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3794 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3795 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3796 commands);
3798 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3799 serial = ntohl(hdr->serial);
3801 /* Verify the ASCONF chunk before processing it. */
3802 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3803 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3804 (void *)err_param, commands);
3806 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3807 * the endpoint stored in a new association variable
3808 * 'Peer-Serial-Number'.
3810 if (serial == asoc->peer.addip_serial + 1) {
3811 /* If this is the first instance of ASCONF in the packet,
3812 * we can clean our old ASCONF-ACKs.
3814 if (!chunk->has_asconf)
3815 sctp_assoc_clean_asconf_ack_cache(asoc);
3817 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3818 * expected, process the ASCONF as described below and after
3819 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3820 * the response packet and cache a copy of it (in the event it
3821 * later needs to be retransmitted).
3823 * Essentially, do V1-V5.
3825 asconf_ack = sctp_process_asconf((struct sctp_association *)
3826 asoc, chunk);
3827 if (!asconf_ack)
3828 return SCTP_DISPOSITION_NOMEM;
3829 } else if (serial < asoc->peer.addip_serial + 1) {
3830 /* ADDIP 5.2 E2)
3831 * If the value found in the Sequence Number is less than the
3832 * ('Peer- Sequence-Number' + 1), simply skip to the next
3833 * ASCONF, and include in the outbound response packet
3834 * any previously cached ASCONF-ACK response that was
3835 * sent and saved that matches the Sequence Number of the
3836 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3837 * Chunk exists. This will occur when an older ASCONF
3838 * arrives out of order. In such a case, the receiver
3839 * should skip the ASCONF Chunk and not include ASCONF-ACK
3840 * Chunk for that chunk.
3842 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3843 if (!asconf_ack)
3844 return SCTP_DISPOSITION_DISCARD;
3846 /* Reset the transport so that we select the correct one
3847 * this time around. This is to make sure that we don't
3848 * accidentally use a stale transport that's been removed.
3850 asconf_ack->transport = NULL;
3851 } else {
3852 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3853 * it must be either a stale packet or from an attacker.
3855 return SCTP_DISPOSITION_DISCARD;
3858 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3859 * containing the ASCONF-ACK Chunks MUST be the source address of
3860 * the SCTP packet that held the ASCONF Chunks.
3862 * To do this properly, we'll set the destination address of the chunk
3863 * and at the transmit time, will try look up the transport to use.
3864 * Since ASCONFs may be bundled, the correct transport may not be
3865 * created until we process the entire packet, thus this workaround.
3867 asconf_ack->dest = chunk->source;
3868 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3869 if (asoc->new_transport) {
3870 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3871 ((struct sctp_association *)asoc)->new_transport = NULL;
3874 return SCTP_DISPOSITION_CONSUME;
3877 static enum sctp_disposition sctp_send_next_asconf(
3878 struct net *net,
3879 const struct sctp_endpoint *ep,
3880 struct sctp_association *asoc,
3881 const union sctp_subtype type,
3882 struct sctp_cmd_seq *commands)
3884 struct sctp_chunk *asconf;
3885 struct list_head *entry;
3887 if (list_empty(&asoc->addip_chunk_list))
3888 return SCTP_DISPOSITION_CONSUME;
3890 entry = asoc->addip_chunk_list.next;
3891 asconf = list_entry(entry, struct sctp_chunk, list);
3893 list_del_init(entry);
3894 sctp_chunk_hold(asconf);
3895 asoc->addip_last_asconf = asconf;
3897 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3901 * ADDIP Section 4.3 General rules for address manipulation
3902 * When building TLV parameters for the ASCONF Chunk that will add or
3903 * delete IP addresses the D0 to D13 rules should be applied:
3905 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3906 const struct sctp_endpoint *ep,
3907 const struct sctp_association *asoc,
3908 const union sctp_subtype type,
3909 void *arg,
3910 struct sctp_cmd_seq *commands)
3912 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3913 struct sctp_paramhdr *err_param = NULL;
3914 struct sctp_chunk *asconf_ack = arg;
3915 struct sctp_addiphdr *addip_hdr;
3916 __u32 sent_serial, rcvd_serial;
3917 struct sctp_chunk *abort;
3919 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3920 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3921 SCTP_NULL());
3922 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3925 /* ADD-IP, Section 4.1.2:
3926 * This chunk MUST be sent in an authenticated way by using
3927 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3928 * is received unauthenticated it MUST be silently discarded as
3929 * described in [I-D.ietf-tsvwg-sctp-auth].
3931 if (!asoc->peer.asconf_capable ||
3932 (!net->sctp.addip_noauth && !asconf_ack->auth))
3933 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3934 commands);
3936 /* Make sure that the ADDIP chunk has a valid length. */
3937 if (!sctp_chunk_length_valid(asconf_ack,
3938 sizeof(struct sctp_addip_chunk)))
3939 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3940 commands);
3942 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3943 rcvd_serial = ntohl(addip_hdr->serial);
3945 /* Verify the ASCONF-ACK chunk before processing it. */
3946 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3947 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3948 (void *)err_param, commands);
3950 if (last_asconf) {
3951 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3952 sent_serial = ntohl(addip_hdr->serial);
3953 } else {
3954 sent_serial = asoc->addip_serial - 1;
3957 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3958 * equal to the next serial number to be used but no ASCONF chunk is
3959 * outstanding the endpoint MUST ABORT the association. Note that a
3960 * sequence number is greater than if it is no more than 2^^31-1
3961 * larger than the current sequence number (using serial arithmetic).
3963 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3964 !(asoc->addip_last_asconf)) {
3965 abort = sctp_make_abort(asoc, asconf_ack,
3966 sizeof(struct sctp_errhdr));
3967 if (abort) {
3968 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3969 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3970 SCTP_CHUNK(abort));
3972 /* We are going to ABORT, so we might as well stop
3973 * processing the rest of the chunks in the packet.
3975 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3976 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3977 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3978 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3979 SCTP_ERROR(ECONNABORTED));
3980 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3981 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3982 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3983 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3984 return SCTP_DISPOSITION_ABORT;
3987 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3988 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3989 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3991 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3992 asconf_ack))
3993 return sctp_send_next_asconf(net, ep,
3994 (struct sctp_association *)asoc,
3995 type, commands);
3997 abort = sctp_make_abort(asoc, asconf_ack,
3998 sizeof(struct sctp_errhdr));
3999 if (abort) {
4000 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
4001 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4002 SCTP_CHUNK(abort));
4004 /* We are going to ABORT, so we might as well stop
4005 * processing the rest of the chunks in the packet.
4007 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4008 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4009 SCTP_ERROR(ECONNABORTED));
4010 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4011 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4012 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4013 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4014 return SCTP_DISPOSITION_ABORT;
4017 return SCTP_DISPOSITION_DISCARD;
4020 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
4021 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
4022 const struct sctp_endpoint *ep,
4023 const struct sctp_association *asoc,
4024 const union sctp_subtype type,
4025 void *arg,
4026 struct sctp_cmd_seq *commands)
4028 struct sctp_paramhdr *err_param = NULL;
4029 struct sctp_chunk *chunk = arg;
4030 struct sctp_reconf_chunk *hdr;
4031 union sctp_params param;
4033 if (!sctp_vtag_verify(chunk, asoc)) {
4034 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4035 SCTP_NULL());
4036 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4039 /* Make sure that the RECONF chunk has a valid length. */
4040 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
4041 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4042 commands);
4044 if (!sctp_verify_reconf(asoc, chunk, &err_param))
4045 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4046 (void *)err_param, commands);
4048 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
4049 sctp_walk_params(param, hdr, params) {
4050 struct sctp_chunk *reply = NULL;
4051 struct sctp_ulpevent *ev = NULL;
4053 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4054 reply = sctp_process_strreset_outreq(
4055 (struct sctp_association *)asoc, param, &ev);
4056 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4057 reply = sctp_process_strreset_inreq(
4058 (struct sctp_association *)asoc, param, &ev);
4059 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4060 reply = sctp_process_strreset_tsnreq(
4061 (struct sctp_association *)asoc, param, &ev);
4062 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4063 reply = sctp_process_strreset_addstrm_out(
4064 (struct sctp_association *)asoc, param, &ev);
4065 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4066 reply = sctp_process_strreset_addstrm_in(
4067 (struct sctp_association *)asoc, param, &ev);
4068 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4069 reply = sctp_process_strreset_resp(
4070 (struct sctp_association *)asoc, param, &ev);
4072 if (ev)
4073 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4074 SCTP_ULPEVENT(ev));
4076 if (reply)
4077 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4078 SCTP_CHUNK(reply));
4081 return SCTP_DISPOSITION_CONSUME;
4085 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4087 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4088 * its cumulative TSN point to the value carried in the FORWARD TSN
4089 * chunk, and then MUST further advance its cumulative TSN point locally
4090 * if possible.
4091 * After the above processing, the data receiver MUST stop reporting any
4092 * missing TSNs earlier than or equal to the new cumulative TSN point.
4094 * Verification Tag: 8.5 Verification Tag [Normal verification]
4096 * The return value is the disposition of the chunk.
4098 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4099 const struct sctp_endpoint *ep,
4100 const struct sctp_association *asoc,
4101 const union sctp_subtype type,
4102 void *arg,
4103 struct sctp_cmd_seq *commands)
4105 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4106 struct sctp_chunk *chunk = arg;
4107 __u16 len;
4108 __u32 tsn;
4110 if (!sctp_vtag_verify(chunk, asoc)) {
4111 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4112 SCTP_NULL());
4113 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4116 if (!asoc->peer.prsctp_capable)
4117 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4119 /* Make sure that the FORWARD_TSN chunk has valid length. */
4120 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4121 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4122 commands);
4124 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4125 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4126 len = ntohs(chunk->chunk_hdr->length);
4127 len -= sizeof(struct sctp_chunkhdr);
4128 skb_pull(chunk->skb, len);
4130 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4131 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4133 /* The TSN is too high--silently discard the chunk and count on it
4134 * getting retransmitted later.
4136 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4137 goto discard_noforce;
4139 if (!asoc->stream.si->validate_ftsn(chunk))
4140 goto discard_noforce;
4142 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4143 if (len > sctp_ftsnhdr_len(&asoc->stream))
4144 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4145 SCTP_CHUNK(chunk));
4147 /* Count this as receiving DATA. */
4148 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4149 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4150 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4153 /* FIXME: For now send a SACK, but DATA processing may
4154 * send another.
4156 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4158 return SCTP_DISPOSITION_CONSUME;
4160 discard_noforce:
4161 return SCTP_DISPOSITION_DISCARD;
4164 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4165 struct net *net,
4166 const struct sctp_endpoint *ep,
4167 const struct sctp_association *asoc,
4168 const union sctp_subtype type,
4169 void *arg,
4170 struct sctp_cmd_seq *commands)
4172 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4173 struct sctp_chunk *chunk = arg;
4174 __u16 len;
4175 __u32 tsn;
4177 if (!sctp_vtag_verify(chunk, asoc)) {
4178 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4179 SCTP_NULL());
4180 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4183 if (!asoc->peer.prsctp_capable)
4184 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4186 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4187 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4188 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4189 commands);
4191 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4192 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4193 len = ntohs(chunk->chunk_hdr->length);
4194 len -= sizeof(struct sctp_chunkhdr);
4195 skb_pull(chunk->skb, len);
4197 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4198 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4200 /* The TSN is too high--silently discard the chunk and count on it
4201 * getting retransmitted later.
4203 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4204 goto gen_shutdown;
4206 if (!asoc->stream.si->validate_ftsn(chunk))
4207 goto gen_shutdown;
4209 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4210 if (len > sctp_ftsnhdr_len(&asoc->stream))
4211 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4212 SCTP_CHUNK(chunk));
4214 /* Go a head and force a SACK, since we are shutting down. */
4215 gen_shutdown:
4216 /* Implementor's Guide.
4218 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4219 * respond to each received packet containing one or more DATA chunk(s)
4220 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4222 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4223 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4224 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4225 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4227 return SCTP_DISPOSITION_CONSUME;
4231 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4233 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4234 * Identifier field. If this algorithm was not specified by the
4235 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4236 * during association setup, the AUTH chunk and all chunks after it MUST
4237 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4238 * defined in Section 4.1.
4240 * If an endpoint with no shared key receives a Shared Key Identifier
4241 * other than 0, it MUST silently discard all authenticated chunks. If
4242 * the endpoint has at least one endpoint pair shared key for the peer,
4243 * it MUST use the key specified by the Shared Key Identifier if a
4244 * key has been configured for that Shared Key Identifier. If no
4245 * endpoint pair shared key has been configured for that Shared Key
4246 * Identifier, all authenticated chunks MUST be silently discarded.
4248 * Verification Tag: 8.5 Verification Tag [Normal verification]
4250 * The return value is the disposition of the chunk.
4252 static enum sctp_ierror sctp_sf_authenticate(
4253 const struct sctp_association *asoc,
4254 struct sctp_chunk *chunk)
4256 struct sctp_shared_key *sh_key = NULL;
4257 struct sctp_authhdr *auth_hdr;
4258 __u8 *save_digest, *digest;
4259 struct sctp_hmac *hmac;
4260 unsigned int sig_len;
4261 __u16 key_id;
4263 /* Pull in the auth header, so we can do some more verification */
4264 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4265 chunk->subh.auth_hdr = auth_hdr;
4266 skb_pull(chunk->skb, sizeof(*auth_hdr));
4268 /* Make sure that we support the HMAC algorithm from the auth
4269 * chunk.
4271 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4272 return SCTP_IERROR_AUTH_BAD_HMAC;
4274 /* Make sure that the provided shared key identifier has been
4275 * configured
4277 key_id = ntohs(auth_hdr->shkey_id);
4278 if (key_id != asoc->active_key_id) {
4279 sh_key = sctp_auth_get_shkey(asoc, key_id);
4280 if (!sh_key)
4281 return SCTP_IERROR_AUTH_BAD_KEYID;
4284 /* Make sure that the length of the signature matches what
4285 * we expect.
4287 sig_len = ntohs(chunk->chunk_hdr->length) -
4288 sizeof(struct sctp_auth_chunk);
4289 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4290 if (sig_len != hmac->hmac_len)
4291 return SCTP_IERROR_PROTO_VIOLATION;
4293 /* Now that we've done validation checks, we can compute and
4294 * verify the hmac. The steps involved are:
4295 * 1. Save the digest from the chunk.
4296 * 2. Zero out the digest in the chunk.
4297 * 3. Compute the new digest
4298 * 4. Compare saved and new digests.
4300 digest = auth_hdr->hmac;
4301 skb_pull(chunk->skb, sig_len);
4303 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4304 if (!save_digest)
4305 goto nomem;
4307 memset(digest, 0, sig_len);
4309 sctp_auth_calculate_hmac(asoc, chunk->skb,
4310 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4311 sh_key, GFP_ATOMIC);
4313 /* Discard the packet if the digests do not match */
4314 if (memcmp(save_digest, digest, sig_len)) {
4315 kfree(save_digest);
4316 return SCTP_IERROR_BAD_SIG;
4319 kfree(save_digest);
4320 chunk->auth = 1;
4322 return SCTP_IERROR_NO_ERROR;
4323 nomem:
4324 return SCTP_IERROR_NOMEM;
4327 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4328 const struct sctp_endpoint *ep,
4329 const struct sctp_association *asoc,
4330 const union sctp_subtype type,
4331 void *arg, struct sctp_cmd_seq *commands)
4333 struct sctp_chunk *chunk = arg;
4334 struct sctp_authhdr *auth_hdr;
4335 struct sctp_chunk *err_chunk;
4336 enum sctp_ierror error;
4338 /* Make sure that the peer has AUTH capable */
4339 if (!asoc->peer.auth_capable)
4340 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4342 if (!sctp_vtag_verify(chunk, asoc)) {
4343 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4344 SCTP_NULL());
4345 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4348 /* Make sure that the AUTH chunk has valid length. */
4349 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4350 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4351 commands);
4353 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4354 error = sctp_sf_authenticate(asoc, chunk);
4355 switch (error) {
4356 case SCTP_IERROR_AUTH_BAD_HMAC:
4357 /* Generate the ERROR chunk and discard the rest
4358 * of the packet
4360 err_chunk = sctp_make_op_error(asoc, chunk,
4361 SCTP_ERROR_UNSUP_HMAC,
4362 &auth_hdr->hmac_id,
4363 sizeof(__u16), 0);
4364 if (err_chunk) {
4365 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4366 SCTP_CHUNK(err_chunk));
4368 fallthrough;
4369 case SCTP_IERROR_AUTH_BAD_KEYID:
4370 case SCTP_IERROR_BAD_SIG:
4371 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4373 case SCTP_IERROR_PROTO_VIOLATION:
4374 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4375 commands);
4377 case SCTP_IERROR_NOMEM:
4378 return SCTP_DISPOSITION_NOMEM;
4380 default: /* Prevent gcc warnings */
4381 break;
4384 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4385 struct sctp_ulpevent *ev;
4387 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4388 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4390 if (!ev)
4391 return -ENOMEM;
4393 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4394 SCTP_ULPEVENT(ev));
4397 return SCTP_DISPOSITION_CONSUME;
4401 * Process an unknown chunk.
4403 * Section: 3.2. Also, 2.1 in the implementor's guide.
4405 * Chunk Types are encoded such that the highest-order two bits specify
4406 * the action that must be taken if the processing endpoint does not
4407 * recognize the Chunk Type.
4409 * 00 - Stop processing this SCTP packet and discard it, do not process
4410 * any further chunks within it.
4412 * 01 - Stop processing this SCTP packet and discard it, do not process
4413 * any further chunks within it, and report the unrecognized
4414 * chunk in an 'Unrecognized Chunk Type'.
4416 * 10 - Skip this chunk and continue processing.
4418 * 11 - Skip this chunk and continue processing, but report in an ERROR
4419 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4421 * The return value is the disposition of the chunk.
4423 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4424 const struct sctp_endpoint *ep,
4425 const struct sctp_association *asoc,
4426 const union sctp_subtype type,
4427 void *arg,
4428 struct sctp_cmd_seq *commands)
4430 struct sctp_chunk *unk_chunk = arg;
4431 struct sctp_chunk *err_chunk;
4432 struct sctp_chunkhdr *hdr;
4434 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4436 if (!sctp_vtag_verify(unk_chunk, asoc))
4437 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4439 /* Make sure that the chunk has a valid length.
4440 * Since we don't know the chunk type, we use a general
4441 * chunkhdr structure to make a comparison.
4443 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4444 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4445 commands);
4447 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4448 case SCTP_CID_ACTION_DISCARD:
4449 /* Discard the packet. */
4450 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4451 case SCTP_CID_ACTION_DISCARD_ERR:
4452 /* Generate an ERROR chunk as response. */
4453 hdr = unk_chunk->chunk_hdr;
4454 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4455 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4456 SCTP_PAD4(ntohs(hdr->length)),
4458 if (err_chunk) {
4459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4460 SCTP_CHUNK(err_chunk));
4463 /* Discard the packet. */
4464 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4465 return SCTP_DISPOSITION_CONSUME;
4466 case SCTP_CID_ACTION_SKIP:
4467 /* Skip the chunk. */
4468 return SCTP_DISPOSITION_DISCARD;
4469 case SCTP_CID_ACTION_SKIP_ERR:
4470 /* Generate an ERROR chunk as response. */
4471 hdr = unk_chunk->chunk_hdr;
4472 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4473 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4474 SCTP_PAD4(ntohs(hdr->length)),
4476 if (err_chunk) {
4477 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4478 SCTP_CHUNK(err_chunk));
4480 /* Skip the chunk. */
4481 return SCTP_DISPOSITION_CONSUME;
4482 default:
4483 break;
4486 return SCTP_DISPOSITION_DISCARD;
4490 * Discard the chunk.
4492 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4493 * [Too numerous to mention...]
4494 * Verification Tag: No verification needed.
4495 * Inputs
4496 * (endpoint, asoc, chunk)
4498 * Outputs
4499 * (asoc, reply_msg, msg_up, timers, counters)
4501 * The return value is the disposition of the chunk.
4503 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4504 const struct sctp_endpoint *ep,
4505 const struct sctp_association *asoc,
4506 const union sctp_subtype type,
4507 void *arg,
4508 struct sctp_cmd_seq *commands)
4510 struct sctp_chunk *chunk = arg;
4512 /* Make sure that the chunk has a valid length.
4513 * Since we don't know the chunk type, we use a general
4514 * chunkhdr structure to make a comparison.
4516 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4517 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4518 commands);
4520 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4522 return SCTP_DISPOSITION_DISCARD;
4526 * Discard the whole packet.
4528 * Section: 8.4 2)
4530 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4531 * silently discard the OOTB packet and take no further action.
4533 * Verification Tag: No verification necessary
4535 * Inputs
4536 * (endpoint, asoc, chunk)
4538 * Outputs
4539 * (asoc, reply_msg, msg_up, timers, counters)
4541 * The return value is the disposition of the chunk.
4543 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4544 const struct sctp_endpoint *ep,
4545 const struct sctp_association *asoc,
4546 const union sctp_subtype type,
4547 void *arg, struct sctp_cmd_seq *commands)
4549 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4550 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4552 return SCTP_DISPOSITION_CONSUME;
4557 * The other end is violating protocol.
4559 * Section: Not specified
4560 * Verification Tag: Not specified
4561 * Inputs
4562 * (endpoint, asoc, chunk)
4564 * Outputs
4565 * (asoc, reply_msg, msg_up, timers, counters)
4567 * We simply tag the chunk as a violation. The state machine will log
4568 * the violation and continue.
4570 enum sctp_disposition sctp_sf_violation(struct net *net,
4571 const struct sctp_endpoint *ep,
4572 const struct sctp_association *asoc,
4573 const union sctp_subtype type,
4574 void *arg,
4575 struct sctp_cmd_seq *commands)
4577 struct sctp_chunk *chunk = arg;
4579 /* Make sure that the chunk has a valid length. */
4580 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4581 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4582 commands);
4584 return SCTP_DISPOSITION_VIOLATION;
4588 * Common function to handle a protocol violation.
4590 static enum sctp_disposition sctp_sf_abort_violation(
4591 struct net *net,
4592 const struct sctp_endpoint *ep,
4593 const struct sctp_association *asoc,
4594 void *arg,
4595 struct sctp_cmd_seq *commands,
4596 const __u8 *payload,
4597 const size_t paylen)
4599 struct sctp_packet *packet = NULL;
4600 struct sctp_chunk *chunk = arg;
4601 struct sctp_chunk *abort = NULL;
4603 /* SCTP-AUTH, Section 6.3:
4604 * It should be noted that if the receiver wants to tear
4605 * down an association in an authenticated way only, the
4606 * handling of malformed packets should not result in
4607 * tearing down the association.
4609 * This means that if we only want to abort associations
4610 * in an authenticated way (i.e AUTH+ABORT), then we
4611 * can't destroy this association just because the packet
4612 * was malformed.
4614 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4615 goto discard;
4617 /* Make the abort chunk. */
4618 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4619 if (!abort)
4620 goto nomem;
4622 if (asoc) {
4623 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4624 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4625 !asoc->peer.i.init_tag) {
4626 struct sctp_initack_chunk *initack;
4628 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4629 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4630 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4631 else {
4632 unsigned int inittag;
4634 inittag = ntohl(initack->init_hdr.init_tag);
4635 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4636 SCTP_U32(inittag));
4640 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4641 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4643 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4644 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4645 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4646 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4647 SCTP_ERROR(ECONNREFUSED));
4648 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4649 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4650 } else {
4651 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4652 SCTP_ERROR(ECONNABORTED));
4653 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4654 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4655 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4657 } else {
4658 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4660 if (!packet)
4661 goto nomem_pkt;
4663 if (sctp_test_T_bit(abort))
4664 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4666 abort->skb->sk = ep->base.sk;
4668 sctp_packet_append_chunk(packet, abort);
4670 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4671 SCTP_PACKET(packet));
4673 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4676 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4678 discard:
4679 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4680 return SCTP_DISPOSITION_ABORT;
4682 nomem_pkt:
4683 sctp_chunk_free(abort);
4684 nomem:
4685 return SCTP_DISPOSITION_NOMEM;
4689 * Handle a protocol violation when the chunk length is invalid.
4690 * "Invalid" length is identified as smaller than the minimal length a
4691 * given chunk can be. For example, a SACK chunk has invalid length
4692 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4694 * We inform the other end by sending an ABORT with a Protocol Violation
4695 * error code.
4697 * Section: Not specified
4698 * Verification Tag: Nothing to do
4699 * Inputs
4700 * (endpoint, asoc, chunk)
4702 * Outputs
4703 * (reply_msg, msg_up, counters)
4705 * Generate an ABORT chunk and terminate the association.
4707 static enum sctp_disposition sctp_sf_violation_chunklen(
4708 struct net *net,
4709 const struct sctp_endpoint *ep,
4710 const struct sctp_association *asoc,
4711 const union sctp_subtype type,
4712 void *arg,
4713 struct sctp_cmd_seq *commands)
4715 static const char err_str[] = "The following chunk had invalid length:";
4717 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4718 sizeof(err_str));
4722 * Handle a protocol violation when the parameter length is invalid.
4723 * If the length is smaller than the minimum length of a given parameter,
4724 * or accumulated length in multi parameters exceeds the end of the chunk,
4725 * the length is considered as invalid.
4727 static enum sctp_disposition sctp_sf_violation_paramlen(
4728 struct net *net,
4729 const struct sctp_endpoint *ep,
4730 const struct sctp_association *asoc,
4731 const union sctp_subtype type,
4732 void *arg, void *ext,
4733 struct sctp_cmd_seq *commands)
4735 struct sctp_paramhdr *param = ext;
4736 struct sctp_chunk *abort = NULL;
4737 struct sctp_chunk *chunk = arg;
4739 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4740 goto discard;
4742 /* Make the abort chunk. */
4743 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4744 if (!abort)
4745 goto nomem;
4747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4748 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4750 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4751 SCTP_ERROR(ECONNABORTED));
4752 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4753 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4754 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4755 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4757 discard:
4758 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4759 return SCTP_DISPOSITION_ABORT;
4760 nomem:
4761 return SCTP_DISPOSITION_NOMEM;
4764 /* Handle a protocol violation when the peer trying to advance the
4765 * cumulative tsn ack to a point beyond the max tsn currently sent.
4767 * We inform the other end by sending an ABORT with a Protocol Violation
4768 * error code.
4770 static enum sctp_disposition sctp_sf_violation_ctsn(
4771 struct net *net,
4772 const struct sctp_endpoint *ep,
4773 const struct sctp_association *asoc,
4774 const union sctp_subtype type,
4775 void *arg,
4776 struct sctp_cmd_seq *commands)
4778 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4780 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4781 sizeof(err_str));
4784 /* Handle protocol violation of an invalid chunk bundling. For example,
4785 * when we have an association and we receive bundled INIT-ACK, or
4786 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4787 * statement from the specs. Additionally, there might be an attacker
4788 * on the path and we may not want to continue this communication.
4790 static enum sctp_disposition sctp_sf_violation_chunk(
4791 struct net *net,
4792 const struct sctp_endpoint *ep,
4793 const struct sctp_association *asoc,
4794 const union sctp_subtype type,
4795 void *arg,
4796 struct sctp_cmd_seq *commands)
4798 static const char err_str[] = "The following chunk violates protocol:";
4800 if (!asoc)
4801 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4803 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4804 sizeof(err_str));
4806 /***************************************************************************
4807 * These are the state functions for handling primitive (Section 10) events.
4808 ***************************************************************************/
4810 * sctp_sf_do_prm_asoc
4812 * Section: 10.1 ULP-to-SCTP
4813 * B) Associate
4815 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4816 * outbound stream count)
4817 * -> association id [,destination transport addr list] [,outbound stream
4818 * count]
4820 * This primitive allows the upper layer to initiate an association to a
4821 * specific peer endpoint.
4823 * The peer endpoint shall be specified by one of the transport addresses
4824 * which defines the endpoint (see Section 1.4). If the local SCTP
4825 * instance has not been initialized, the ASSOCIATE is considered an
4826 * error.
4827 * [This is not relevant for the kernel implementation since we do all
4828 * initialization at boot time. It we hadn't initialized we wouldn't
4829 * get anywhere near this code.]
4831 * An association id, which is a local handle to the SCTP association,
4832 * will be returned on successful establishment of the association. If
4833 * SCTP is not able to open an SCTP association with the peer endpoint,
4834 * an error is returned.
4835 * [In the kernel implementation, the struct sctp_association needs to
4836 * be created BEFORE causing this primitive to run.]
4838 * Other association parameters may be returned, including the
4839 * complete destination transport addresses of the peer as well as the
4840 * outbound stream count of the local endpoint. One of the transport
4841 * address from the returned destination addresses will be selected by
4842 * the local endpoint as default primary path for sending SCTP packets
4843 * to this peer. The returned "destination transport addr list" can
4844 * be used by the ULP to change the default primary path or to force
4845 * sending a packet to a specific transport address. [All of this
4846 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4847 * function.]
4849 * Mandatory attributes:
4851 * o local SCTP instance name - obtained from the INITIALIZE operation.
4852 * [This is the argument asoc.]
4853 * o destination transport addr - specified as one of the transport
4854 * addresses of the peer endpoint with which the association is to be
4855 * established.
4856 * [This is asoc->peer.active_path.]
4857 * o outbound stream count - the number of outbound streams the ULP
4858 * would like to open towards this peer endpoint.
4859 * [BUG: This is not currently implemented.]
4860 * Optional attributes:
4862 * None.
4864 * The return value is a disposition.
4866 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4867 const struct sctp_endpoint *ep,
4868 const struct sctp_association *asoc,
4869 const union sctp_subtype type,
4870 void *arg,
4871 struct sctp_cmd_seq *commands)
4873 struct sctp_association *my_asoc;
4874 struct sctp_chunk *repl;
4876 /* The comment below says that we enter COOKIE-WAIT AFTER
4877 * sending the INIT, but that doesn't actually work in our
4878 * implementation...
4880 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4881 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4883 /* RFC 2960 5.1 Normal Establishment of an Association
4885 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4886 * must provide its Verification Tag (Tag_A) in the Initiate
4887 * Tag field. Tag_A SHOULD be a random number in the range of
4888 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4891 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4892 if (!repl)
4893 goto nomem;
4895 /* Choose transport for INIT. */
4896 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4897 SCTP_CHUNK(repl));
4899 /* Cast away the const modifier, as we want to just
4900 * rerun it through as a sideffect.
4902 my_asoc = (struct sctp_association *)asoc;
4903 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4905 /* After sending the INIT, "A" starts the T1-init timer and
4906 * enters the COOKIE-WAIT state.
4908 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4909 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4910 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4911 return SCTP_DISPOSITION_CONSUME;
4913 nomem:
4914 return SCTP_DISPOSITION_NOMEM;
4918 * Process the SEND primitive.
4920 * Section: 10.1 ULP-to-SCTP
4921 * E) Send
4923 * Format: SEND(association id, buffer address, byte count [,context]
4924 * [,stream id] [,life time] [,destination transport address]
4925 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4926 * -> result
4928 * This is the main method to send user data via SCTP.
4930 * Mandatory attributes:
4932 * o association id - local handle to the SCTP association
4934 * o buffer address - the location where the user message to be
4935 * transmitted is stored;
4937 * o byte count - The size of the user data in number of bytes;
4939 * Optional attributes:
4941 * o context - an optional 32 bit integer that will be carried in the
4942 * sending failure notification to the ULP if the transportation of
4943 * this User Message fails.
4945 * o stream id - to indicate which stream to send the data on. If not
4946 * specified, stream 0 will be used.
4948 * o life time - specifies the life time of the user data. The user data
4949 * will not be sent by SCTP after the life time expires. This
4950 * parameter can be used to avoid efforts to transmit stale
4951 * user messages. SCTP notifies the ULP if the data cannot be
4952 * initiated to transport (i.e. sent to the destination via SCTP's
4953 * send primitive) within the life time variable. However, the
4954 * user data will be transmitted if SCTP has attempted to transmit a
4955 * chunk before the life time expired.
4957 * o destination transport address - specified as one of the destination
4958 * transport addresses of the peer endpoint to which this packet
4959 * should be sent. Whenever possible, SCTP should use this destination
4960 * transport address for sending the packets, instead of the current
4961 * primary path.
4963 * o unorder flag - this flag, if present, indicates that the user
4964 * would like the data delivered in an unordered fashion to the peer
4965 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4966 * message).
4968 * o no-bundle flag - instructs SCTP not to bundle this user data with
4969 * other outbound DATA chunks. SCTP MAY still bundle even when
4970 * this flag is present, when faced with network congestion.
4972 * o payload protocol-id - A 32 bit unsigned integer that is to be
4973 * passed to the peer indicating the type of payload protocol data
4974 * being transmitted. This value is passed as opaque data by SCTP.
4976 * The return value is the disposition.
4978 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4979 const struct sctp_endpoint *ep,
4980 const struct sctp_association *asoc,
4981 const union sctp_subtype type,
4982 void *arg,
4983 struct sctp_cmd_seq *commands)
4985 struct sctp_datamsg *msg = arg;
4987 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4988 return SCTP_DISPOSITION_CONSUME;
4992 * Process the SHUTDOWN primitive.
4994 * Section: 10.1:
4995 * C) Shutdown
4997 * Format: SHUTDOWN(association id)
4998 * -> result
5000 * Gracefully closes an association. Any locally queued user data
5001 * will be delivered to the peer. The association will be terminated only
5002 * after the peer acknowledges all the SCTP packets sent. A success code
5003 * will be returned on successful termination of the association. If
5004 * attempting to terminate the association results in a failure, an error
5005 * code shall be returned.
5007 * Mandatory attributes:
5009 * o association id - local handle to the SCTP association
5011 * Optional attributes:
5013 * None.
5015 * The return value is the disposition.
5017 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
5018 struct net *net,
5019 const struct sctp_endpoint *ep,
5020 const struct sctp_association *asoc,
5021 const union sctp_subtype type,
5022 void *arg,
5023 struct sctp_cmd_seq *commands)
5025 enum sctp_disposition disposition;
5027 /* From 9.2 Shutdown of an Association
5028 * Upon receipt of the SHUTDOWN primitive from its upper
5029 * layer, the endpoint enters SHUTDOWN-PENDING state and
5030 * remains there until all outstanding data has been
5031 * acknowledged by its peer. The endpoint accepts no new data
5032 * from its upper layer, but retransmits data to the far end
5033 * if necessary to fill gaps.
5035 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5036 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5038 disposition = SCTP_DISPOSITION_CONSUME;
5039 if (sctp_outq_is_empty(&asoc->outqueue)) {
5040 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5041 arg, commands);
5044 return disposition;
5048 * Process the ABORT primitive.
5050 * Section: 10.1:
5051 * C) Abort
5053 * Format: Abort(association id [, cause code])
5054 * -> result
5056 * Ungracefully closes an association. Any locally queued user data
5057 * will be discarded and an ABORT chunk is sent to the peer. A success code
5058 * will be returned on successful abortion of the association. If
5059 * attempting to abort the association results in a failure, an error
5060 * code shall be returned.
5062 * Mandatory attributes:
5064 * o association id - local handle to the SCTP association
5066 * Optional attributes:
5068 * o cause code - reason of the abort to be passed to the peer
5070 * None.
5072 * The return value is the disposition.
5074 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5075 struct net *net,
5076 const struct sctp_endpoint *ep,
5077 const struct sctp_association *asoc,
5078 const union sctp_subtype type,
5079 void *arg,
5080 struct sctp_cmd_seq *commands)
5082 /* From 9.1 Abort of an Association
5083 * Upon receipt of the ABORT primitive from its upper
5084 * layer, the endpoint enters CLOSED state and
5085 * discard all outstanding data has been
5086 * acknowledged by its peer. The endpoint accepts no new data
5087 * from its upper layer, but retransmits data to the far end
5088 * if necessary to fill gaps.
5090 struct sctp_chunk *abort = arg;
5092 if (abort)
5093 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5095 /* Even if we can't send the ABORT due to low memory delete the
5096 * TCB. This is a departure from our typical NOMEM handling.
5099 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5100 SCTP_ERROR(ECONNABORTED));
5101 /* Delete the established association. */
5102 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5103 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5105 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5106 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5108 return SCTP_DISPOSITION_ABORT;
5111 /* We tried an illegal operation on an association which is closed. */
5112 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5113 const struct sctp_endpoint *ep,
5114 const struct sctp_association *asoc,
5115 const union sctp_subtype type,
5116 void *arg,
5117 struct sctp_cmd_seq *commands)
5119 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5120 return SCTP_DISPOSITION_CONSUME;
5123 /* We tried an illegal operation on an association which is shutting
5124 * down.
5126 enum sctp_disposition sctp_sf_error_shutdown(
5127 struct net *net,
5128 const struct sctp_endpoint *ep,
5129 const struct sctp_association *asoc,
5130 const union sctp_subtype type,
5131 void *arg,
5132 struct sctp_cmd_seq *commands)
5134 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5135 SCTP_ERROR(-ESHUTDOWN));
5136 return SCTP_DISPOSITION_CONSUME;
5140 * sctp_cookie_wait_prm_shutdown
5142 * Section: 4 Note: 2
5143 * Verification Tag:
5144 * Inputs
5145 * (endpoint, asoc)
5147 * The RFC does not explicitly address this issue, but is the route through the
5148 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5150 * Outputs
5151 * (timers)
5153 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5154 struct net *net,
5155 const struct sctp_endpoint *ep,
5156 const struct sctp_association *asoc,
5157 const union sctp_subtype type,
5158 void *arg,
5159 struct sctp_cmd_seq *commands)
5161 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5162 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5164 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5165 SCTP_STATE(SCTP_STATE_CLOSED));
5167 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5169 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5171 return SCTP_DISPOSITION_DELETE_TCB;
5175 * sctp_cookie_echoed_prm_shutdown
5177 * Section: 4 Note: 2
5178 * Verification Tag:
5179 * Inputs
5180 * (endpoint, asoc)
5182 * The RFC does not explcitly address this issue, but is the route through the
5183 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5185 * Outputs
5186 * (timers)
5188 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5189 struct net *net,
5190 const struct sctp_endpoint *ep,
5191 const struct sctp_association *asoc,
5192 const union sctp_subtype type,
5193 void *arg,
5194 struct sctp_cmd_seq *commands)
5196 /* There is a single T1 timer, so we should be able to use
5197 * common function with the COOKIE-WAIT state.
5199 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5203 * sctp_sf_cookie_wait_prm_abort
5205 * Section: 4 Note: 2
5206 * Verification Tag:
5207 * Inputs
5208 * (endpoint, asoc)
5210 * The RFC does not explicitly address this issue, but is the route through the
5211 * state table when someone issues an abort while in COOKIE_WAIT state.
5213 * Outputs
5214 * (timers)
5216 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5217 struct net *net,
5218 const struct sctp_endpoint *ep,
5219 const struct sctp_association *asoc,
5220 const union sctp_subtype type,
5221 void *arg,
5222 struct sctp_cmd_seq *commands)
5224 struct sctp_chunk *abort = arg;
5226 /* Stop T1-init timer */
5227 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5228 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5230 if (abort)
5231 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5233 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5234 SCTP_STATE(SCTP_STATE_CLOSED));
5236 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5238 /* Even if we can't send the ABORT due to low memory delete the
5239 * TCB. This is a departure from our typical NOMEM handling.
5242 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5243 SCTP_ERROR(ECONNREFUSED));
5244 /* Delete the established association. */
5245 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5246 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5248 return SCTP_DISPOSITION_ABORT;
5252 * sctp_sf_cookie_echoed_prm_abort
5254 * Section: 4 Note: 3
5255 * Verification Tag:
5256 * Inputs
5257 * (endpoint, asoc)
5259 * The RFC does not explcitly address this issue, but is the route through the
5260 * state table when someone issues an abort while in COOKIE_ECHOED state.
5262 * Outputs
5263 * (timers)
5265 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5266 struct net *net,
5267 const struct sctp_endpoint *ep,
5268 const struct sctp_association *asoc,
5269 const union sctp_subtype type,
5270 void *arg,
5271 struct sctp_cmd_seq *commands)
5273 /* There is a single T1 timer, so we should be able to use
5274 * common function with the COOKIE-WAIT state.
5276 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5280 * sctp_sf_shutdown_pending_prm_abort
5282 * Inputs
5283 * (endpoint, asoc)
5285 * The RFC does not explicitly address this issue, but is the route through the
5286 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5288 * Outputs
5289 * (timers)
5291 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5292 struct net *net,
5293 const struct sctp_endpoint *ep,
5294 const struct sctp_association *asoc,
5295 const union sctp_subtype type,
5296 void *arg,
5297 struct sctp_cmd_seq *commands)
5299 /* Stop the T5-shutdown guard timer. */
5300 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5301 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5303 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5307 * sctp_sf_shutdown_sent_prm_abort
5309 * Inputs
5310 * (endpoint, asoc)
5312 * The RFC does not explicitly address this issue, but is the route through the
5313 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5315 * Outputs
5316 * (timers)
5318 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5319 struct net *net,
5320 const struct sctp_endpoint *ep,
5321 const struct sctp_association *asoc,
5322 const union sctp_subtype type,
5323 void *arg,
5324 struct sctp_cmd_seq *commands)
5326 /* Stop the T2-shutdown timer. */
5327 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5328 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5330 /* Stop the T5-shutdown guard timer. */
5331 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5332 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5334 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5338 * sctp_sf_cookie_echoed_prm_abort
5340 * Inputs
5341 * (endpoint, asoc)
5343 * The RFC does not explcitly address this issue, but is the route through the
5344 * state table when someone issues an abort while in COOKIE_ECHOED state.
5346 * Outputs
5347 * (timers)
5349 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5350 struct net *net,
5351 const struct sctp_endpoint *ep,
5352 const struct sctp_association *asoc,
5353 const union sctp_subtype type,
5354 void *arg,
5355 struct sctp_cmd_seq *commands)
5357 /* The same T2 timer, so we should be able to use
5358 * common function with the SHUTDOWN-SENT state.
5360 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5364 * Process the REQUESTHEARTBEAT primitive
5366 * 10.1 ULP-to-SCTP
5367 * J) Request Heartbeat
5369 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5371 * -> result
5373 * Instructs the local endpoint to perform a HeartBeat on the specified
5374 * destination transport address of the given association. The returned
5375 * result should indicate whether the transmission of the HEARTBEAT
5376 * chunk to the destination address is successful.
5378 * Mandatory attributes:
5380 * o association id - local handle to the SCTP association
5382 * o destination transport address - the transport address of the
5383 * association on which a heartbeat should be issued.
5385 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5386 struct net *net,
5387 const struct sctp_endpoint *ep,
5388 const struct sctp_association *asoc,
5389 const union sctp_subtype type,
5390 void *arg,
5391 struct sctp_cmd_seq *commands)
5393 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5394 (struct sctp_transport *)arg, commands))
5395 return SCTP_DISPOSITION_NOMEM;
5398 * RFC 2960 (bis), section 8.3
5400 * D) Request an on-demand HEARTBEAT on a specific destination
5401 * transport address of a given association.
5403 * The endpoint should increment the respective error counter of
5404 * the destination transport address each time a HEARTBEAT is sent
5405 * to that address and not acknowledged within one RTO.
5408 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5409 SCTP_TRANSPORT(arg));
5410 return SCTP_DISPOSITION_CONSUME;
5414 * ADDIP Section 4.1 ASCONF Chunk Procedures
5415 * When an endpoint has an ASCONF signaled change to be sent to the
5416 * remote endpoint it should do A1 to A9
5418 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5419 const struct sctp_endpoint *ep,
5420 const struct sctp_association *asoc,
5421 const union sctp_subtype type,
5422 void *arg,
5423 struct sctp_cmd_seq *commands)
5425 struct sctp_chunk *chunk = arg;
5427 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5428 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5429 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5430 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5431 return SCTP_DISPOSITION_CONSUME;
5434 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5435 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5436 const struct sctp_endpoint *ep,
5437 const struct sctp_association *asoc,
5438 const union sctp_subtype type,
5439 void *arg,
5440 struct sctp_cmd_seq *commands)
5442 struct sctp_chunk *chunk = arg;
5444 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5445 return SCTP_DISPOSITION_CONSUME;
5449 * Ignore the primitive event
5451 * The return value is the disposition of the primitive.
5453 enum sctp_disposition sctp_sf_ignore_primitive(
5454 struct net *net,
5455 const struct sctp_endpoint *ep,
5456 const struct sctp_association *asoc,
5457 const union sctp_subtype type,
5458 void *arg,
5459 struct sctp_cmd_seq *commands)
5461 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5462 type.primitive);
5464 return SCTP_DISPOSITION_DISCARD;
5467 /***************************************************************************
5468 * These are the state functions for the OTHER events.
5469 ***************************************************************************/
5472 * When the SCTP stack has no more user data to send or retransmit, this
5473 * notification is given to the user. Also, at the time when a user app
5474 * subscribes to this event, if there is no data to be sent or
5475 * retransmit, the stack will immediately send up this notification.
5477 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5478 struct net *net,
5479 const struct sctp_endpoint *ep,
5480 const struct sctp_association *asoc,
5481 const union sctp_subtype type,
5482 void *arg,
5483 struct sctp_cmd_seq *commands)
5485 struct sctp_ulpevent *event;
5487 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5488 if (!event)
5489 return SCTP_DISPOSITION_NOMEM;
5491 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5493 return SCTP_DISPOSITION_CONSUME;
5497 * Start the shutdown negotiation.
5499 * From Section 9.2:
5500 * Once all its outstanding data has been acknowledged, the endpoint
5501 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5502 * TSN Ack field the last sequential TSN it has received from the peer.
5503 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5504 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5505 * with the updated last sequential TSN received from its peer.
5507 * The return value is the disposition.
5509 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5510 struct net *net,
5511 const struct sctp_endpoint *ep,
5512 const struct sctp_association *asoc,
5513 const union sctp_subtype type,
5514 void *arg,
5515 struct sctp_cmd_seq *commands)
5517 struct sctp_chunk *reply;
5519 /* Once all its outstanding data has been acknowledged, the
5520 * endpoint shall send a SHUTDOWN chunk to its peer including
5521 * in the Cumulative TSN Ack field the last sequential TSN it
5522 * has received from the peer.
5524 reply = sctp_make_shutdown(asoc, arg);
5525 if (!reply)
5526 goto nomem;
5528 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5529 * T2-shutdown timer.
5531 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5533 /* It shall then start the T2-shutdown timer */
5534 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5535 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5537 /* RFC 4960 Section 9.2
5538 * The sender of the SHUTDOWN MAY also start an overall guard timer
5539 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5541 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5542 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5544 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5545 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5546 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5548 /* and enter the SHUTDOWN-SENT state. */
5549 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5550 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5552 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5554 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5555 * or SHUTDOWN-ACK.
5557 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5559 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5561 return SCTP_DISPOSITION_CONSUME;
5563 nomem:
5564 return SCTP_DISPOSITION_NOMEM;
5568 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5570 * From Section 9.2:
5572 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5573 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5574 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5575 * endpoint must re-send the SHUTDOWN ACK.
5577 * The return value is the disposition.
5579 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5580 struct net *net,
5581 const struct sctp_endpoint *ep,
5582 const struct sctp_association *asoc,
5583 const union sctp_subtype type,
5584 void *arg,
5585 struct sctp_cmd_seq *commands)
5587 struct sctp_chunk *chunk = arg;
5588 struct sctp_chunk *reply;
5590 /* There are 2 ways of getting here:
5591 * 1) called in response to a SHUTDOWN chunk
5592 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5594 * For the case (2), the arg parameter is set to NULL. We need
5595 * to check that we have a chunk before accessing it's fields.
5597 if (chunk) {
5598 if (!sctp_vtag_verify(chunk, asoc))
5599 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5600 commands);
5602 /* Make sure that the SHUTDOWN chunk has a valid length. */
5603 if (!sctp_chunk_length_valid(
5604 chunk, sizeof(struct sctp_shutdown_chunk)))
5605 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5606 arg, commands);
5609 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5610 * shall send a SHUTDOWN ACK ...
5612 reply = sctp_make_shutdown_ack(asoc, chunk);
5613 if (!reply)
5614 goto nomem;
5616 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5617 * the T2-shutdown timer.
5619 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5621 /* and start/restart a T2-shutdown timer of its own, */
5622 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5623 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5625 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5626 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5627 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5629 /* Enter the SHUTDOWN-ACK-SENT state. */
5630 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5631 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5633 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5635 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5636 * or SHUTDOWN-ACK.
5638 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5640 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5642 return SCTP_DISPOSITION_CONSUME;
5644 nomem:
5645 return SCTP_DISPOSITION_NOMEM;
5649 * Ignore the event defined as other
5651 * The return value is the disposition of the event.
5653 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5654 const struct sctp_endpoint *ep,
5655 const struct sctp_association *asoc,
5656 const union sctp_subtype type,
5657 void *arg,
5658 struct sctp_cmd_seq *commands)
5660 pr_debug("%s: the event other type:%d is ignored\n",
5661 __func__, type.other);
5663 return SCTP_DISPOSITION_DISCARD;
5666 /************************************************************
5667 * These are the state functions for handling timeout events.
5668 ************************************************************/
5671 * RTX Timeout
5673 * Section: 6.3.3 Handle T3-rtx Expiration
5675 * Whenever the retransmission timer T3-rtx expires for a destination
5676 * address, do the following:
5677 * [See below]
5679 * The return value is the disposition of the chunk.
5681 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5682 const struct sctp_endpoint *ep,
5683 const struct sctp_association *asoc,
5684 const union sctp_subtype type,
5685 void *arg,
5686 struct sctp_cmd_seq *commands)
5688 struct sctp_transport *transport = arg;
5690 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5692 if (asoc->overall_error_count >= asoc->max_retrans) {
5693 if (asoc->peer.zero_window_announced &&
5694 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5696 * We are here likely because the receiver had its rwnd
5697 * closed for a while and we have not been able to
5698 * transmit the locally queued data within the maximum
5699 * retransmission attempts limit. Start the T5
5700 * shutdown guard timer to give the receiver one last
5701 * chance and some additional time to recover before
5702 * aborting.
5704 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5705 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5706 } else {
5707 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5708 SCTP_ERROR(ETIMEDOUT));
5709 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5710 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5711 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5712 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5713 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5714 return SCTP_DISPOSITION_DELETE_TCB;
5718 /* E1) For the destination address for which the timer
5719 * expires, adjust its ssthresh with rules defined in Section
5720 * 7.2.3 and set the cwnd <- MTU.
5723 /* E2) For the destination address for which the timer
5724 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5725 * maximum value discussed in rule C7 above (RTO.max) may be
5726 * used to provide an upper bound to this doubling operation.
5729 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5730 * outstanding DATA chunks for the address for which the
5731 * T3-rtx has expired will fit into a single packet, subject
5732 * to the MTU constraint for the path corresponding to the
5733 * destination transport address to which the retransmission
5734 * is being sent (this may be different from the address for
5735 * which the timer expires [see Section 6.4]). Call this
5736 * value K. Bundle and retransmit those K DATA chunks in a
5737 * single packet to the destination endpoint.
5739 * Note: Any DATA chunks that were sent to the address for
5740 * which the T3-rtx timer expired but did not fit in one MTU
5741 * (rule E3 above), should be marked for retransmission and
5742 * sent as soon as cwnd allows (normally when a SACK arrives).
5745 /* Do some failure management (Section 8.2). */
5746 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5748 /* NB: Rules E4 and F1 are implicit in R1. */
5749 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5751 return SCTP_DISPOSITION_CONSUME;
5755 * Generate delayed SACK on timeout
5757 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5759 * The guidelines on delayed acknowledgement algorithm specified in
5760 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5761 * acknowledgement SHOULD be generated for at least every second packet
5762 * (not every second DATA chunk) received, and SHOULD be generated
5763 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5764 * some situations it may be beneficial for an SCTP transmitter to be
5765 * more conservative than the algorithms detailed in this document
5766 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5767 * the following algorithms allow.
5769 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5770 const struct sctp_endpoint *ep,
5771 const struct sctp_association *asoc,
5772 const union sctp_subtype type,
5773 void *arg,
5774 struct sctp_cmd_seq *commands)
5776 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5777 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5778 return SCTP_DISPOSITION_CONSUME;
5782 * sctp_sf_t1_init_timer_expire
5784 * Section: 4 Note: 2
5785 * Verification Tag:
5786 * Inputs
5787 * (endpoint, asoc)
5789 * RFC 2960 Section 4 Notes
5790 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5791 * and re-start the T1-init timer without changing state. This MUST
5792 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5793 * endpoint MUST abort the initialization process and report the
5794 * error to SCTP user.
5796 * Outputs
5797 * (timers, events)
5800 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5801 struct net *net,
5802 const struct sctp_endpoint *ep,
5803 const struct sctp_association *asoc,
5804 const union sctp_subtype type,
5805 void *arg,
5806 struct sctp_cmd_seq *commands)
5808 int attempts = asoc->init_err_counter + 1;
5809 struct sctp_chunk *repl = NULL;
5810 struct sctp_bind_addr *bp;
5812 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5814 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5816 if (attempts <= asoc->max_init_attempts) {
5817 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5818 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5819 if (!repl)
5820 return SCTP_DISPOSITION_NOMEM;
5822 /* Choose transport for INIT. */
5823 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5824 SCTP_CHUNK(repl));
5826 /* Issue a sideeffect to do the needed accounting. */
5827 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5828 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5831 } else {
5832 pr_debug("%s: giving up on INIT, attempts:%d "
5833 "max_init_attempts:%d\n", __func__, attempts,
5834 asoc->max_init_attempts);
5836 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5837 SCTP_ERROR(ETIMEDOUT));
5838 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5839 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5840 return SCTP_DISPOSITION_DELETE_TCB;
5843 return SCTP_DISPOSITION_CONSUME;
5847 * sctp_sf_t1_cookie_timer_expire
5849 * Section: 4 Note: 2
5850 * Verification Tag:
5851 * Inputs
5852 * (endpoint, asoc)
5854 * RFC 2960 Section 4 Notes
5855 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5856 * COOKIE ECHO and re-start the T1-cookie timer without changing
5857 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5858 * After that, the endpoint MUST abort the initialization process and
5859 * report the error to SCTP user.
5861 * Outputs
5862 * (timers, events)
5865 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5866 struct net *net,
5867 const struct sctp_endpoint *ep,
5868 const struct sctp_association *asoc,
5869 const union sctp_subtype type,
5870 void *arg,
5871 struct sctp_cmd_seq *commands)
5873 int attempts = asoc->init_err_counter + 1;
5874 struct sctp_chunk *repl = NULL;
5876 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5878 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5880 if (attempts <= asoc->max_init_attempts) {
5881 repl = sctp_make_cookie_echo(asoc, NULL);
5882 if (!repl)
5883 return SCTP_DISPOSITION_NOMEM;
5885 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5886 SCTP_CHUNK(repl));
5887 /* Issue a sideeffect to do the needed accounting. */
5888 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5889 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5891 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5892 } else {
5893 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5894 SCTP_ERROR(ETIMEDOUT));
5895 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5896 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5897 return SCTP_DISPOSITION_DELETE_TCB;
5900 return SCTP_DISPOSITION_CONSUME;
5903 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5904 * with the updated last sequential TSN received from its peer.
5906 * An endpoint should limit the number of retransmissions of the
5907 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5908 * If this threshold is exceeded the endpoint should destroy the TCB and
5909 * MUST report the peer endpoint unreachable to the upper layer (and
5910 * thus the association enters the CLOSED state). The reception of any
5911 * packet from its peer (i.e. as the peer sends all of its queued DATA
5912 * chunks) should clear the endpoint's retransmission count and restart
5913 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5914 * all of its queued DATA chunks that have not yet been sent.
5916 enum sctp_disposition sctp_sf_t2_timer_expire(
5917 struct net *net,
5918 const struct sctp_endpoint *ep,
5919 const struct sctp_association *asoc,
5920 const union sctp_subtype type,
5921 void *arg,
5922 struct sctp_cmd_seq *commands)
5924 struct sctp_chunk *reply = NULL;
5926 pr_debug("%s: timer T2 expired\n", __func__);
5928 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5930 ((struct sctp_association *)asoc)->shutdown_retries++;
5932 if (asoc->overall_error_count >= asoc->max_retrans) {
5933 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5934 SCTP_ERROR(ETIMEDOUT));
5935 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5936 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5937 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5938 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5939 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5940 return SCTP_DISPOSITION_DELETE_TCB;
5943 switch (asoc->state) {
5944 case SCTP_STATE_SHUTDOWN_SENT:
5945 reply = sctp_make_shutdown(asoc, NULL);
5946 break;
5948 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5949 reply = sctp_make_shutdown_ack(asoc, NULL);
5950 break;
5952 default:
5953 BUG();
5954 break;
5957 if (!reply)
5958 goto nomem;
5960 /* Do some failure management (Section 8.2).
5961 * If we remove the transport an SHUTDOWN was last sent to, don't
5962 * do failure management.
5964 if (asoc->shutdown_last_sent_to)
5965 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5966 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5968 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5969 * the T2-shutdown timer.
5971 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5973 /* Restart the T2-shutdown timer. */
5974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5975 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5976 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5977 return SCTP_DISPOSITION_CONSUME;
5979 nomem:
5980 return SCTP_DISPOSITION_NOMEM;
5984 * ADDIP Section 4.1 ASCONF CHunk Procedures
5985 * If the T4 RTO timer expires the endpoint should do B1 to B5
5987 enum sctp_disposition sctp_sf_t4_timer_expire(
5988 struct net *net,
5989 const struct sctp_endpoint *ep,
5990 const struct sctp_association *asoc,
5991 const union sctp_subtype type,
5992 void *arg,
5993 struct sctp_cmd_seq *commands)
5995 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5996 struct sctp_transport *transport = chunk->transport;
5998 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
6000 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
6001 * detection on the appropriate destination address as defined in
6002 * RFC2960 [5] section 8.1 and 8.2.
6004 if (transport)
6005 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6006 SCTP_TRANSPORT(transport));
6008 /* Reconfig T4 timer and transport. */
6009 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
6011 /* ADDIP 4.1 B2) Increment the association error counters and perform
6012 * endpoint failure detection on the association as defined in
6013 * RFC2960 [5] section 8.1 and 8.2.
6014 * association error counter is incremented in SCTP_CMD_STRIKE.
6016 if (asoc->overall_error_count >= asoc->max_retrans) {
6017 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
6018 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6019 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6020 SCTP_ERROR(ETIMEDOUT));
6021 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6022 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6023 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6024 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6025 return SCTP_DISPOSITION_ABORT;
6028 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
6029 * the ASCONF chunk was sent by doubling the RTO timer value.
6030 * This is done in SCTP_CMD_STRIKE.
6033 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
6034 * choose an alternate destination address (please refer to RFC2960
6035 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
6036 * chunk, it MUST be the same (including its serial number) as the last
6037 * ASCONF sent.
6039 sctp_chunk_hold(asoc->addip_last_asconf);
6040 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6041 SCTP_CHUNK(asoc->addip_last_asconf));
6043 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
6044 * destination is selected, then the RTO used will be that of the new
6045 * destination address.
6047 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6048 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6050 return SCTP_DISPOSITION_CONSUME;
6053 /* sctpimpguide-05 Section 2.12.2
6054 * The sender of the SHUTDOWN MAY also start an overall guard timer
6055 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6056 * At the expiration of this timer the sender SHOULD abort the association
6057 * by sending an ABORT chunk.
6059 enum sctp_disposition sctp_sf_t5_timer_expire(
6060 struct net *net,
6061 const struct sctp_endpoint *ep,
6062 const struct sctp_association *asoc,
6063 const union sctp_subtype type,
6064 void *arg,
6065 struct sctp_cmd_seq *commands)
6067 struct sctp_chunk *reply = NULL;
6069 pr_debug("%s: timer T5 expired\n", __func__);
6071 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6073 reply = sctp_make_abort(asoc, NULL, 0);
6074 if (!reply)
6075 goto nomem;
6077 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6078 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6079 SCTP_ERROR(ETIMEDOUT));
6080 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6081 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6083 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6084 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6086 return SCTP_DISPOSITION_DELETE_TCB;
6087 nomem:
6088 return SCTP_DISPOSITION_NOMEM;
6091 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6092 * the association is automatically closed by starting the shutdown process.
6093 * The work that needs to be done is same as when SHUTDOWN is initiated by
6094 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6096 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6097 struct net *net,
6098 const struct sctp_endpoint *ep,
6099 const struct sctp_association *asoc,
6100 const union sctp_subtype type,
6101 void *arg,
6102 struct sctp_cmd_seq *commands)
6104 enum sctp_disposition disposition;
6106 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6108 /* From 9.2 Shutdown of an Association
6109 * Upon receipt of the SHUTDOWN primitive from its upper
6110 * layer, the endpoint enters SHUTDOWN-PENDING state and
6111 * remains there until all outstanding data has been
6112 * acknowledged by its peer. The endpoint accepts no new data
6113 * from its upper layer, but retransmits data to the far end
6114 * if necessary to fill gaps.
6116 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6117 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6119 disposition = SCTP_DISPOSITION_CONSUME;
6120 if (sctp_outq_is_empty(&asoc->outqueue)) {
6121 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6122 NULL, commands);
6125 return disposition;
6128 /*****************************************************************************
6129 * These are sa state functions which could apply to all types of events.
6130 ****************************************************************************/
6133 * This table entry is not implemented.
6135 * Inputs
6136 * (endpoint, asoc, chunk)
6138 * The return value is the disposition of the chunk.
6140 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6141 const struct sctp_endpoint *ep,
6142 const struct sctp_association *asoc,
6143 const union sctp_subtype type,
6144 void *arg, struct sctp_cmd_seq *commands)
6146 return SCTP_DISPOSITION_NOT_IMPL;
6150 * This table entry represents a bug.
6152 * Inputs
6153 * (endpoint, asoc, chunk)
6155 * The return value is the disposition of the chunk.
6157 enum sctp_disposition sctp_sf_bug(struct net *net,
6158 const struct sctp_endpoint *ep,
6159 const struct sctp_association *asoc,
6160 const union sctp_subtype type,
6161 void *arg, struct sctp_cmd_seq *commands)
6163 return SCTP_DISPOSITION_BUG;
6167 * This table entry represents the firing of a timer in the wrong state.
6168 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6169 * when the association is in the wrong state. This event should
6170 * be ignored, so as to prevent any rearming of the timer.
6172 * Inputs
6173 * (endpoint, asoc, chunk)
6175 * The return value is the disposition of the chunk.
6177 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6178 const struct sctp_endpoint *ep,
6179 const struct sctp_association *asoc,
6180 const union sctp_subtype type,
6181 void *arg,
6182 struct sctp_cmd_seq *commands)
6184 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6186 return SCTP_DISPOSITION_CONSUME;
6189 /********************************************************************
6190 * 2nd Level Abstractions
6191 ********************************************************************/
6193 /* Pull the SACK chunk based on the SACK header. */
6194 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6196 struct sctp_sackhdr *sack;
6197 __u16 num_dup_tsns;
6198 unsigned int len;
6199 __u16 num_blocks;
6201 /* Protect ourselves from reading too far into
6202 * the skb from a bogus sender.
6204 sack = (struct sctp_sackhdr *) chunk->skb->data;
6206 num_blocks = ntohs(sack->num_gap_ack_blocks);
6207 num_dup_tsns = ntohs(sack->num_dup_tsns);
6208 len = sizeof(struct sctp_sackhdr);
6209 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6210 if (len > chunk->skb->len)
6211 return NULL;
6213 skb_pull(chunk->skb, len);
6215 return sack;
6218 /* Create an ABORT packet to be sent as a response, with the specified
6219 * error causes.
6221 static struct sctp_packet *sctp_abort_pkt_new(
6222 struct net *net,
6223 const struct sctp_endpoint *ep,
6224 const struct sctp_association *asoc,
6225 struct sctp_chunk *chunk,
6226 const void *payload, size_t paylen)
6228 struct sctp_packet *packet;
6229 struct sctp_chunk *abort;
6231 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6233 if (packet) {
6234 /* Make an ABORT.
6235 * The T bit will be set if the asoc is NULL.
6237 abort = sctp_make_abort(asoc, chunk, paylen);
6238 if (!abort) {
6239 sctp_ootb_pkt_free(packet);
6240 return NULL;
6243 /* Reflect vtag if T-Bit is set */
6244 if (sctp_test_T_bit(abort))
6245 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6247 /* Add specified error causes, i.e., payload, to the
6248 * end of the chunk.
6250 sctp_addto_chunk(abort, paylen, payload);
6252 /* Set the skb to the belonging sock for accounting. */
6253 abort->skb->sk = ep->base.sk;
6255 sctp_packet_append_chunk(packet, abort);
6259 return packet;
6262 /* Allocate a packet for responding in the OOTB conditions. */
6263 static struct sctp_packet *sctp_ootb_pkt_new(
6264 struct net *net,
6265 const struct sctp_association *asoc,
6266 const struct sctp_chunk *chunk)
6268 struct sctp_transport *transport;
6269 struct sctp_packet *packet;
6270 __u16 sport, dport;
6271 __u32 vtag;
6273 /* Get the source and destination port from the inbound packet. */
6274 sport = ntohs(chunk->sctp_hdr->dest);
6275 dport = ntohs(chunk->sctp_hdr->source);
6277 /* The V-tag is going to be the same as the inbound packet if no
6278 * association exists, otherwise, use the peer's vtag.
6280 if (asoc) {
6281 /* Special case the INIT-ACK as there is no peer's vtag
6282 * yet.
6284 switch (chunk->chunk_hdr->type) {
6285 case SCTP_CID_INIT_ACK:
6287 struct sctp_initack_chunk *initack;
6289 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6290 vtag = ntohl(initack->init_hdr.init_tag);
6291 break;
6293 default:
6294 vtag = asoc->peer.i.init_tag;
6295 break;
6297 } else {
6298 /* Special case the INIT and stale COOKIE_ECHO as there is no
6299 * vtag yet.
6301 switch (chunk->chunk_hdr->type) {
6302 case SCTP_CID_INIT:
6304 struct sctp_init_chunk *init;
6306 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6307 vtag = ntohl(init->init_hdr.init_tag);
6308 break;
6310 default:
6311 vtag = ntohl(chunk->sctp_hdr->vtag);
6312 break;
6316 /* Make a transport for the bucket, Eliza... */
6317 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6318 if (!transport)
6319 goto nomem;
6321 transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
6323 /* Cache a route for the transport with the chunk's destination as
6324 * the source address.
6326 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6327 sctp_sk(net->sctp.ctl_sock));
6329 packet = &transport->packet;
6330 sctp_packet_init(packet, transport, sport, dport);
6331 sctp_packet_config(packet, vtag, 0);
6333 return packet;
6335 nomem:
6336 return NULL;
6339 /* Free the packet allocated earlier for responding in the OOTB condition. */
6340 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6342 sctp_transport_free(packet->transport);
6345 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6346 static void sctp_send_stale_cookie_err(struct net *net,
6347 const struct sctp_endpoint *ep,
6348 const struct sctp_association *asoc,
6349 const struct sctp_chunk *chunk,
6350 struct sctp_cmd_seq *commands,
6351 struct sctp_chunk *err_chunk)
6353 struct sctp_packet *packet;
6355 if (err_chunk) {
6356 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6357 if (packet) {
6358 struct sctp_signed_cookie *cookie;
6360 /* Override the OOTB vtag from the cookie. */
6361 cookie = chunk->subh.cookie_hdr;
6362 packet->vtag = cookie->c.peer_vtag;
6364 /* Set the skb to the belonging sock for accounting. */
6365 err_chunk->skb->sk = ep->base.sk;
6366 sctp_packet_append_chunk(packet, err_chunk);
6367 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6368 SCTP_PACKET(packet));
6369 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6370 } else
6371 sctp_chunk_free (err_chunk);
6376 /* Process a data chunk */
6377 static int sctp_eat_data(const struct sctp_association *asoc,
6378 struct sctp_chunk *chunk,
6379 struct sctp_cmd_seq *commands)
6381 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6382 struct sock *sk = asoc->base.sk;
6383 struct net *net = sock_net(sk);
6384 struct sctp_datahdr *data_hdr;
6385 struct sctp_chunk *err;
6386 enum sctp_verb deliver;
6387 size_t datalen;
6388 __u32 tsn;
6389 int tmp;
6391 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6392 chunk->subh.data_hdr = data_hdr;
6393 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6395 tsn = ntohl(data_hdr->tsn);
6396 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6398 /* ASSERT: Now skb->data is really the user data. */
6400 /* Process ECN based congestion.
6402 * Since the chunk structure is reused for all chunks within
6403 * a packet, we use ecn_ce_done to track if we've already
6404 * done CE processing for this packet.
6406 * We need to do ECN processing even if we plan to discard the
6407 * chunk later.
6410 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6411 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6412 chunk->ecn_ce_done = 1;
6414 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6415 /* Do real work as sideffect. */
6416 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6417 SCTP_U32(tsn));
6421 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6422 if (tmp < 0) {
6423 /* The TSN is too high--silently discard the chunk and
6424 * count on it getting retransmitted later.
6426 if (chunk->asoc)
6427 chunk->asoc->stats.outofseqtsns++;
6428 return SCTP_IERROR_HIGH_TSN;
6429 } else if (tmp > 0) {
6430 /* This is a duplicate. Record it. */
6431 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6432 return SCTP_IERROR_DUP_TSN;
6435 /* This is a new TSN. */
6437 /* Discard if there is no room in the receive window.
6438 * Actually, allow a little bit of overflow (up to a MTU).
6440 datalen = ntohs(chunk->chunk_hdr->length);
6441 datalen -= sctp_datachk_len(&asoc->stream);
6443 deliver = SCTP_CMD_CHUNK_ULP;
6445 /* Think about partial delivery. */
6446 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6448 /* Even if we don't accept this chunk there is
6449 * memory pressure.
6451 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6454 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6455 * seems a bit troublesome in that frag_point varies based on
6456 * PMTU. In cases, such as loopback, this might be a rather
6457 * large spill over.
6459 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6460 (datalen > asoc->rwnd + asoc->frag_point))) {
6462 /* If this is the next TSN, consider reneging to make
6463 * room. Note: Playing nice with a confused sender. A
6464 * malicious sender can still eat up all our buffer
6465 * space and in the future we may want to detect and
6466 * do more drastic reneging.
6468 if (sctp_tsnmap_has_gap(map) &&
6469 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6470 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6471 deliver = SCTP_CMD_RENEGE;
6472 } else {
6473 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6474 __func__, tsn, datalen, asoc->rwnd);
6476 return SCTP_IERROR_IGNORE_TSN;
6481 * Also try to renege to limit our memory usage in the event that
6482 * we are under memory pressure
6483 * If we can't renege, don't worry about it, the sk_rmem_schedule
6484 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6485 * memory usage too much
6487 if (sk_under_memory_pressure(sk)) {
6488 if (sctp_tsnmap_has_gap(map) &&
6489 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6490 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6491 __func__, tsn);
6492 deliver = SCTP_CMD_RENEGE;
6493 } else {
6494 sk_mem_reclaim(sk);
6499 * Section 3.3.10.9 No User Data (9)
6501 * Cause of error
6502 * ---------------
6503 * No User Data: This error cause is returned to the originator of a
6504 * DATA chunk if a received DATA chunk has no user data.
6506 if (unlikely(0 == datalen)) {
6507 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6508 if (err) {
6509 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6510 SCTP_CHUNK(err));
6512 /* We are going to ABORT, so we might as well stop
6513 * processing the rest of the chunks in the packet.
6515 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6516 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6517 SCTP_ERROR(ECONNABORTED));
6518 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6519 SCTP_PERR(SCTP_ERROR_NO_DATA));
6520 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6521 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6522 return SCTP_IERROR_NO_DATA;
6525 chunk->data_accepted = 1;
6527 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6528 * if we renege and the chunk arrives again.
6530 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6531 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6532 if (chunk->asoc)
6533 chunk->asoc->stats.iuodchunks++;
6534 } else {
6535 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6536 if (chunk->asoc)
6537 chunk->asoc->stats.iodchunks++;
6540 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6542 * If an endpoint receive a DATA chunk with an invalid stream
6543 * identifier, it shall acknowledge the reception of the DATA chunk
6544 * following the normal procedure, immediately send an ERROR chunk
6545 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6546 * and discard the DATA chunk.
6548 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6549 /* Mark tsn as received even though we drop it */
6550 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6552 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6553 &data_hdr->stream,
6554 sizeof(data_hdr->stream),
6555 sizeof(u16));
6556 if (err)
6557 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6558 SCTP_CHUNK(err));
6559 return SCTP_IERROR_BAD_STREAM;
6562 /* Check to see if the SSN is possible for this TSN.
6563 * The biggest gap we can record is 4K wide. Since SSNs wrap
6564 * at an unsigned short, there is no way that an SSN can
6565 * wrap and for a valid TSN. We can simply check if the current
6566 * SSN is smaller then the next expected one. If it is, it wrapped
6567 * and is invalid.
6569 if (!asoc->stream.si->validate_data(chunk))
6570 return SCTP_IERROR_PROTO_VIOLATION;
6572 /* Send the data up to the user. Note: Schedule the
6573 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6574 * chunk needs the updated rwnd.
6576 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6578 return SCTP_IERROR_NO_ERROR;