ALSA: caiaq - Fix possible string-buffer overflow
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / sm_statefuns.c
blobc3f75e79bedc3760e3dc56ed283dde6e5e00dea6
1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
8 * This is part of the SCTP Linux Kernel Implementation.
10 * These are the state functions for the state machine.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #include <linux/types.h>
54 #include <linux/kernel.h>
55 #include <linux/ip.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <net/sock.h>
60 #include <net/inet_ecn.h>
61 #include <linux/skbuff.h>
62 #include <net/sctp/sctp.h>
63 #include <net/sctp/sm.h>
64 #include <net/sctp/structs.h>
66 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
67 const struct sctp_association *asoc,
68 struct sctp_chunk *chunk,
69 const void *payload,
70 size_t paylen);
71 static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 sctp_cmd_seq_t *commands);
74 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75 const struct sctp_chunk *chunk);
76 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 sctp_cmd_seq_t *commands,
80 struct sctp_chunk *err_chunk);
81 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
86 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const sctp_subtype_t type,
89 void *arg,
90 sctp_cmd_seq_t *commands);
91 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
92 const struct sctp_association *asoc,
93 const sctp_subtype_t type,
94 void *arg,
95 sctp_cmd_seq_t *commands);
96 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
98 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
99 __be16 error, int sk_err,
100 const struct sctp_association *asoc,
101 struct sctp_transport *transport);
103 static sctp_disposition_t sctp_sf_abort_violation(
104 const struct sctp_endpoint *ep,
105 const struct sctp_association *asoc,
106 void *arg,
107 sctp_cmd_seq_t *commands,
108 const __u8 *payload,
109 const size_t paylen);
111 static sctp_disposition_t sctp_sf_violation_chunklen(
112 const struct sctp_endpoint *ep,
113 const struct sctp_association *asoc,
114 const sctp_subtype_t type,
115 void *arg,
116 sctp_cmd_seq_t *commands);
118 static sctp_disposition_t sctp_sf_violation_paramlen(
119 const struct sctp_endpoint *ep,
120 const struct sctp_association *asoc,
121 const sctp_subtype_t type,
122 void *arg, void *ext,
123 sctp_cmd_seq_t *commands);
125 static sctp_disposition_t sctp_sf_violation_ctsn(
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const sctp_subtype_t type,
129 void *arg,
130 sctp_cmd_seq_t *commands);
132 static sctp_disposition_t sctp_sf_violation_chunk(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
139 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
140 const struct sctp_association *asoc,
141 const sctp_subtype_t type,
142 struct sctp_chunk *chunk);
144 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
145 const struct sctp_association *asoc,
146 const sctp_subtype_t type,
147 void *arg,
148 sctp_cmd_seq_t *commands);
150 /* Small helper function that checks if the chunk length
151 * is of the appropriate length. The 'required_length' argument
152 * is set to be the size of a specific chunk we are testing.
153 * Return Values: 1 = Valid length
154 * 0 = Invalid length
157 static inline int
158 sctp_chunk_length_valid(struct sctp_chunk *chunk,
159 __u16 required_length)
161 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
163 if (unlikely(chunk_length < required_length))
164 return 0;
166 return 1;
169 /**********************************************************
170 * These are the state functions for handling chunk events.
171 **********************************************************/
174 * Process the final SHUTDOWN COMPLETE.
176 * Section: 4 (C) (diagram), 9.2
177 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
178 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
179 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
180 * should stop the T2-shutdown timer and remove all knowledge of the
181 * association (and thus the association enters the CLOSED state).
183 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
184 * C) Rules for packet carrying SHUTDOWN COMPLETE:
185 * ...
186 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
187 * if the Verification Tag field of the packet matches its own tag and
188 * the T bit is not set
189 * OR
190 * it is set to its peer's tag and the T bit is set in the Chunk
191 * Flags.
192 * Otherwise, the receiver MUST silently discard the packet
193 * and take no further action. An endpoint MUST ignore the
194 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
196 * Inputs
197 * (endpoint, asoc, chunk)
199 * Outputs
200 * (asoc, reply_msg, msg_up, timers, counters)
202 * The return value is the disposition of the chunk.
204 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
205 const struct sctp_association *asoc,
206 const sctp_subtype_t type,
207 void *arg,
208 sctp_cmd_seq_t *commands)
210 struct sctp_chunk *chunk = arg;
211 struct sctp_ulpevent *ev;
213 if (!sctp_vtag_verify_either(chunk, asoc))
214 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
216 /* RFC 2960 6.10 Bundling
218 * An endpoint MUST NOT bundle INIT, INIT ACK or
219 * SHUTDOWN COMPLETE with any other chunks.
221 if (!chunk->singleton)
222 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
224 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
225 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
226 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
227 commands);
229 /* RFC 2960 10.2 SCTP-to-ULP
231 * H) SHUTDOWN COMPLETE notification
233 * When SCTP completes the shutdown procedures (section 9.2) this
234 * notification is passed to the upper layer.
236 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
237 0, 0, 0, NULL, GFP_ATOMIC);
238 if (ev)
239 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
240 SCTP_ULPEVENT(ev));
242 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
243 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
244 * not the chunk should be discarded. If the endpoint is in
245 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
246 * T2-shutdown timer and remove all knowledge of the
247 * association (and thus the association enters the CLOSED
248 * state).
250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
256 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257 SCTP_STATE(SCTP_STATE_CLOSED));
259 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
262 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
264 return SCTP_DISPOSITION_DELETE_TCB;
268 * Respond to a normal INIT chunk.
269 * We are the side that is being asked for an association.
271 * Section: 5.1 Normal Establishment of an Association, B
272 * B) "Z" shall respond immediately with an INIT ACK chunk. The
273 * destination IP address of the INIT ACK MUST be set to the source
274 * IP address of the INIT to which this INIT ACK is responding. In
275 * the response, besides filling in other parameters, "Z" must set the
276 * Verification Tag field to Tag_A, and also provide its own
277 * Verification Tag (Tag_Z) in the Initiate Tag field.
279 * Verification Tag: Must be 0.
281 * Inputs
282 * (endpoint, asoc, chunk)
284 * Outputs
285 * (asoc, reply_msg, msg_up, timers, counters)
287 * The return value is the disposition of the chunk.
289 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
290 const struct sctp_association *asoc,
291 const sctp_subtype_t type,
292 void *arg,
293 sctp_cmd_seq_t *commands)
295 struct sctp_chunk *chunk = arg;
296 struct sctp_chunk *repl;
297 struct sctp_association *new_asoc;
298 struct sctp_chunk *err_chunk;
299 struct sctp_packet *packet;
300 sctp_unrecognized_param_t *unk_param;
301 int len;
303 /* 6.10 Bundling
304 * An endpoint MUST NOT bundle INIT, INIT ACK or
305 * SHUTDOWN COMPLETE with any other chunks.
307 * IG Section 2.11.2
308 * Furthermore, we require that the receiver of an INIT chunk MUST
309 * enforce these rules by silently discarding an arriving packet
310 * with an INIT chunk that is bundled with other chunks.
312 if (!chunk->singleton)
313 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
315 /* If the packet is an OOTB packet which is temporarily on the
316 * control endpoint, respond with an ABORT.
318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
320 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
323 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
324 * Tag.
326 if (chunk->sctp_hdr->vtag != 0)
327 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
329 /* Make sure that the INIT chunk has a valid length.
330 * Normally, this would cause an ABORT with a Protocol Violation
331 * error, but since we don't have an association, we'll
332 * just discard the packet.
334 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
335 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
337 /* If the INIT is coming toward a closing socket, we'll send back
338 * and ABORT. Essentially, this catches the race of INIT being
339 * backloged to the socket at the same time as the user isses close().
340 * Since the socket and all its associations are going away, we
341 * can treat this OOTB
343 if (sctp_sstate(ep->base.sk, CLOSING))
344 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
346 /* Verify the INIT chunk before processing it. */
347 err_chunk = NULL;
348 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
349 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
350 &err_chunk)) {
351 /* This chunk contains fatal error. It is to be discarded.
352 * Send an ABORT, with causes if there is any.
354 if (err_chunk) {
355 packet = sctp_abort_pkt_new(ep, asoc, arg,
356 (__u8 *)(err_chunk->chunk_hdr) +
357 sizeof(sctp_chunkhdr_t),
358 ntohs(err_chunk->chunk_hdr->length) -
359 sizeof(sctp_chunkhdr_t));
361 sctp_chunk_free(err_chunk);
363 if (packet) {
364 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
365 SCTP_PACKET(packet));
366 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
367 return SCTP_DISPOSITION_CONSUME;
368 } else {
369 return SCTP_DISPOSITION_NOMEM;
371 } else {
372 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
373 commands);
377 /* Grab the INIT header. */
378 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
380 /* Tag the variable length parameters. */
381 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
383 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
384 if (!new_asoc)
385 goto nomem;
387 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
388 sctp_scope(sctp_source(chunk)),
389 GFP_ATOMIC) < 0)
390 goto nomem_init;
392 /* The call, sctp_process_init(), can fail on memory allocation. */
393 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
394 sctp_source(chunk),
395 (sctp_init_chunk_t *)chunk->chunk_hdr,
396 GFP_ATOMIC))
397 goto nomem_init;
399 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
401 /* If there are errors need to be reported for unknown parameters,
402 * make sure to reserve enough room in the INIT ACK for them.
404 len = 0;
405 if (err_chunk)
406 len = ntohs(err_chunk->chunk_hdr->length) -
407 sizeof(sctp_chunkhdr_t);
409 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
410 if (!repl)
411 goto nomem_init;
413 /* If there are errors need to be reported for unknown parameters,
414 * include them in the outgoing INIT ACK as "Unrecognized parameter"
415 * parameter.
417 if (err_chunk) {
418 /* Get the "Unrecognized parameter" parameter(s) out of the
419 * ERROR chunk generated by sctp_verify_init(). Since the
420 * error cause code for "unknown parameter" and the
421 * "Unrecognized parameter" type is the same, we can
422 * construct the parameters in INIT ACK by copying the
423 * ERROR causes over.
425 unk_param = (sctp_unrecognized_param_t *)
426 ((__u8 *)(err_chunk->chunk_hdr) +
427 sizeof(sctp_chunkhdr_t));
428 /* Replace the cause code with the "Unrecognized parameter"
429 * parameter type.
431 sctp_addto_chunk(repl, len, unk_param);
432 sctp_chunk_free(err_chunk);
435 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
437 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
440 * Note: After sending out INIT ACK with the State Cookie parameter,
441 * "Z" MUST NOT allocate any resources, nor keep any states for the
442 * new association. Otherwise, "Z" will be vulnerable to resource
443 * attacks.
445 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
447 return SCTP_DISPOSITION_DELETE_TCB;
449 nomem_init:
450 sctp_association_free(new_asoc);
451 nomem:
452 if (err_chunk)
453 sctp_chunk_free(err_chunk);
454 return SCTP_DISPOSITION_NOMEM;
458 * Respond to a normal INIT ACK chunk.
459 * We are the side that is initiating the association.
461 * Section: 5.1 Normal Establishment of an Association, C
462 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
463 * timer and leave COOKIE-WAIT state. "A" shall then send the State
464 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
465 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
467 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
468 * DATA chunks, but it MUST be the first chunk in the packet and
469 * until the COOKIE ACK is returned the sender MUST NOT send any
470 * other packets to the peer.
472 * Verification Tag: 3.3.3
473 * If the value of the Initiate Tag in a received INIT ACK chunk is
474 * found to be 0, the receiver MUST treat it as an error and close the
475 * association by transmitting an ABORT.
477 * Inputs
478 * (endpoint, asoc, chunk)
480 * Outputs
481 * (asoc, reply_msg, msg_up, timers, counters)
483 * The return value is the disposition of the chunk.
485 sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
486 const struct sctp_association *asoc,
487 const sctp_subtype_t type,
488 void *arg,
489 sctp_cmd_seq_t *commands)
491 struct sctp_chunk *chunk = arg;
492 sctp_init_chunk_t *initchunk;
493 struct sctp_chunk *err_chunk;
494 struct sctp_packet *packet;
496 if (!sctp_vtag_verify(chunk, asoc))
497 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
499 /* 6.10 Bundling
500 * An endpoint MUST NOT bundle INIT, INIT ACK or
501 * SHUTDOWN COMPLETE with any other chunks.
503 if (!chunk->singleton)
504 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
506 /* Make sure that the INIT-ACK chunk has a valid length */
507 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
508 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
509 commands);
510 /* Grab the INIT header. */
511 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
513 /* Verify the INIT chunk before processing it. */
514 err_chunk = NULL;
515 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
516 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
517 &err_chunk)) {
519 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
521 /* This chunk contains fatal error. It is to be discarded.
522 * Send an ABORT, with causes. If there are no causes,
523 * then there wasn't enough memory. Just terminate
524 * the association.
526 if (err_chunk) {
527 packet = sctp_abort_pkt_new(ep, asoc, arg,
528 (__u8 *)(err_chunk->chunk_hdr) +
529 sizeof(sctp_chunkhdr_t),
530 ntohs(err_chunk->chunk_hdr->length) -
531 sizeof(sctp_chunkhdr_t));
533 sctp_chunk_free(err_chunk);
535 if (packet) {
536 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
537 SCTP_PACKET(packet));
538 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
539 error = SCTP_ERROR_INV_PARAM;
543 /* SCTP-AUTH, Section 6.3:
544 * It should be noted that if the receiver wants to tear
545 * down an association in an authenticated way only, the
546 * handling of malformed packets should not result in
547 * tearing down the association.
549 * This means that if we only want to abort associations
550 * in an authenticated way (i.e AUTH+ABORT), then we
551 * can't destroy this association just becuase the packet
552 * was malformed.
554 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
555 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
557 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
558 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
559 asoc, chunk->transport);
562 /* Tag the variable length parameters. Note that we never
563 * convert the parameters in an INIT chunk.
565 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
567 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
569 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
570 SCTP_PEER_INIT(initchunk));
572 /* Reset init error count upon receipt of INIT-ACK. */
573 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
575 /* 5.1 C) "A" shall stop the T1-init timer and leave
576 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
577 * timer, and enter the COOKIE-ECHOED state.
579 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
580 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
581 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
582 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
583 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
584 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
586 /* SCTP-AUTH: genereate the assocition shared keys so that
587 * we can potentially signe the COOKIE-ECHO.
589 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
591 /* 5.1 C) "A" shall then send the State Cookie received in the
592 * INIT ACK chunk in a COOKIE ECHO chunk, ...
594 /* If there is any errors to report, send the ERROR chunk generated
595 * for unknown parameters as well.
597 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
598 SCTP_CHUNK(err_chunk));
600 return SCTP_DISPOSITION_CONSUME;
604 * Respond to a normal COOKIE ECHO chunk.
605 * We are the side that is being asked for an association.
607 * Section: 5.1 Normal Establishment of an Association, D
608 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
609 * with a COOKIE ACK chunk after building a TCB and moving to
610 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
611 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
612 * chunk MUST be the first chunk in the packet.
614 * IMPLEMENTATION NOTE: An implementation may choose to send the
615 * Communication Up notification to the SCTP user upon reception
616 * of a valid COOKIE ECHO chunk.
618 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
619 * D) Rules for packet carrying a COOKIE ECHO
621 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
622 * Initial Tag received in the INIT ACK.
624 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
626 * Inputs
627 * (endpoint, asoc, chunk)
629 * Outputs
630 * (asoc, reply_msg, msg_up, timers, counters)
632 * The return value is the disposition of the chunk.
634 sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
635 const struct sctp_association *asoc,
636 const sctp_subtype_t type, void *arg,
637 sctp_cmd_seq_t *commands)
639 struct sctp_chunk *chunk = arg;
640 struct sctp_association *new_asoc;
641 sctp_init_chunk_t *peer_init;
642 struct sctp_chunk *repl;
643 struct sctp_ulpevent *ev, *ai_ev = NULL;
644 int error = 0;
645 struct sctp_chunk *err_chk_p;
646 struct sock *sk;
648 /* If the packet is an OOTB packet which is temporarily on the
649 * control endpoint, respond with an ABORT.
651 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
652 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
653 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
656 /* Make sure that the COOKIE_ECHO chunk has a valid length.
657 * In this case, we check that we have enough for at least a
658 * chunk header. More detailed verification is done
659 * in sctp_unpack_cookie().
661 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
662 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
664 /* If the endpoint is not listening or if the number of associations
665 * on the TCP-style socket exceed the max backlog, respond with an
666 * ABORT.
668 sk = ep->base.sk;
669 if (!sctp_sstate(sk, LISTENING) ||
670 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
671 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
673 /* "Decode" the chunk. We have no optional parameters so we
674 * are in good shape.
676 chunk->subh.cookie_hdr =
677 (struct sctp_signed_cookie *)chunk->skb->data;
678 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
679 sizeof(sctp_chunkhdr_t)))
680 goto nomem;
682 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
683 * "Z" will reply with a COOKIE ACK chunk after building a TCB
684 * and moving to the ESTABLISHED state.
686 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
687 &err_chk_p);
689 /* FIXME:
690 * If the re-build failed, what is the proper error path
691 * from here?
693 * [We should abort the association. --piggy]
695 if (!new_asoc) {
696 /* FIXME: Several errors are possible. A bad cookie should
697 * be silently discarded, but think about logging it too.
699 switch (error) {
700 case -SCTP_IERROR_NOMEM:
701 goto nomem;
703 case -SCTP_IERROR_STALE_COOKIE:
704 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
705 err_chk_p);
706 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
708 case -SCTP_IERROR_BAD_SIG:
709 default:
710 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
715 /* Delay state machine commands until later.
717 * Re-build the bind address for the association is done in
718 * the sctp_unpack_cookie() already.
720 /* This is a brand-new association, so these are not yet side
721 * effects--it is safe to run them here.
723 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
725 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
726 &chunk->subh.cookie_hdr->c.peer_addr,
727 peer_init, GFP_ATOMIC))
728 goto nomem_init;
730 /* SCTP-AUTH: Now that we've populate required fields in
731 * sctp_process_init, set up the assocaition shared keys as
732 * necessary so that we can potentially authenticate the ACK
734 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
735 if (error)
736 goto nomem_init;
738 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
739 * is supposed to be authenticated and we have to do delayed
740 * authentication. We've just recreated the association using
741 * the information in the cookie and now it's much easier to
742 * do the authentication.
744 if (chunk->auth_chunk) {
745 struct sctp_chunk auth;
746 sctp_ierror_t ret;
748 /* set-up our fake chunk so that we can process it */
749 auth.skb = chunk->auth_chunk;
750 auth.asoc = chunk->asoc;
751 auth.sctp_hdr = chunk->sctp_hdr;
752 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
753 sizeof(sctp_chunkhdr_t));
754 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
755 auth.transport = chunk->transport;
757 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
759 /* We can now safely free the auth_chunk clone */
760 kfree_skb(chunk->auth_chunk);
762 if (ret != SCTP_IERROR_NO_ERROR) {
763 sctp_association_free(new_asoc);
764 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
768 repl = sctp_make_cookie_ack(new_asoc, chunk);
769 if (!repl)
770 goto nomem_init;
772 /* RFC 2960 5.1 Normal Establishment of an Association
774 * D) IMPLEMENTATION NOTE: An implementation may choose to
775 * send the Communication Up notification to the SCTP user
776 * upon reception of a valid COOKIE ECHO chunk.
778 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
779 new_asoc->c.sinit_num_ostreams,
780 new_asoc->c.sinit_max_instreams,
781 NULL, GFP_ATOMIC);
782 if (!ev)
783 goto nomem_ev;
785 /* Sockets API Draft Section 5.3.1.6
786 * When a peer sends a Adaptation Layer Indication parameter , SCTP
787 * delivers this notification to inform the application that of the
788 * peers requested adaptation layer.
790 if (new_asoc->peer.adaptation_ind) {
791 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
792 GFP_ATOMIC);
793 if (!ai_ev)
794 goto nomem_aiev;
797 /* Add all the state machine commands now since we've created
798 * everything. This way we don't introduce memory corruptions
799 * during side-effect processing and correclty count established
800 * associations.
802 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
803 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
804 SCTP_STATE(SCTP_STATE_ESTABLISHED));
805 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
806 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
807 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
809 if (new_asoc->autoclose)
810 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
811 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
813 /* This will send the COOKIE ACK */
814 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
816 /* Queue the ASSOC_CHANGE event */
817 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
819 /* Send up the Adaptation Layer Indication event */
820 if (ai_ev)
821 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
822 SCTP_ULPEVENT(ai_ev));
824 return SCTP_DISPOSITION_CONSUME;
826 nomem_aiev:
827 sctp_ulpevent_free(ev);
828 nomem_ev:
829 sctp_chunk_free(repl);
830 nomem_init:
831 sctp_association_free(new_asoc);
832 nomem:
833 return SCTP_DISPOSITION_NOMEM;
837 * Respond to a normal COOKIE ACK chunk.
838 * We are the side that is being asked for an association.
840 * RFC 2960 5.1 Normal Establishment of an Association
842 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
843 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
844 * timer. It may also notify its ULP about the successful
845 * establishment of the association with a Communication Up
846 * notification (see Section 10).
848 * Verification Tag:
849 * Inputs
850 * (endpoint, asoc, chunk)
852 * Outputs
853 * (asoc, reply_msg, msg_up, timers, counters)
855 * The return value is the disposition of the chunk.
857 sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
858 const struct sctp_association *asoc,
859 const sctp_subtype_t type, void *arg,
860 sctp_cmd_seq_t *commands)
862 struct sctp_chunk *chunk = arg;
863 struct sctp_ulpevent *ev;
865 if (!sctp_vtag_verify(chunk, asoc))
866 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
868 /* Verify that the chunk length for the COOKIE-ACK is OK.
869 * If we don't do this, any bundled chunks may be junked.
871 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
872 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
873 commands);
875 /* Reset init error count upon receipt of COOKIE-ACK,
876 * to avoid problems with the managemement of this
877 * counter in stale cookie situations when a transition back
878 * from the COOKIE-ECHOED state to the COOKIE-WAIT
879 * state is performed.
881 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
883 /* RFC 2960 5.1 Normal Establishment of an Association
885 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
886 * from the COOKIE-ECHOED state to the ESTABLISHED state,
887 * stopping the T1-cookie timer.
889 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
890 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
891 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
892 SCTP_STATE(SCTP_STATE_ESTABLISHED));
893 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
894 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
895 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
896 if (asoc->autoclose)
897 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
898 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
900 /* It may also notify its ULP about the successful
901 * establishment of the association with a Communication Up
902 * notification (see Section 10).
904 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
905 0, asoc->c.sinit_num_ostreams,
906 asoc->c.sinit_max_instreams,
907 NULL, GFP_ATOMIC);
909 if (!ev)
910 goto nomem;
912 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
914 /* Sockets API Draft Section 5.3.1.6
915 * When a peer sends a Adaptation Layer Indication parameter , SCTP
916 * delivers this notification to inform the application that of the
917 * peers requested adaptation layer.
919 if (asoc->peer.adaptation_ind) {
920 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
921 if (!ev)
922 goto nomem;
924 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
925 SCTP_ULPEVENT(ev));
928 return SCTP_DISPOSITION_CONSUME;
929 nomem:
930 return SCTP_DISPOSITION_NOMEM;
933 /* Generate and sendout a heartbeat packet. */
934 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
935 const struct sctp_association *asoc,
936 const sctp_subtype_t type,
937 void *arg,
938 sctp_cmd_seq_t *commands)
940 struct sctp_transport *transport = (struct sctp_transport *) arg;
941 struct sctp_chunk *reply;
942 sctp_sender_hb_info_t hbinfo;
943 size_t paylen = 0;
945 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
946 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
947 hbinfo.daddr = transport->ipaddr;
948 hbinfo.sent_at = jiffies;
949 hbinfo.hb_nonce = transport->hb_nonce;
951 /* Send a heartbeat to our peer. */
952 paylen = sizeof(sctp_sender_hb_info_t);
953 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
954 if (!reply)
955 return SCTP_DISPOSITION_NOMEM;
957 /* Set rto_pending indicating that an RTT measurement
958 * is started with this heartbeat chunk.
960 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
961 SCTP_TRANSPORT(transport));
963 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
964 return SCTP_DISPOSITION_CONSUME;
967 /* Generate a HEARTBEAT packet on the given transport. */
968 sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
969 const struct sctp_association *asoc,
970 const sctp_subtype_t type,
971 void *arg,
972 sctp_cmd_seq_t *commands)
974 struct sctp_transport *transport = (struct sctp_transport *) arg;
976 if (asoc->overall_error_count >= asoc->max_retrans) {
977 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
978 SCTP_ERROR(ETIMEDOUT));
979 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
980 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
981 SCTP_PERR(SCTP_ERROR_NO_ERROR));
982 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
983 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
984 return SCTP_DISPOSITION_DELETE_TCB;
987 /* Section 3.3.5.
988 * The Sender-specific Heartbeat Info field should normally include
989 * information about the sender's current time when this HEARTBEAT
990 * chunk is sent and the destination transport address to which this
991 * HEARTBEAT is sent (see Section 8.3).
994 if (transport->param_flags & SPP_HB_ENABLE) {
995 if (SCTP_DISPOSITION_NOMEM ==
996 sctp_sf_heartbeat(ep, asoc, type, arg,
997 commands))
998 return SCTP_DISPOSITION_NOMEM;
1000 /* Set transport error counter and association error counter
1001 * when sending heartbeat.
1003 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1004 SCTP_TRANSPORT(transport));
1006 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1007 SCTP_TRANSPORT(transport));
1008 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1009 SCTP_TRANSPORT(transport));
1011 return SCTP_DISPOSITION_CONSUME;
1015 * Process an heartbeat request.
1017 * Section: 8.3 Path Heartbeat
1018 * The receiver of the HEARTBEAT should immediately respond with a
1019 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1020 * from the received HEARTBEAT chunk.
1022 * Verification Tag: 8.5 Verification Tag [Normal verification]
1023 * When receiving an SCTP packet, the endpoint MUST ensure that the
1024 * value in the Verification Tag field of the received SCTP packet
1025 * matches its own Tag. If the received Verification Tag value does not
1026 * match the receiver's own tag value, the receiver shall silently
1027 * discard the packet and shall not process it any further except for
1028 * those cases listed in Section 8.5.1 below.
1030 * Inputs
1031 * (endpoint, asoc, chunk)
1033 * Outputs
1034 * (asoc, reply_msg, msg_up, timers, counters)
1036 * The return value is the disposition of the chunk.
1038 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1039 const struct sctp_association *asoc,
1040 const sctp_subtype_t type,
1041 void *arg,
1042 sctp_cmd_seq_t *commands)
1044 struct sctp_chunk *chunk = arg;
1045 struct sctp_chunk *reply;
1046 size_t paylen = 0;
1048 if (!sctp_vtag_verify(chunk, asoc))
1049 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1051 /* Make sure that the HEARTBEAT chunk has a valid length. */
1052 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1053 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1054 commands);
1056 /* 8.3 The receiver of the HEARTBEAT should immediately
1057 * respond with a HEARTBEAT ACK that contains the Heartbeat
1058 * Information field copied from the received HEARTBEAT chunk.
1060 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1061 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1062 if (!pskb_pull(chunk->skb, paylen))
1063 goto nomem;
1065 reply = sctp_make_heartbeat_ack(asoc, chunk,
1066 chunk->subh.hb_hdr, paylen);
1067 if (!reply)
1068 goto nomem;
1070 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1071 return SCTP_DISPOSITION_CONSUME;
1073 nomem:
1074 return SCTP_DISPOSITION_NOMEM;
1078 * Process the returning HEARTBEAT ACK.
1080 * Section: 8.3 Path Heartbeat
1081 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1082 * should clear the error counter of the destination transport
1083 * address to which the HEARTBEAT was sent, and mark the destination
1084 * transport address as active if it is not so marked. The endpoint may
1085 * optionally report to the upper layer when an inactive destination
1086 * address is marked as active due to the reception of the latest
1087 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1088 * clear the association overall error count as well (as defined
1089 * in section 8.1).
1091 * The receiver of the HEARTBEAT ACK should also perform an RTT
1092 * measurement for that destination transport address using the time
1093 * value carried in the HEARTBEAT ACK chunk.
1095 * Verification Tag: 8.5 Verification Tag [Normal verification]
1097 * Inputs
1098 * (endpoint, asoc, chunk)
1100 * Outputs
1101 * (asoc, reply_msg, msg_up, timers, counters)
1103 * The return value is the disposition of the chunk.
1105 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1106 const struct sctp_association *asoc,
1107 const sctp_subtype_t type,
1108 void *arg,
1109 sctp_cmd_seq_t *commands)
1111 struct sctp_chunk *chunk = arg;
1112 union sctp_addr from_addr;
1113 struct sctp_transport *link;
1114 sctp_sender_hb_info_t *hbinfo;
1115 unsigned long max_interval;
1117 if (!sctp_vtag_verify(chunk, asoc))
1118 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1120 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1121 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1122 sizeof(sctp_sender_hb_info_t)))
1123 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1124 commands);
1126 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1127 /* Make sure that the length of the parameter is what we expect */
1128 if (ntohs(hbinfo->param_hdr.length) !=
1129 sizeof(sctp_sender_hb_info_t)) {
1130 return SCTP_DISPOSITION_DISCARD;
1133 from_addr = hbinfo->daddr;
1134 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1136 /* This should never happen, but lets log it if so. */
1137 if (unlikely(!link)) {
1138 if (from_addr.sa.sa_family == AF_INET6) {
1139 if (net_ratelimit())
1140 printk(KERN_WARNING
1141 "%s association %p could not find address %pI6\n",
1142 __func__,
1143 asoc,
1144 &from_addr.v6.sin6_addr);
1145 } else {
1146 if (net_ratelimit())
1147 printk(KERN_WARNING
1148 "%s association %p could not find address %pI4\n",
1149 __func__,
1150 asoc,
1151 &from_addr.v4.sin_addr.s_addr);
1153 return SCTP_DISPOSITION_DISCARD;
1156 /* Validate the 64-bit random nonce. */
1157 if (hbinfo->hb_nonce != link->hb_nonce)
1158 return SCTP_DISPOSITION_DISCARD;
1160 max_interval = link->hbinterval + link->rto;
1162 /* Check if the timestamp looks valid. */
1163 if (time_after(hbinfo->sent_at, jiffies) ||
1164 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1165 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1166 "received for transport: %p\n",
1167 __func__, link);
1168 return SCTP_DISPOSITION_DISCARD;
1171 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1172 * the HEARTBEAT should clear the error counter of the
1173 * destination transport address to which the HEARTBEAT was
1174 * sent and mark the destination transport address as active if
1175 * it is not so marked.
1177 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1179 return SCTP_DISPOSITION_CONSUME;
1182 /* Helper function to send out an abort for the restart
1183 * condition.
1185 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1186 struct sctp_chunk *init,
1187 sctp_cmd_seq_t *commands)
1189 int len;
1190 struct sctp_packet *pkt;
1191 union sctp_addr_param *addrparm;
1192 struct sctp_errhdr *errhdr;
1193 struct sctp_endpoint *ep;
1194 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1195 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1197 /* Build the error on the stack. We are way to malloc crazy
1198 * throughout the code today.
1200 errhdr = (struct sctp_errhdr *)buffer;
1201 addrparm = (union sctp_addr_param *)errhdr->variable;
1203 /* Copy into a parm format. */
1204 len = af->to_addr_param(ssa, addrparm);
1205 len += sizeof(sctp_errhdr_t);
1207 errhdr->cause = SCTP_ERROR_RESTART;
1208 errhdr->length = htons(len);
1210 /* Assign to the control socket. */
1211 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1213 /* Association is NULL since this may be a restart attack and we
1214 * want to send back the attacker's vtag.
1216 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1218 if (!pkt)
1219 goto out;
1220 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1222 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1224 /* Discard the rest of the inbound packet. */
1225 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1227 out:
1228 /* Even if there is no memory, treat as a failure so
1229 * the packet will get dropped.
1231 return 0;
1234 /* A restart is occurring, check to make sure no new addresses
1235 * are being added as we may be under a takeover attack.
1237 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1238 const struct sctp_association *asoc,
1239 struct sctp_chunk *init,
1240 sctp_cmd_seq_t *commands)
1242 struct sctp_transport *new_addr, *addr;
1243 int found;
1245 /* Implementor's Guide - Sectin 5.2.2
1246 * ...
1247 * Before responding the endpoint MUST check to see if the
1248 * unexpected INIT adds new addresses to the association. If new
1249 * addresses are added to the association, the endpoint MUST respond
1250 * with an ABORT..
1253 /* Search through all current addresses and make sure
1254 * we aren't adding any new ones.
1256 new_addr = NULL;
1257 found = 0;
1259 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1260 transports) {
1261 found = 0;
1262 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1263 transports) {
1264 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1265 &addr->ipaddr)) {
1266 found = 1;
1267 break;
1270 if (!found)
1271 break;
1274 /* If a new address was added, ABORT the sender. */
1275 if (!found && new_addr) {
1276 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1279 /* Return success if all addresses were found. */
1280 return found;
1283 /* Populate the verification/tie tags based on overlapping INIT
1284 * scenario.
1286 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1288 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1289 const struct sctp_association *asoc)
1291 switch (asoc->state) {
1293 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1295 case SCTP_STATE_COOKIE_WAIT:
1296 new_asoc->c.my_vtag = asoc->c.my_vtag;
1297 new_asoc->c.my_ttag = asoc->c.my_vtag;
1298 new_asoc->c.peer_ttag = 0;
1299 break;
1301 case SCTP_STATE_COOKIE_ECHOED:
1302 new_asoc->c.my_vtag = asoc->c.my_vtag;
1303 new_asoc->c.my_ttag = asoc->c.my_vtag;
1304 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1305 break;
1307 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1308 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1310 default:
1311 new_asoc->c.my_ttag = asoc->c.my_vtag;
1312 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1313 break;
1316 /* Other parameters for the endpoint SHOULD be copied from the
1317 * existing parameters of the association (e.g. number of
1318 * outbound streams) into the INIT ACK and cookie.
1320 new_asoc->rwnd = asoc->rwnd;
1321 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1322 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1323 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1327 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1328 * handling action.
1330 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1332 * Returns value representing action to be taken. These action values
1333 * correspond to Action/Description values in RFC 2960, Table 2.
1335 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1336 const struct sctp_association *asoc)
1338 /* In this case, the peer may have restarted. */
1339 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1340 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1341 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1342 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1343 return 'A';
1345 /* Collision case B. */
1346 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1347 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1348 (0 == asoc->c.peer_vtag))) {
1349 return 'B';
1352 /* Collision case D. */
1353 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1354 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1355 return 'D';
1357 /* Collision case C. */
1358 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1359 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1360 (0 == new_asoc->c.my_ttag) &&
1361 (0 == new_asoc->c.peer_ttag))
1362 return 'C';
1364 /* No match to any of the special cases; discard this packet. */
1365 return 'E';
1368 /* Common helper routine for both duplicate and simulataneous INIT
1369 * chunk handling.
1371 static sctp_disposition_t sctp_sf_do_unexpected_init(
1372 const struct sctp_endpoint *ep,
1373 const struct sctp_association *asoc,
1374 const sctp_subtype_t type,
1375 void *arg, sctp_cmd_seq_t *commands)
1377 sctp_disposition_t retval;
1378 struct sctp_chunk *chunk = arg;
1379 struct sctp_chunk *repl;
1380 struct sctp_association *new_asoc;
1381 struct sctp_chunk *err_chunk;
1382 struct sctp_packet *packet;
1383 sctp_unrecognized_param_t *unk_param;
1384 int len;
1386 /* 6.10 Bundling
1387 * An endpoint MUST NOT bundle INIT, INIT ACK or
1388 * SHUTDOWN COMPLETE with any other chunks.
1390 * IG Section 2.11.2
1391 * Furthermore, we require that the receiver of an INIT chunk MUST
1392 * enforce these rules by silently discarding an arriving packet
1393 * with an INIT chunk that is bundled with other chunks.
1395 if (!chunk->singleton)
1396 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1398 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1399 * Tag.
1401 if (chunk->sctp_hdr->vtag != 0)
1402 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1404 /* Make sure that the INIT chunk has a valid length.
1405 * In this case, we generate a protocol violation since we have
1406 * an association established.
1408 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1409 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1410 commands);
1411 /* Grab the INIT header. */
1412 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1414 /* Tag the variable length parameters. */
1415 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1417 /* Verify the INIT chunk before processing it. */
1418 err_chunk = NULL;
1419 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1420 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1421 &err_chunk)) {
1422 /* This chunk contains fatal error. It is to be discarded.
1423 * Send an ABORT, with causes if there is any.
1425 if (err_chunk) {
1426 packet = sctp_abort_pkt_new(ep, asoc, arg,
1427 (__u8 *)(err_chunk->chunk_hdr) +
1428 sizeof(sctp_chunkhdr_t),
1429 ntohs(err_chunk->chunk_hdr->length) -
1430 sizeof(sctp_chunkhdr_t));
1432 if (packet) {
1433 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1434 SCTP_PACKET(packet));
1435 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1436 retval = SCTP_DISPOSITION_CONSUME;
1437 } else {
1438 retval = SCTP_DISPOSITION_NOMEM;
1440 goto cleanup;
1441 } else {
1442 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1443 commands);
1448 * Other parameters for the endpoint SHOULD be copied from the
1449 * existing parameters of the association (e.g. number of
1450 * outbound streams) into the INIT ACK and cookie.
1451 * FIXME: We are copying parameters from the endpoint not the
1452 * association.
1454 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1455 if (!new_asoc)
1456 goto nomem;
1458 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1459 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1460 goto nomem;
1462 /* In the outbound INIT ACK the endpoint MUST copy its current
1463 * Verification Tag and Peers Verification tag into a reserved
1464 * place (local tie-tag and per tie-tag) within the state cookie.
1466 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1467 sctp_source(chunk),
1468 (sctp_init_chunk_t *)chunk->chunk_hdr,
1469 GFP_ATOMIC))
1470 goto nomem;
1472 /* Make sure no new addresses are being added during the
1473 * restart. Do not do this check for COOKIE-WAIT state,
1474 * since there are no peer addresses to check against.
1475 * Upon return an ABORT will have been sent if needed.
1477 if (!sctp_state(asoc, COOKIE_WAIT)) {
1478 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1479 commands)) {
1480 retval = SCTP_DISPOSITION_CONSUME;
1481 goto nomem_retval;
1485 sctp_tietags_populate(new_asoc, asoc);
1487 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1489 /* If there are errors need to be reported for unknown parameters,
1490 * make sure to reserve enough room in the INIT ACK for them.
1492 len = 0;
1493 if (err_chunk) {
1494 len = ntohs(err_chunk->chunk_hdr->length) -
1495 sizeof(sctp_chunkhdr_t);
1498 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1499 if (!repl)
1500 goto nomem;
1502 /* If there are errors need to be reported for unknown parameters,
1503 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1504 * parameter.
1506 if (err_chunk) {
1507 /* Get the "Unrecognized parameter" parameter(s) out of the
1508 * ERROR chunk generated by sctp_verify_init(). Since the
1509 * error cause code for "unknown parameter" and the
1510 * "Unrecognized parameter" type is the same, we can
1511 * construct the parameters in INIT ACK by copying the
1512 * ERROR causes over.
1514 unk_param = (sctp_unrecognized_param_t *)
1515 ((__u8 *)(err_chunk->chunk_hdr) +
1516 sizeof(sctp_chunkhdr_t));
1517 /* Replace the cause code with the "Unrecognized parameter"
1518 * parameter type.
1520 sctp_addto_chunk(repl, len, unk_param);
1523 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1524 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1527 * Note: After sending out INIT ACK with the State Cookie parameter,
1528 * "Z" MUST NOT allocate any resources for this new association.
1529 * Otherwise, "Z" will be vulnerable to resource attacks.
1531 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1532 retval = SCTP_DISPOSITION_CONSUME;
1534 return retval;
1536 nomem:
1537 retval = SCTP_DISPOSITION_NOMEM;
1538 nomem_retval:
1539 if (new_asoc)
1540 sctp_association_free(new_asoc);
1541 cleanup:
1542 if (err_chunk)
1543 sctp_chunk_free(err_chunk);
1544 return retval;
1548 * Handle simultanous INIT.
1549 * This means we started an INIT and then we got an INIT request from
1550 * our peer.
1552 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1553 * This usually indicates an initialization collision, i.e., each
1554 * endpoint is attempting, at about the same time, to establish an
1555 * association with the other endpoint.
1557 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1558 * endpoint MUST respond with an INIT ACK using the same parameters it
1559 * sent in its original INIT chunk (including its Verification Tag,
1560 * unchanged). These original parameters are combined with those from the
1561 * newly received INIT chunk. The endpoint shall also generate a State
1562 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1563 * INIT to calculate the State Cookie.
1565 * After that, the endpoint MUST NOT change its state, the T1-init
1566 * timer shall be left running and the corresponding TCB MUST NOT be
1567 * destroyed. The normal procedures for handling State Cookies when
1568 * a TCB exists will resolve the duplicate INITs to a single association.
1570 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1571 * its Tie-Tags with the Tag information of itself and its peer (see
1572 * section 5.2.2 for a description of the Tie-Tags).
1574 * Verification Tag: Not explicit, but an INIT can not have a valid
1575 * verification tag, so we skip the check.
1577 * Inputs
1578 * (endpoint, asoc, chunk)
1580 * Outputs
1581 * (asoc, reply_msg, msg_up, timers, counters)
1583 * The return value is the disposition of the chunk.
1585 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1586 const struct sctp_association *asoc,
1587 const sctp_subtype_t type,
1588 void *arg,
1589 sctp_cmd_seq_t *commands)
1591 /* Call helper to do the real work for both simulataneous and
1592 * duplicate INIT chunk handling.
1594 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1598 * Handle duplicated INIT messages. These are usually delayed
1599 * restransmissions.
1601 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1602 * COOKIE-ECHOED and COOKIE-WAIT
1604 * Unless otherwise stated, upon reception of an unexpected INIT for
1605 * this association, the endpoint shall generate an INIT ACK with a
1606 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1607 * current Verification Tag and peer's Verification Tag into a reserved
1608 * place within the state cookie. We shall refer to these locations as
1609 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1610 * containing this INIT ACK MUST carry a Verification Tag value equal to
1611 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1612 * MUST contain a new Initiation Tag (randomly generated see Section
1613 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1614 * existing parameters of the association (e.g. number of outbound
1615 * streams) into the INIT ACK and cookie.
1617 * After sending out the INIT ACK, the endpoint shall take no further
1618 * actions, i.e., the existing association, including its current state,
1619 * and the corresponding TCB MUST NOT be changed.
1621 * Note: Only when a TCB exists and the association is not in a COOKIE-
1622 * WAIT state are the Tie-Tags populated. For a normal association INIT
1623 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1624 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1625 * State Cookie are populated as specified in section 5.2.1.
1627 * Verification Tag: Not specified, but an INIT has no way of knowing
1628 * what the verification tag could be, so we ignore it.
1630 * Inputs
1631 * (endpoint, asoc, chunk)
1633 * Outputs
1634 * (asoc, reply_msg, msg_up, timers, counters)
1636 * The return value is the disposition of the chunk.
1638 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1639 const struct sctp_association *asoc,
1640 const sctp_subtype_t type,
1641 void *arg,
1642 sctp_cmd_seq_t *commands)
1644 /* Call helper to do the real work for both simulataneous and
1645 * duplicate INIT chunk handling.
1647 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1652 * Unexpected INIT-ACK handler.
1654 * Section 5.2.3
1655 * If an INIT ACK received by an endpoint in any state other than the
1656 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1657 * An unexpected INIT ACK usually indicates the processing of an old or
1658 * duplicated INIT chunk.
1660 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1661 const struct sctp_association *asoc,
1662 const sctp_subtype_t type,
1663 void *arg, sctp_cmd_seq_t *commands)
1665 /* Per the above section, we'll discard the chunk if we have an
1666 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1668 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
1669 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1670 else
1671 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1674 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1676 * Section 5.2.4
1677 * A) In this case, the peer may have restarted.
1679 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1680 const struct sctp_association *asoc,
1681 struct sctp_chunk *chunk,
1682 sctp_cmd_seq_t *commands,
1683 struct sctp_association *new_asoc)
1685 sctp_init_chunk_t *peer_init;
1686 struct sctp_ulpevent *ev;
1687 struct sctp_chunk *repl;
1688 struct sctp_chunk *err;
1689 sctp_disposition_t disposition;
1691 /* new_asoc is a brand-new association, so these are not yet
1692 * side effects--it is safe to run them here.
1694 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1696 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1697 sctp_source(chunk), peer_init,
1698 GFP_ATOMIC))
1699 goto nomem;
1701 /* Make sure no new addresses are being added during the
1702 * restart. Though this is a pretty complicated attack
1703 * since you'd have to get inside the cookie.
1705 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1706 return SCTP_DISPOSITION_CONSUME;
1709 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1710 * the peer has restarted (Action A), it MUST NOT setup a new
1711 * association but instead resend the SHUTDOWN ACK and send an ERROR
1712 * chunk with a "Cookie Received while Shutting Down" error cause to
1713 * its peer.
1715 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1716 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1717 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1718 chunk, commands);
1719 if (SCTP_DISPOSITION_NOMEM == disposition)
1720 goto nomem;
1722 err = sctp_make_op_error(asoc, chunk,
1723 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1724 NULL, 0, 0);
1725 if (err)
1726 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1727 SCTP_CHUNK(err));
1729 return SCTP_DISPOSITION_CONSUME;
1732 /* For now, fail any unsent/unacked data. Consider the optional
1733 * choice of resending of this data.
1735 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1737 repl = sctp_make_cookie_ack(new_asoc, chunk);
1738 if (!repl)
1739 goto nomem;
1741 /* Report association restart to upper layer. */
1742 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1743 new_asoc->c.sinit_num_ostreams,
1744 new_asoc->c.sinit_max_instreams,
1745 NULL, GFP_ATOMIC);
1746 if (!ev)
1747 goto nomem_ev;
1749 /* Update the content of current association. */
1750 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1751 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1752 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1753 return SCTP_DISPOSITION_CONSUME;
1755 nomem_ev:
1756 sctp_chunk_free(repl);
1757 nomem:
1758 return SCTP_DISPOSITION_NOMEM;
1761 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1763 * Section 5.2.4
1764 * B) In this case, both sides may be attempting to start an association
1765 * at about the same time but the peer endpoint started its INIT
1766 * after responding to the local endpoint's INIT
1768 /* This case represents an initialization collision. */
1769 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1770 const struct sctp_association *asoc,
1771 struct sctp_chunk *chunk,
1772 sctp_cmd_seq_t *commands,
1773 struct sctp_association *new_asoc)
1775 sctp_init_chunk_t *peer_init;
1776 struct sctp_chunk *repl;
1778 /* new_asoc is a brand-new association, so these are not yet
1779 * side effects--it is safe to run them here.
1781 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1782 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1783 sctp_source(chunk), peer_init,
1784 GFP_ATOMIC))
1785 goto nomem;
1787 /* Update the content of current association. */
1788 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1789 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1790 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1791 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1792 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1794 repl = sctp_make_cookie_ack(new_asoc, chunk);
1795 if (!repl)
1796 goto nomem;
1798 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1800 /* RFC 2960 5.1 Normal Establishment of an Association
1802 * D) IMPLEMENTATION NOTE: An implementation may choose to
1803 * send the Communication Up notification to the SCTP user
1804 * upon reception of a valid COOKIE ECHO chunk.
1806 * Sadly, this needs to be implemented as a side-effect, because
1807 * we are not guaranteed to have set the association id of the real
1808 * association and so these notifications need to be delayed until
1809 * the association id is allocated.
1812 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1814 /* Sockets API Draft Section 5.3.1.6
1815 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1816 * delivers this notification to inform the application that of the
1817 * peers requested adaptation layer.
1819 * This also needs to be done as a side effect for the same reason as
1820 * above.
1822 if (asoc->peer.adaptation_ind)
1823 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1825 return SCTP_DISPOSITION_CONSUME;
1827 nomem:
1828 return SCTP_DISPOSITION_NOMEM;
1831 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1833 * Section 5.2.4
1834 * C) In this case, the local endpoint's cookie has arrived late.
1835 * Before it arrived, the local endpoint sent an INIT and received an
1836 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1837 * but a new tag of its own.
1839 /* This case represents an initialization collision. */
1840 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1841 const struct sctp_association *asoc,
1842 struct sctp_chunk *chunk,
1843 sctp_cmd_seq_t *commands,
1844 struct sctp_association *new_asoc)
1846 /* The cookie should be silently discarded.
1847 * The endpoint SHOULD NOT change states and should leave
1848 * any timers running.
1850 return SCTP_DISPOSITION_DISCARD;
1853 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1855 * Section 5.2.4
1857 * D) When both local and remote tags match the endpoint should always
1858 * enter the ESTABLISHED state, if it has not already done so.
1860 /* This case represents an initialization collision. */
1861 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1862 const struct sctp_association *asoc,
1863 struct sctp_chunk *chunk,
1864 sctp_cmd_seq_t *commands,
1865 struct sctp_association *new_asoc)
1867 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1868 struct sctp_chunk *repl;
1870 /* Clarification from Implementor's Guide:
1871 * D) When both local and remote tags match the endpoint should
1872 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1873 * It should stop any cookie timer that may be running and send
1874 * a COOKIE ACK.
1877 /* Don't accidentally move back into established state. */
1878 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1879 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1880 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1881 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1882 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1883 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1884 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1885 SCTP_NULL());
1887 /* RFC 2960 5.1 Normal Establishment of an Association
1889 * D) IMPLEMENTATION NOTE: An implementation may choose
1890 * to send the Communication Up notification to the
1891 * SCTP user upon reception of a valid COOKIE
1892 * ECHO chunk.
1894 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1895 SCTP_COMM_UP, 0,
1896 asoc->c.sinit_num_ostreams,
1897 asoc->c.sinit_max_instreams,
1898 NULL, GFP_ATOMIC);
1899 if (!ev)
1900 goto nomem;
1902 /* Sockets API Draft Section 5.3.1.6
1903 * When a peer sends a Adaptation Layer Indication parameter,
1904 * SCTP delivers this notification to inform the application
1905 * that of the peers requested adaptation layer.
1907 if (asoc->peer.adaptation_ind) {
1908 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1909 GFP_ATOMIC);
1910 if (!ai_ev)
1911 goto nomem;
1916 repl = sctp_make_cookie_ack(new_asoc, chunk);
1917 if (!repl)
1918 goto nomem;
1920 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1922 if (ev)
1923 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1924 SCTP_ULPEVENT(ev));
1925 if (ai_ev)
1926 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1927 SCTP_ULPEVENT(ai_ev));
1929 return SCTP_DISPOSITION_CONSUME;
1931 nomem:
1932 if (ai_ev)
1933 sctp_ulpevent_free(ai_ev);
1934 if (ev)
1935 sctp_ulpevent_free(ev);
1936 return SCTP_DISPOSITION_NOMEM;
1940 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1941 * chunk was retransmitted and then delayed in the network.
1943 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1945 * Verification Tag: None. Do cookie validation.
1947 * Inputs
1948 * (endpoint, asoc, chunk)
1950 * Outputs
1951 * (asoc, reply_msg, msg_up, timers, counters)
1953 * The return value is the disposition of the chunk.
1955 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1956 const struct sctp_association *asoc,
1957 const sctp_subtype_t type,
1958 void *arg,
1959 sctp_cmd_seq_t *commands)
1961 sctp_disposition_t retval;
1962 struct sctp_chunk *chunk = arg;
1963 struct sctp_association *new_asoc;
1964 int error = 0;
1965 char action;
1966 struct sctp_chunk *err_chk_p;
1968 /* Make sure that the chunk has a valid length from the protocol
1969 * perspective. In this case check to make sure we have at least
1970 * enough for the chunk header. Cookie length verification is
1971 * done later.
1973 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1974 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1975 commands);
1977 /* "Decode" the chunk. We have no optional parameters so we
1978 * are in good shape.
1980 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1981 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1982 sizeof(sctp_chunkhdr_t)))
1983 goto nomem;
1985 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1986 * of a duplicate COOKIE ECHO match the Verification Tags of the
1987 * current association, consider the State Cookie valid even if
1988 * the lifespan is exceeded.
1990 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1991 &err_chk_p);
1993 /* FIXME:
1994 * If the re-build failed, what is the proper error path
1995 * from here?
1997 * [We should abort the association. --piggy]
1999 if (!new_asoc) {
2000 /* FIXME: Several errors are possible. A bad cookie should
2001 * be silently discarded, but think about logging it too.
2003 switch (error) {
2004 case -SCTP_IERROR_NOMEM:
2005 goto nomem;
2007 case -SCTP_IERROR_STALE_COOKIE:
2008 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2009 err_chk_p);
2010 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2011 case -SCTP_IERROR_BAD_SIG:
2012 default:
2013 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2017 /* Compare the tie_tag in cookie with the verification tag of
2018 * current association.
2020 action = sctp_tietags_compare(new_asoc, asoc);
2022 switch (action) {
2023 case 'A': /* Association restart. */
2024 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2025 new_asoc);
2026 break;
2028 case 'B': /* Collision case B. */
2029 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2030 new_asoc);
2031 break;
2033 case 'C': /* Collision case C. */
2034 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2035 new_asoc);
2036 break;
2038 case 'D': /* Collision case D. */
2039 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2040 new_asoc);
2041 break;
2043 default: /* Discard packet for all others. */
2044 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2045 break;
2048 /* Delete the tempory new association. */
2049 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2050 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2052 return retval;
2054 nomem:
2055 return SCTP_DISPOSITION_NOMEM;
2059 * Process an ABORT. (SHUTDOWN-PENDING state)
2061 * See sctp_sf_do_9_1_abort().
2063 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2064 const struct sctp_endpoint *ep,
2065 const struct sctp_association *asoc,
2066 const sctp_subtype_t type,
2067 void *arg,
2068 sctp_cmd_seq_t *commands)
2070 struct sctp_chunk *chunk = arg;
2072 if (!sctp_vtag_verify_either(chunk, asoc))
2073 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2075 /* Make sure that the ABORT chunk has a valid length.
2076 * Since this is an ABORT chunk, we have to discard it
2077 * because of the following text:
2078 * RFC 2960, Section 3.3.7
2079 * If an endpoint receives an ABORT with a format error or for an
2080 * association that doesn't exist, it MUST silently discard it.
2081 * Becasue the length is "invalid", we can't really discard just
2082 * as we do not know its true length. So, to be safe, discard the
2083 * packet.
2085 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2086 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2088 /* ADD-IP: Special case for ABORT chunks
2089 * F4) One special consideration is that ABORT Chunks arriving
2090 * destined to the IP address being deleted MUST be
2091 * ignored (see Section 5.3.1 for further details).
2093 if (SCTP_ADDR_DEL ==
2094 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2095 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2097 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2101 * Process an ABORT. (SHUTDOWN-SENT state)
2103 * See sctp_sf_do_9_1_abort().
2105 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2106 const struct sctp_association *asoc,
2107 const sctp_subtype_t type,
2108 void *arg,
2109 sctp_cmd_seq_t *commands)
2111 struct sctp_chunk *chunk = arg;
2113 if (!sctp_vtag_verify_either(chunk, asoc))
2114 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2116 /* Make sure that the ABORT chunk has a valid length.
2117 * Since this is an ABORT chunk, we have to discard it
2118 * because of the following text:
2119 * RFC 2960, Section 3.3.7
2120 * If an endpoint receives an ABORT with a format error or for an
2121 * association that doesn't exist, it MUST silently discard it.
2122 * Becasue the length is "invalid", we can't really discard just
2123 * as we do not know its true length. So, to be safe, discard the
2124 * packet.
2126 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2127 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2129 /* ADD-IP: Special case for ABORT chunks
2130 * F4) One special consideration is that ABORT Chunks arriving
2131 * destined to the IP address being deleted MUST be
2132 * ignored (see Section 5.3.1 for further details).
2134 if (SCTP_ADDR_DEL ==
2135 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2136 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2138 /* Stop the T2-shutdown timer. */
2139 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2140 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2142 /* Stop the T5-shutdown guard timer. */
2143 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2144 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2146 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2150 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2152 * See sctp_sf_do_9_1_abort().
2154 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2155 const struct sctp_endpoint *ep,
2156 const struct sctp_association *asoc,
2157 const sctp_subtype_t type,
2158 void *arg,
2159 sctp_cmd_seq_t *commands)
2161 /* The same T2 timer, so we should be able to use
2162 * common function with the SHUTDOWN-SENT state.
2164 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2168 * Handle an Error received in COOKIE_ECHOED state.
2170 * Only handle the error type of stale COOKIE Error, the other errors will
2171 * be ignored.
2173 * Inputs
2174 * (endpoint, asoc, chunk)
2176 * Outputs
2177 * (asoc, reply_msg, msg_up, timers, counters)
2179 * The return value is the disposition of the chunk.
2181 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2182 const struct sctp_association *asoc,
2183 const sctp_subtype_t type,
2184 void *arg,
2185 sctp_cmd_seq_t *commands)
2187 struct sctp_chunk *chunk = arg;
2188 sctp_errhdr_t *err;
2190 if (!sctp_vtag_verify(chunk, asoc))
2191 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2193 /* Make sure that the ERROR chunk has a valid length.
2194 * The parameter walking depends on this as well.
2196 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2197 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2198 commands);
2200 /* Process the error here */
2201 /* FUTURE FIXME: When PR-SCTP related and other optional
2202 * parms are emitted, this will have to change to handle multiple
2203 * errors.
2205 sctp_walk_errors(err, chunk->chunk_hdr) {
2206 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2207 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
2208 arg, commands);
2211 /* It is possible to have malformed error causes, and that
2212 * will cause us to end the walk early. However, since
2213 * we are discarding the packet, there should be no adverse
2214 * affects.
2216 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2220 * Handle a Stale COOKIE Error
2222 * Section: 5.2.6 Handle Stale COOKIE Error
2223 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2224 * one of the following three alternatives.
2225 * ...
2226 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2227 * Preservative parameter requesting an extension to the lifetime of
2228 * the State Cookie. When calculating the time extension, an
2229 * implementation SHOULD use the RTT information measured based on the
2230 * previous COOKIE ECHO / ERROR exchange, and should add no more
2231 * than 1 second beyond the measured RTT, due to long State Cookie
2232 * lifetimes making the endpoint more subject to a replay attack.
2234 * Verification Tag: Not explicit, but safe to ignore.
2236 * Inputs
2237 * (endpoint, asoc, chunk)
2239 * Outputs
2240 * (asoc, reply_msg, msg_up, timers, counters)
2242 * The return value is the disposition of the chunk.
2244 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2245 const struct sctp_association *asoc,
2246 const sctp_subtype_t type,
2247 void *arg,
2248 sctp_cmd_seq_t *commands)
2250 struct sctp_chunk *chunk = arg;
2251 time_t stale;
2252 sctp_cookie_preserve_param_t bht;
2253 sctp_errhdr_t *err;
2254 struct sctp_chunk *reply;
2255 struct sctp_bind_addr *bp;
2256 int attempts = asoc->init_err_counter + 1;
2258 if (attempts > asoc->max_init_attempts) {
2259 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2260 SCTP_ERROR(ETIMEDOUT));
2261 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2262 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2263 return SCTP_DISPOSITION_DELETE_TCB;
2266 err = (sctp_errhdr_t *)(chunk->skb->data);
2268 /* When calculating the time extension, an implementation
2269 * SHOULD use the RTT information measured based on the
2270 * previous COOKIE ECHO / ERROR exchange, and should add no
2271 * more than 1 second beyond the measured RTT, due to long
2272 * State Cookie lifetimes making the endpoint more subject to
2273 * a replay attack.
2274 * Measure of Staleness's unit is usec. (1/1000000 sec)
2275 * Suggested Cookie Life-span Increment's unit is msec.
2276 * (1/1000 sec)
2277 * In general, if you use the suggested cookie life, the value
2278 * found in the field of measure of staleness should be doubled
2279 * to give ample time to retransmit the new cookie and thus
2280 * yield a higher probability of success on the reattempt.
2282 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2283 stale = (stale * 2) / 1000;
2285 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2286 bht.param_hdr.length = htons(sizeof(bht));
2287 bht.lifespan_increment = htonl(stale);
2289 /* Build that new INIT chunk. */
2290 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2291 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2292 if (!reply)
2293 goto nomem;
2295 sctp_addto_chunk(reply, sizeof(bht), &bht);
2297 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2298 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2300 /* Stop pending T3-rtx and heartbeat timers */
2301 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2302 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2304 /* Delete non-primary peer ip addresses since we are transitioning
2305 * back to the COOKIE-WAIT state
2307 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2309 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2310 * resend
2312 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2313 SCTP_TRANSPORT(asoc->peer.primary_path));
2315 /* Cast away the const modifier, as we want to just
2316 * rerun it through as a sideffect.
2318 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2320 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2321 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2322 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2323 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2324 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2325 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2327 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2329 return SCTP_DISPOSITION_CONSUME;
2331 nomem:
2332 return SCTP_DISPOSITION_NOMEM;
2336 * Process an ABORT.
2338 * Section: 9.1
2339 * After checking the Verification Tag, the receiving endpoint shall
2340 * remove the association from its record, and shall report the
2341 * termination to its upper layer.
2343 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2344 * B) Rules for packet carrying ABORT:
2346 * - The endpoint shall always fill in the Verification Tag field of the
2347 * outbound packet with the destination endpoint's tag value if it
2348 * is known.
2350 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2351 * MUST follow the procedure described in Section 8.4.
2353 * - The receiver MUST accept the packet if the Verification Tag
2354 * matches either its own tag, OR the tag of its peer. Otherwise, the
2355 * receiver MUST silently discard the packet and take no further
2356 * action.
2358 * Inputs
2359 * (endpoint, asoc, chunk)
2361 * Outputs
2362 * (asoc, reply_msg, msg_up, timers, counters)
2364 * The return value is the disposition of the chunk.
2366 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2367 const struct sctp_association *asoc,
2368 const sctp_subtype_t type,
2369 void *arg,
2370 sctp_cmd_seq_t *commands)
2372 struct sctp_chunk *chunk = arg;
2374 if (!sctp_vtag_verify_either(chunk, asoc))
2375 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2377 /* Make sure that the ABORT chunk has a valid length.
2378 * Since this is an ABORT chunk, we have to discard it
2379 * because of the following text:
2380 * RFC 2960, Section 3.3.7
2381 * If an endpoint receives an ABORT with a format error or for an
2382 * association that doesn't exist, it MUST silently discard it.
2383 * Becasue the length is "invalid", we can't really discard just
2384 * as we do not know its true length. So, to be safe, discard the
2385 * packet.
2387 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2388 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2390 /* ADD-IP: Special case for ABORT chunks
2391 * F4) One special consideration is that ABORT Chunks arriving
2392 * destined to the IP address being deleted MUST be
2393 * ignored (see Section 5.3.1 for further details).
2395 if (SCTP_ADDR_DEL ==
2396 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2397 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2399 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2402 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2403 const struct sctp_association *asoc,
2404 const sctp_subtype_t type,
2405 void *arg,
2406 sctp_cmd_seq_t *commands)
2408 struct sctp_chunk *chunk = arg;
2409 unsigned len;
2410 __be16 error = SCTP_ERROR_NO_ERROR;
2412 /* See if we have an error cause code in the chunk. */
2413 len = ntohs(chunk->chunk_hdr->length);
2414 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2415 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2417 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2418 /* ASSOC_FAILED will DELETE_TCB. */
2419 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2420 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2421 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2423 return SCTP_DISPOSITION_ABORT;
2427 * Process an ABORT. (COOKIE-WAIT state)
2429 * See sctp_sf_do_9_1_abort() above.
2431 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2432 const struct sctp_association *asoc,
2433 const sctp_subtype_t type,
2434 void *arg,
2435 sctp_cmd_seq_t *commands)
2437 struct sctp_chunk *chunk = arg;
2438 unsigned len;
2439 __be16 error = SCTP_ERROR_NO_ERROR;
2441 if (!sctp_vtag_verify_either(chunk, asoc))
2442 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2444 /* Make sure that the ABORT chunk has a valid length.
2445 * Since this is an ABORT chunk, we have to discard it
2446 * because of the following text:
2447 * RFC 2960, Section 3.3.7
2448 * If an endpoint receives an ABORT with a format error or for an
2449 * association that doesn't exist, it MUST silently discard it.
2450 * Becasue the length is "invalid", we can't really discard just
2451 * as we do not know its true length. So, to be safe, discard the
2452 * packet.
2454 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2455 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2457 /* See if we have an error cause code in the chunk. */
2458 len = ntohs(chunk->chunk_hdr->length);
2459 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2460 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2462 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2463 chunk->transport);
2467 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2469 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2470 const struct sctp_association *asoc,
2471 const sctp_subtype_t type,
2472 void *arg,
2473 sctp_cmd_seq_t *commands)
2475 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2476 ENOPROTOOPT, asoc,
2477 (struct sctp_transport *)arg);
2481 * Process an ABORT. (COOKIE-ECHOED state)
2483 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2484 const struct sctp_association *asoc,
2485 const sctp_subtype_t type,
2486 void *arg,
2487 sctp_cmd_seq_t *commands)
2489 /* There is a single T1 timer, so we should be able to use
2490 * common function with the COOKIE-WAIT state.
2492 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2496 * Stop T1 timer and abort association with "INIT failed".
2498 * This is common code called by several sctp_sf_*_abort() functions above.
2500 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
2501 __be16 error, int sk_err,
2502 const struct sctp_association *asoc,
2503 struct sctp_transport *transport)
2505 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2506 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2507 SCTP_STATE(SCTP_STATE_CLOSED));
2508 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2509 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2510 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2511 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2512 /* CMD_INIT_FAILED will DELETE_TCB. */
2513 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2514 SCTP_PERR(error));
2515 return SCTP_DISPOSITION_ABORT;
2519 * sctp_sf_do_9_2_shut
2521 * Section: 9.2
2522 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2523 * - enter the SHUTDOWN-RECEIVED state,
2525 * - stop accepting new data from its SCTP user
2527 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2528 * that all its outstanding DATA chunks have been received by the
2529 * SHUTDOWN sender.
2531 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2532 * send a SHUTDOWN in response to a ULP request. And should discard
2533 * subsequent SHUTDOWN chunks.
2535 * If there are still outstanding DATA chunks left, the SHUTDOWN
2536 * receiver shall continue to follow normal data transmission
2537 * procedures defined in Section 6 until all outstanding DATA chunks
2538 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2539 * new data from its SCTP user.
2541 * Verification Tag: 8.5 Verification Tag [Normal verification]
2543 * Inputs
2544 * (endpoint, asoc, chunk)
2546 * Outputs
2547 * (asoc, reply_msg, msg_up, timers, counters)
2549 * The return value is the disposition of the chunk.
2551 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2552 const struct sctp_association *asoc,
2553 const sctp_subtype_t type,
2554 void *arg,
2555 sctp_cmd_seq_t *commands)
2557 struct sctp_chunk *chunk = arg;
2558 sctp_shutdownhdr_t *sdh;
2559 sctp_disposition_t disposition;
2560 struct sctp_ulpevent *ev;
2561 __u32 ctsn;
2563 if (!sctp_vtag_verify(chunk, asoc))
2564 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2566 /* Make sure that the SHUTDOWN chunk has a valid length. */
2567 if (!sctp_chunk_length_valid(chunk,
2568 sizeof(struct sctp_shutdown_chunk_t)))
2569 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2570 commands);
2572 /* Convert the elaborate header. */
2573 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2574 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2575 chunk->subh.shutdown_hdr = sdh;
2576 ctsn = ntohl(sdh->cum_tsn_ack);
2578 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2579 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2580 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2581 return SCTP_DISPOSITION_DISCARD;
2584 /* If Cumulative TSN Ack beyond the max tsn currently
2585 * send, terminating the association and respond to the
2586 * sender with an ABORT.
2588 if (!TSN_lt(ctsn, asoc->next_tsn))
2589 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2591 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2592 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2593 * inform the application that it should cease sending data.
2595 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2596 if (!ev) {
2597 disposition = SCTP_DISPOSITION_NOMEM;
2598 goto out;
2600 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2602 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2603 * - enter the SHUTDOWN-RECEIVED state,
2604 * - stop accepting new data from its SCTP user
2606 * [This is implicit in the new state.]
2608 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2609 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2610 disposition = SCTP_DISPOSITION_CONSUME;
2612 if (sctp_outq_is_empty(&asoc->outqueue)) {
2613 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2614 arg, commands);
2617 if (SCTP_DISPOSITION_NOMEM == disposition)
2618 goto out;
2620 /* - verify, by checking the Cumulative TSN Ack field of the
2621 * chunk, that all its outstanding DATA chunks have been
2622 * received by the SHUTDOWN sender.
2624 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2625 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2627 out:
2628 return disposition;
2632 * sctp_sf_do_9_2_shut_ctsn
2634 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2635 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2636 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2637 * MUST be processed.
2639 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2640 const struct sctp_association *asoc,
2641 const sctp_subtype_t type,
2642 void *arg,
2643 sctp_cmd_seq_t *commands)
2645 struct sctp_chunk *chunk = arg;
2646 sctp_shutdownhdr_t *sdh;
2647 __u32 ctsn;
2649 if (!sctp_vtag_verify(chunk, asoc))
2650 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2652 /* Make sure that the SHUTDOWN chunk has a valid length. */
2653 if (!sctp_chunk_length_valid(chunk,
2654 sizeof(struct sctp_shutdown_chunk_t)))
2655 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2656 commands);
2658 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2659 ctsn = ntohl(sdh->cum_tsn_ack);
2661 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2662 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2663 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2664 return SCTP_DISPOSITION_DISCARD;
2667 /* If Cumulative TSN Ack beyond the max tsn currently
2668 * send, terminating the association and respond to the
2669 * sender with an ABORT.
2671 if (!TSN_lt(ctsn, asoc->next_tsn))
2672 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2674 /* verify, by checking the Cumulative TSN Ack field of the
2675 * chunk, that all its outstanding DATA chunks have been
2676 * received by the SHUTDOWN sender.
2678 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2679 SCTP_BE32(sdh->cum_tsn_ack));
2681 return SCTP_DISPOSITION_CONSUME;
2684 /* RFC 2960 9.2
2685 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2686 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2687 * transport addresses (either in the IP addresses or in the INIT chunk)
2688 * that belong to this association, it should discard the INIT chunk and
2689 * retransmit the SHUTDOWN ACK chunk.
2691 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2692 const struct sctp_association *asoc,
2693 const sctp_subtype_t type,
2694 void *arg,
2695 sctp_cmd_seq_t *commands)
2697 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2698 struct sctp_chunk *reply;
2700 /* Make sure that the chunk has a valid length */
2701 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2702 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2703 commands);
2705 /* Since we are not going to really process this INIT, there
2706 * is no point in verifying chunk boundries. Just generate
2707 * the SHUTDOWN ACK.
2709 reply = sctp_make_shutdown_ack(asoc, chunk);
2710 if (NULL == reply)
2711 goto nomem;
2713 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2714 * the T2-SHUTDOWN timer.
2716 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2718 /* and restart the T2-shutdown timer. */
2719 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2720 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2722 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2724 return SCTP_DISPOSITION_CONSUME;
2725 nomem:
2726 return SCTP_DISPOSITION_NOMEM;
2730 * sctp_sf_do_ecn_cwr
2732 * Section: Appendix A: Explicit Congestion Notification
2734 * CWR:
2736 * RFC 2481 details a specific bit for a sender to send in the header of
2737 * its next outbound TCP segment to indicate to its peer that it has
2738 * reduced its congestion window. This is termed the CWR bit. For
2739 * SCTP the same indication is made by including the CWR chunk.
2740 * This chunk contains one data element, i.e. the TSN number that
2741 * was sent in the ECNE chunk. This element represents the lowest
2742 * TSN number in the datagram that was originally marked with the
2743 * CE bit.
2745 * Verification Tag: 8.5 Verification Tag [Normal verification]
2746 * Inputs
2747 * (endpoint, asoc, chunk)
2749 * Outputs
2750 * (asoc, reply_msg, msg_up, timers, counters)
2752 * The return value is the disposition of the chunk.
2754 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2755 const struct sctp_association *asoc,
2756 const sctp_subtype_t type,
2757 void *arg,
2758 sctp_cmd_seq_t *commands)
2760 sctp_cwrhdr_t *cwr;
2761 struct sctp_chunk *chunk = arg;
2762 u32 lowest_tsn;
2764 if (!sctp_vtag_verify(chunk, asoc))
2765 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2767 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2768 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2769 commands);
2771 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2772 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2774 lowest_tsn = ntohl(cwr->lowest_tsn);
2776 /* Does this CWR ack the last sent congestion notification? */
2777 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2778 /* Stop sending ECNE. */
2779 sctp_add_cmd_sf(commands,
2780 SCTP_CMD_ECN_CWR,
2781 SCTP_U32(lowest_tsn));
2783 return SCTP_DISPOSITION_CONSUME;
2787 * sctp_sf_do_ecne
2789 * Section: Appendix A: Explicit Congestion Notification
2791 * ECN-Echo
2793 * RFC 2481 details a specific bit for a receiver to send back in its
2794 * TCP acknowledgements to notify the sender of the Congestion
2795 * Experienced (CE) bit having arrived from the network. For SCTP this
2796 * same indication is made by including the ECNE chunk. This chunk
2797 * contains one data element, i.e. the lowest TSN associated with the IP
2798 * datagram marked with the CE bit.....
2800 * Verification Tag: 8.5 Verification Tag [Normal verification]
2801 * Inputs
2802 * (endpoint, asoc, chunk)
2804 * Outputs
2805 * (asoc, reply_msg, msg_up, timers, counters)
2807 * The return value is the disposition of the chunk.
2809 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2810 const struct sctp_association *asoc,
2811 const sctp_subtype_t type,
2812 void *arg,
2813 sctp_cmd_seq_t *commands)
2815 sctp_ecnehdr_t *ecne;
2816 struct sctp_chunk *chunk = arg;
2818 if (!sctp_vtag_verify(chunk, asoc))
2819 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2821 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2822 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2823 commands);
2825 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2826 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2828 /* If this is a newer ECNE than the last CWR packet we sent out */
2829 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2830 SCTP_U32(ntohl(ecne->lowest_tsn)));
2832 return SCTP_DISPOSITION_CONSUME;
2836 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2838 * The SCTP endpoint MUST always acknowledge the reception of each valid
2839 * DATA chunk.
2841 * The guidelines on delayed acknowledgement algorithm specified in
2842 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2843 * acknowledgement SHOULD be generated for at least every second packet
2844 * (not every second DATA chunk) received, and SHOULD be generated within
2845 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2846 * situations it may be beneficial for an SCTP transmitter to be more
2847 * conservative than the algorithms detailed in this document allow.
2848 * However, an SCTP transmitter MUST NOT be more aggressive than the
2849 * following algorithms allow.
2851 * A SCTP receiver MUST NOT generate more than one SACK for every
2852 * incoming packet, other than to update the offered window as the
2853 * receiving application consumes new data.
2855 * Verification Tag: 8.5 Verification Tag [Normal verification]
2857 * Inputs
2858 * (endpoint, asoc, chunk)
2860 * Outputs
2861 * (asoc, reply_msg, msg_up, timers, counters)
2863 * The return value is the disposition of the chunk.
2865 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2866 const struct sctp_association *asoc,
2867 const sctp_subtype_t type,
2868 void *arg,
2869 sctp_cmd_seq_t *commands)
2871 struct sctp_chunk *chunk = arg;
2872 sctp_arg_t force = SCTP_NOFORCE();
2873 int error;
2875 if (!sctp_vtag_verify(chunk, asoc)) {
2876 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2877 SCTP_NULL());
2878 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2881 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2882 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2883 commands);
2885 error = sctp_eat_data(asoc, chunk, commands );
2886 switch (error) {
2887 case SCTP_IERROR_NO_ERROR:
2888 break;
2889 case SCTP_IERROR_HIGH_TSN:
2890 case SCTP_IERROR_BAD_STREAM:
2891 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2892 goto discard_noforce;
2893 case SCTP_IERROR_DUP_TSN:
2894 case SCTP_IERROR_IGNORE_TSN:
2895 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2896 goto discard_force;
2897 case SCTP_IERROR_NO_DATA:
2898 goto consume;
2899 case SCTP_IERROR_PROTO_VIOLATION:
2900 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2901 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2902 default:
2903 BUG();
2906 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2907 force = SCTP_FORCE();
2909 if (asoc->autoclose) {
2910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2911 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2914 /* If this is the last chunk in a packet, we need to count it
2915 * toward sack generation. Note that we need to SACK every
2916 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2917 * THEM. We elect to NOT generate SACK's if the chunk fails
2918 * the verification tag test.
2920 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2922 * The SCTP endpoint MUST always acknowledge the reception of
2923 * each valid DATA chunk.
2925 * The guidelines on delayed acknowledgement algorithm
2926 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2927 * Specifically, an acknowledgement SHOULD be generated for at
2928 * least every second packet (not every second DATA chunk)
2929 * received, and SHOULD be generated within 200 ms of the
2930 * arrival of any unacknowledged DATA chunk. In some
2931 * situations it may be beneficial for an SCTP transmitter to
2932 * be more conservative than the algorithms detailed in this
2933 * document allow. However, an SCTP transmitter MUST NOT be
2934 * more aggressive than the following algorithms allow.
2936 if (chunk->end_of_packet)
2937 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
2939 return SCTP_DISPOSITION_CONSUME;
2941 discard_force:
2942 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2944 * When a packet arrives with duplicate DATA chunk(s) and with
2945 * no new DATA chunk(s), the endpoint MUST immediately send a
2946 * SACK with no delay. If a packet arrives with duplicate
2947 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2948 * MAY immediately send a SACK. Normally receipt of duplicate
2949 * DATA chunks will occur when the original SACK chunk was lost
2950 * and the peer's RTO has expired. The duplicate TSN number(s)
2951 * SHOULD be reported in the SACK as duplicate.
2953 /* In our case, we split the MAY SACK advice up whether or not
2954 * the last chunk is a duplicate.'
2956 if (chunk->end_of_packet)
2957 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2958 return SCTP_DISPOSITION_DISCARD;
2960 discard_noforce:
2961 if (chunk->end_of_packet)
2962 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
2964 return SCTP_DISPOSITION_DISCARD;
2965 consume:
2966 return SCTP_DISPOSITION_CONSUME;
2971 * sctp_sf_eat_data_fast_4_4
2973 * Section: 4 (4)
2974 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2975 * DATA chunks without delay.
2977 * Verification Tag: 8.5 Verification Tag [Normal verification]
2978 * Inputs
2979 * (endpoint, asoc, chunk)
2981 * Outputs
2982 * (asoc, reply_msg, msg_up, timers, counters)
2984 * The return value is the disposition of the chunk.
2986 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2987 const struct sctp_association *asoc,
2988 const sctp_subtype_t type,
2989 void *arg,
2990 sctp_cmd_seq_t *commands)
2992 struct sctp_chunk *chunk = arg;
2993 int error;
2995 if (!sctp_vtag_verify(chunk, asoc)) {
2996 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2997 SCTP_NULL());
2998 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3001 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3002 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3003 commands);
3005 error = sctp_eat_data(asoc, chunk, commands );
3006 switch (error) {
3007 case SCTP_IERROR_NO_ERROR:
3008 case SCTP_IERROR_HIGH_TSN:
3009 case SCTP_IERROR_DUP_TSN:
3010 case SCTP_IERROR_IGNORE_TSN:
3011 case SCTP_IERROR_BAD_STREAM:
3012 break;
3013 case SCTP_IERROR_NO_DATA:
3014 goto consume;
3015 case SCTP_IERROR_PROTO_VIOLATION:
3016 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3017 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3018 default:
3019 BUG();
3022 /* Go a head and force a SACK, since we are shutting down. */
3024 /* Implementor's Guide.
3026 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3027 * respond to each received packet containing one or more DATA chunk(s)
3028 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3030 if (chunk->end_of_packet) {
3031 /* We must delay the chunk creation since the cumulative
3032 * TSN has not been updated yet.
3034 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3035 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3036 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3037 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3040 consume:
3041 return SCTP_DISPOSITION_CONSUME;
3045 * Section: 6.2 Processing a Received SACK
3046 * D) Any time a SACK arrives, the endpoint performs the following:
3048 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3049 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3050 * increasing, a SACK whose Cumulative TSN Ack is less than the
3051 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3053 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3054 * of bytes still outstanding after processing the Cumulative TSN Ack
3055 * and the Gap Ack Blocks.
3057 * iii) If the SACK is missing a TSN that was previously
3058 * acknowledged via a Gap Ack Block (e.g., the data receiver
3059 * reneged on the data), then mark the corresponding DATA chunk
3060 * as available for retransmit: Mark it as missing for fast
3061 * retransmit as described in Section 7.2.4 and if no retransmit
3062 * timer is running for the destination address to which the DATA
3063 * chunk was originally transmitted, then T3-rtx is started for
3064 * that destination address.
3066 * Verification Tag: 8.5 Verification Tag [Normal verification]
3068 * Inputs
3069 * (endpoint, asoc, chunk)
3071 * Outputs
3072 * (asoc, reply_msg, msg_up, timers, counters)
3074 * The return value is the disposition of the chunk.
3076 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3077 const struct sctp_association *asoc,
3078 const sctp_subtype_t type,
3079 void *arg,
3080 sctp_cmd_seq_t *commands)
3082 struct sctp_chunk *chunk = arg;
3083 sctp_sackhdr_t *sackh;
3084 __u32 ctsn;
3086 if (!sctp_vtag_verify(chunk, asoc))
3087 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3089 /* Make sure that the SACK chunk has a valid length. */
3090 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3091 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3092 commands);
3094 /* Pull the SACK chunk from the data buffer */
3095 sackh = sctp_sm_pull_sack(chunk);
3096 /* Was this a bogus SACK? */
3097 if (!sackh)
3098 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3099 chunk->subh.sack_hdr = sackh;
3100 ctsn = ntohl(sackh->cum_tsn_ack);
3102 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3103 * Ack Point, then drop the SACK. Since Cumulative TSN
3104 * Ack is monotonically increasing, a SACK whose
3105 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3106 * Point indicates an out-of-order SACK.
3108 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3109 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3110 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3111 return SCTP_DISPOSITION_DISCARD;
3114 /* If Cumulative TSN Ack beyond the max tsn currently
3115 * send, terminating the association and respond to the
3116 * sender with an ABORT.
3118 if (!TSN_lt(ctsn, asoc->next_tsn))
3119 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3121 /* Return this SACK for further processing. */
3122 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3124 /* Note: We do the rest of the work on the PROCESS_SACK
3125 * sideeffect.
3127 return SCTP_DISPOSITION_CONSUME;
3131 * Generate an ABORT in response to a packet.
3133 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3135 * 8) The receiver should respond to the sender of the OOTB packet with
3136 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3137 * MUST fill in the Verification Tag field of the outbound packet
3138 * with the value found in the Verification Tag field of the OOTB
3139 * packet and set the T-bit in the Chunk Flags to indicate that the
3140 * Verification Tag is reflected. After sending this ABORT, the
3141 * receiver of the OOTB packet shall discard the OOTB packet and take
3142 * no further action.
3144 * Verification Tag:
3146 * The return value is the disposition of the chunk.
3148 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
3149 const struct sctp_association *asoc,
3150 const sctp_subtype_t type,
3151 void *arg,
3152 sctp_cmd_seq_t *commands)
3154 struct sctp_packet *packet = NULL;
3155 struct sctp_chunk *chunk = arg;
3156 struct sctp_chunk *abort;
3158 packet = sctp_ootb_pkt_new(asoc, chunk);
3160 if (packet) {
3161 /* Make an ABORT. The T bit will be set if the asoc
3162 * is NULL.
3164 abort = sctp_make_abort(asoc, chunk, 0);
3165 if (!abort) {
3166 sctp_ootb_pkt_free(packet);
3167 return SCTP_DISPOSITION_NOMEM;
3170 /* Reflect vtag if T-Bit is set */
3171 if (sctp_test_T_bit(abort))
3172 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3174 /* Set the skb to the belonging sock for accounting. */
3175 abort->skb->sk = ep->base.sk;
3177 sctp_packet_append_chunk(packet, abort);
3179 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3180 SCTP_PACKET(packet));
3182 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3184 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3185 return SCTP_DISPOSITION_CONSUME;
3188 return SCTP_DISPOSITION_NOMEM;
3192 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3193 * event as ULP notification for each cause included in the chunk.
3195 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3197 * The return value is the disposition of the chunk.
3199 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3200 const struct sctp_association *asoc,
3201 const sctp_subtype_t type,
3202 void *arg,
3203 sctp_cmd_seq_t *commands)
3205 struct sctp_chunk *chunk = arg;
3207 if (!sctp_vtag_verify(chunk, asoc))
3208 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3210 /* Make sure that the ERROR chunk has a valid length. */
3211 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3212 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3213 commands);
3215 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3216 SCTP_CHUNK(chunk));
3218 return SCTP_DISPOSITION_CONSUME;
3222 * Process an inbound SHUTDOWN ACK.
3224 * From Section 9.2:
3225 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3226 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3227 * peer, and remove all record of the association.
3229 * The return value is the disposition.
3231 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3232 const struct sctp_association *asoc,
3233 const sctp_subtype_t type,
3234 void *arg,
3235 sctp_cmd_seq_t *commands)
3237 struct sctp_chunk *chunk = arg;
3238 struct sctp_chunk *reply;
3239 struct sctp_ulpevent *ev;
3241 if (!sctp_vtag_verify(chunk, asoc))
3242 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3244 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3245 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3246 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3247 commands);
3248 /* 10.2 H) SHUTDOWN COMPLETE notification
3250 * When SCTP completes the shutdown procedures (section 9.2) this
3251 * notification is passed to the upper layer.
3253 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3254 0, 0, 0, NULL, GFP_ATOMIC);
3255 if (!ev)
3256 goto nomem;
3258 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3259 reply = sctp_make_shutdown_complete(asoc, chunk);
3260 if (!reply)
3261 goto nomem_chunk;
3263 /* Do all the commands now (after allocation), so that we
3264 * have consistent state if memory allocation failes
3266 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3268 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3269 * stop the T2-shutdown timer,
3271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3272 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3274 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3275 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3277 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3278 SCTP_STATE(SCTP_STATE_CLOSED));
3279 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3280 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3281 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3283 /* ...and remove all record of the association. */
3284 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3285 return SCTP_DISPOSITION_DELETE_TCB;
3287 nomem_chunk:
3288 sctp_ulpevent_free(ev);
3289 nomem:
3290 return SCTP_DISPOSITION_NOMEM;
3294 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3296 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3297 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3298 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3299 * packet must fill in the Verification Tag field of the outbound
3300 * packet with the Verification Tag received in the SHUTDOWN ACK and
3301 * set the T-bit in the Chunk Flags to indicate that the Verification
3302 * Tag is reflected.
3304 * 8) The receiver should respond to the sender of the OOTB packet with
3305 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3306 * MUST fill in the Verification Tag field of the outbound packet
3307 * with the value found in the Verification Tag field of the OOTB
3308 * packet and set the T-bit in the Chunk Flags to indicate that the
3309 * Verification Tag is reflected. After sending this ABORT, the
3310 * receiver of the OOTB packet shall discard the OOTB packet and take
3311 * no further action.
3313 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3314 const struct sctp_association *asoc,
3315 const sctp_subtype_t type,
3316 void *arg,
3317 sctp_cmd_seq_t *commands)
3319 struct sctp_chunk *chunk = arg;
3320 struct sk_buff *skb = chunk->skb;
3321 sctp_chunkhdr_t *ch;
3322 __u8 *ch_end;
3323 int ootb_shut_ack = 0;
3325 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3327 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3328 do {
3329 /* Report violation if the chunk is less then minimal */
3330 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3331 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3332 commands);
3334 /* Now that we know we at least have a chunk header,
3335 * do things that are type appropriate.
3337 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3338 ootb_shut_ack = 1;
3340 /* RFC 2960, Section 3.3.7
3341 * Moreover, under any circumstances, an endpoint that
3342 * receives an ABORT MUST NOT respond to that ABORT by
3343 * sending an ABORT of its own.
3345 if (SCTP_CID_ABORT == ch->type)
3346 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3348 /* Report violation if chunk len overflows */
3349 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3350 if (ch_end > skb_tail_pointer(skb))
3351 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3352 commands);
3354 ch = (sctp_chunkhdr_t *) ch_end;
3355 } while (ch_end < skb_tail_pointer(skb));
3357 if (ootb_shut_ack)
3358 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3359 else
3360 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3364 * Handle an "Out of the blue" SHUTDOWN ACK.
3366 * Section: 8.4 5, sctpimpguide 2.41.
3368 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3369 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3370 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3371 * packet must fill in the Verification Tag field of the outbound
3372 * packet with the Verification Tag received in the SHUTDOWN ACK and
3373 * set the T-bit in the Chunk Flags to indicate that the Verification
3374 * Tag is reflected.
3376 * Inputs
3377 * (endpoint, asoc, type, arg, commands)
3379 * Outputs
3380 * (sctp_disposition_t)
3382 * The return value is the disposition of the chunk.
3384 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3385 const struct sctp_association *asoc,
3386 const sctp_subtype_t type,
3387 void *arg,
3388 sctp_cmd_seq_t *commands)
3390 struct sctp_packet *packet = NULL;
3391 struct sctp_chunk *chunk = arg;
3392 struct sctp_chunk *shut;
3394 packet = sctp_ootb_pkt_new(asoc, chunk);
3396 if (packet) {
3397 /* Make an SHUTDOWN_COMPLETE.
3398 * The T bit will be set if the asoc is NULL.
3400 shut = sctp_make_shutdown_complete(asoc, chunk);
3401 if (!shut) {
3402 sctp_ootb_pkt_free(packet);
3403 return SCTP_DISPOSITION_NOMEM;
3406 /* Reflect vtag if T-Bit is set */
3407 if (sctp_test_T_bit(shut))
3408 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3410 /* Set the skb to the belonging sock for accounting. */
3411 shut->skb->sk = ep->base.sk;
3413 sctp_packet_append_chunk(packet, shut);
3415 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3416 SCTP_PACKET(packet));
3418 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3420 /* If the chunk length is invalid, we don't want to process
3421 * the reset of the packet.
3423 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3424 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3426 /* We need to discard the rest of the packet to prevent
3427 * potential bomming attacks from additional bundled chunks.
3428 * This is documented in SCTP Threats ID.
3430 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3433 return SCTP_DISPOSITION_NOMEM;
3437 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3439 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3440 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3441 * procedures in section 8.4 SHOULD be followed, in other words it
3442 * should be treated as an Out Of The Blue packet.
3443 * [This means that we do NOT check the Verification Tag on these
3444 * chunks. --piggy ]
3447 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3448 const struct sctp_association *asoc,
3449 const sctp_subtype_t type,
3450 void *arg,
3451 sctp_cmd_seq_t *commands)
3453 struct sctp_chunk *chunk = arg;
3455 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3456 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3457 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3458 commands);
3460 /* Although we do have an association in this case, it corresponds
3461 * to a restarted association. So the packet is treated as an OOTB
3462 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3463 * called with a NULL association.
3465 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3467 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3470 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3471 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3472 const struct sctp_association *asoc,
3473 const sctp_subtype_t type, void *arg,
3474 sctp_cmd_seq_t *commands)
3476 struct sctp_chunk *chunk = arg;
3477 struct sctp_chunk *asconf_ack = NULL;
3478 struct sctp_paramhdr *err_param = NULL;
3479 sctp_addiphdr_t *hdr;
3480 union sctp_addr_param *addr_param;
3481 __u32 serial;
3482 int length;
3484 if (!sctp_vtag_verify(chunk, asoc)) {
3485 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3486 SCTP_NULL());
3487 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3490 /* ADD-IP: Section 4.1.1
3491 * This chunk MUST be sent in an authenticated way by using
3492 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3493 * is received unauthenticated it MUST be silently discarded as
3494 * described in [I-D.ietf-tsvwg-sctp-auth].
3496 if (!sctp_addip_noauth && !chunk->auth)
3497 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3499 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3500 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3501 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3502 commands);
3504 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3505 serial = ntohl(hdr->serial);
3507 addr_param = (union sctp_addr_param *)hdr->params;
3508 length = ntohs(addr_param->p.length);
3509 if (length < sizeof(sctp_paramhdr_t))
3510 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3511 (void *)addr_param, commands);
3513 /* Verify the ASCONF chunk before processing it. */
3514 if (!sctp_verify_asconf(asoc,
3515 (sctp_paramhdr_t *)((void *)addr_param + length),
3516 (void *)chunk->chunk_end,
3517 &err_param))
3518 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3519 (void *)err_param, commands);
3521 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3522 * the endpoint stored in a new association variable
3523 * 'Peer-Serial-Number'.
3525 if (serial == asoc->peer.addip_serial + 1) {
3526 /* If this is the first instance of ASCONF in the packet,
3527 * we can clean our old ASCONF-ACKs.
3529 if (!chunk->has_asconf)
3530 sctp_assoc_clean_asconf_ack_cache(asoc);
3532 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3533 * expected, process the ASCONF as described below and after
3534 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3535 * the response packet and cache a copy of it (in the event it
3536 * later needs to be retransmitted).
3538 * Essentially, do V1-V5.
3540 asconf_ack = sctp_process_asconf((struct sctp_association *)
3541 asoc, chunk);
3542 if (!asconf_ack)
3543 return SCTP_DISPOSITION_NOMEM;
3544 } else if (serial < asoc->peer.addip_serial + 1) {
3545 /* ADDIP 5.2 E2)
3546 * If the value found in the Sequence Number is less than the
3547 * ('Peer- Sequence-Number' + 1), simply skip to the next
3548 * ASCONF, and include in the outbound response packet
3549 * any previously cached ASCONF-ACK response that was
3550 * sent and saved that matches the Sequence Number of the
3551 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3552 * Chunk exists. This will occur when an older ASCONF
3553 * arrives out of order. In such a case, the receiver
3554 * should skip the ASCONF Chunk and not include ASCONF-ACK
3555 * Chunk for that chunk.
3557 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3558 if (!asconf_ack)
3559 return SCTP_DISPOSITION_DISCARD;
3561 /* Reset the transport so that we select the correct one
3562 * this time around. This is to make sure that we don't
3563 * accidentally use a stale transport that's been removed.
3565 asconf_ack->transport = NULL;
3566 } else {
3567 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3568 * it must be either a stale packet or from an attacker.
3570 return SCTP_DISPOSITION_DISCARD;
3573 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3574 * containing the ASCONF-ACK Chunks MUST be the source address of
3575 * the SCTP packet that held the ASCONF Chunks.
3577 * To do this properly, we'll set the destination address of the chunk
3578 * and at the transmit time, will try look up the transport to use.
3579 * Since ASCONFs may be bundled, the correct transport may not be
3580 * created until we process the entire packet, thus this workaround.
3582 asconf_ack->dest = chunk->source;
3583 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3585 return SCTP_DISPOSITION_CONSUME;
3589 * ADDIP Section 4.3 General rules for address manipulation
3590 * When building TLV parameters for the ASCONF Chunk that will add or
3591 * delete IP addresses the D0 to D13 rules should be applied:
3593 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3594 const struct sctp_association *asoc,
3595 const sctp_subtype_t type, void *arg,
3596 sctp_cmd_seq_t *commands)
3598 struct sctp_chunk *asconf_ack = arg;
3599 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3600 struct sctp_chunk *abort;
3601 struct sctp_paramhdr *err_param = NULL;
3602 sctp_addiphdr_t *addip_hdr;
3603 __u32 sent_serial, rcvd_serial;
3605 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3606 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3607 SCTP_NULL());
3608 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3611 /* ADD-IP, Section 4.1.2:
3612 * This chunk MUST be sent in an authenticated way by using
3613 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3614 * is received unauthenticated it MUST be silently discarded as
3615 * described in [I-D.ietf-tsvwg-sctp-auth].
3617 if (!sctp_addip_noauth && !asconf_ack->auth)
3618 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3620 /* Make sure that the ADDIP chunk has a valid length. */
3621 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3622 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3623 commands);
3625 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3626 rcvd_serial = ntohl(addip_hdr->serial);
3628 /* Verify the ASCONF-ACK chunk before processing it. */
3629 if (!sctp_verify_asconf(asoc,
3630 (sctp_paramhdr_t *)addip_hdr->params,
3631 (void *)asconf_ack->chunk_end,
3632 &err_param))
3633 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3634 (void *)err_param, commands);
3636 if (last_asconf) {
3637 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3638 sent_serial = ntohl(addip_hdr->serial);
3639 } else {
3640 sent_serial = asoc->addip_serial - 1;
3643 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3644 * equal to the next serial number to be used but no ASCONF chunk is
3645 * outstanding the endpoint MUST ABORT the association. Note that a
3646 * sequence number is greater than if it is no more than 2^^31-1
3647 * larger than the current sequence number (using serial arithmetic).
3649 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3650 !(asoc->addip_last_asconf)) {
3651 abort = sctp_make_abort(asoc, asconf_ack,
3652 sizeof(sctp_errhdr_t));
3653 if (abort) {
3654 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3655 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3656 SCTP_CHUNK(abort));
3658 /* We are going to ABORT, so we might as well stop
3659 * processing the rest of the chunks in the packet.
3661 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3662 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3663 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3664 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3665 SCTP_ERROR(ECONNABORTED));
3666 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3667 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3668 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3669 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3670 return SCTP_DISPOSITION_ABORT;
3673 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3674 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3675 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3677 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3678 asconf_ack)) {
3679 /* Successfully processed ASCONF_ACK. We can
3680 * release the next asconf if we have one.
3682 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3683 SCTP_NULL());
3684 return SCTP_DISPOSITION_CONSUME;
3687 abort = sctp_make_abort(asoc, asconf_ack,
3688 sizeof(sctp_errhdr_t));
3689 if (abort) {
3690 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3691 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3692 SCTP_CHUNK(abort));
3694 /* We are going to ABORT, so we might as well stop
3695 * processing the rest of the chunks in the packet.
3697 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3698 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3699 SCTP_ERROR(ECONNABORTED));
3700 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3701 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3702 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3703 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3704 return SCTP_DISPOSITION_ABORT;
3707 return SCTP_DISPOSITION_DISCARD;
3711 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3713 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3714 * its cumulative TSN point to the value carried in the FORWARD TSN
3715 * chunk, and then MUST further advance its cumulative TSN point locally
3716 * if possible.
3717 * After the above processing, the data receiver MUST stop reporting any
3718 * missing TSNs earlier than or equal to the new cumulative TSN point.
3720 * Verification Tag: 8.5 Verification Tag [Normal verification]
3722 * The return value is the disposition of the chunk.
3724 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3725 const struct sctp_association *asoc,
3726 const sctp_subtype_t type,
3727 void *arg,
3728 sctp_cmd_seq_t *commands)
3730 struct sctp_chunk *chunk = arg;
3731 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3732 struct sctp_fwdtsn_skip *skip;
3733 __u16 len;
3734 __u32 tsn;
3736 if (!sctp_vtag_verify(chunk, asoc)) {
3737 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3738 SCTP_NULL());
3739 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3742 /* Make sure that the FORWARD_TSN chunk has valid length. */
3743 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3744 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3745 commands);
3747 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3748 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3749 len = ntohs(chunk->chunk_hdr->length);
3750 len -= sizeof(struct sctp_chunkhdr);
3751 skb_pull(chunk->skb, len);
3753 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3754 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3756 /* The TSN is too high--silently discard the chunk and count on it
3757 * getting retransmitted later.
3759 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3760 goto discard_noforce;
3762 /* Silently discard the chunk if stream-id is not valid */
3763 sctp_walk_fwdtsn(skip, chunk) {
3764 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3765 goto discard_noforce;
3768 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3769 if (len > sizeof(struct sctp_fwdtsn_hdr))
3770 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3771 SCTP_CHUNK(chunk));
3773 /* Count this as receiving DATA. */
3774 if (asoc->autoclose) {
3775 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3776 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3779 /* FIXME: For now send a SACK, but DATA processing may
3780 * send another.
3782 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3784 return SCTP_DISPOSITION_CONSUME;
3786 discard_noforce:
3787 return SCTP_DISPOSITION_DISCARD;
3790 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3791 const struct sctp_endpoint *ep,
3792 const struct sctp_association *asoc,
3793 const sctp_subtype_t type,
3794 void *arg,
3795 sctp_cmd_seq_t *commands)
3797 struct sctp_chunk *chunk = arg;
3798 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3799 struct sctp_fwdtsn_skip *skip;
3800 __u16 len;
3801 __u32 tsn;
3803 if (!sctp_vtag_verify(chunk, asoc)) {
3804 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3805 SCTP_NULL());
3806 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3809 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3810 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3811 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3812 commands);
3814 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3815 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3816 len = ntohs(chunk->chunk_hdr->length);
3817 len -= sizeof(struct sctp_chunkhdr);
3818 skb_pull(chunk->skb, len);
3820 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3821 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3823 /* The TSN is too high--silently discard the chunk and count on it
3824 * getting retransmitted later.
3826 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3827 goto gen_shutdown;
3829 /* Silently discard the chunk if stream-id is not valid */
3830 sctp_walk_fwdtsn(skip, chunk) {
3831 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3832 goto gen_shutdown;
3835 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3836 if (len > sizeof(struct sctp_fwdtsn_hdr))
3837 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3838 SCTP_CHUNK(chunk));
3840 /* Go a head and force a SACK, since we are shutting down. */
3841 gen_shutdown:
3842 /* Implementor's Guide.
3844 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3845 * respond to each received packet containing one or more DATA chunk(s)
3846 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3848 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3849 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3850 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3851 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3853 return SCTP_DISPOSITION_CONSUME;
3857 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3859 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3860 * Identifier field. If this algorithm was not specified by the
3861 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3862 * during association setup, the AUTH chunk and all chunks after it MUST
3863 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3864 * defined in Section 4.1.
3866 * If an endpoint with no shared key receives a Shared Key Identifier
3867 * other than 0, it MUST silently discard all authenticated chunks. If
3868 * the endpoint has at least one endpoint pair shared key for the peer,
3869 * it MUST use the key specified by the Shared Key Identifier if a
3870 * key has been configured for that Shared Key Identifier. If no
3871 * endpoint pair shared key has been configured for that Shared Key
3872 * Identifier, all authenticated chunks MUST be silently discarded.
3874 * Verification Tag: 8.5 Verification Tag [Normal verification]
3876 * The return value is the disposition of the chunk.
3878 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3879 const struct sctp_association *asoc,
3880 const sctp_subtype_t type,
3881 struct sctp_chunk *chunk)
3883 struct sctp_authhdr *auth_hdr;
3884 struct sctp_hmac *hmac;
3885 unsigned int sig_len;
3886 __u16 key_id;
3887 __u8 *save_digest;
3888 __u8 *digest;
3890 /* Pull in the auth header, so we can do some more verification */
3891 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3892 chunk->subh.auth_hdr = auth_hdr;
3893 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3895 /* Make sure that we suport the HMAC algorithm from the auth
3896 * chunk.
3898 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3899 return SCTP_IERROR_AUTH_BAD_HMAC;
3901 /* Make sure that the provided shared key identifier has been
3902 * configured
3904 key_id = ntohs(auth_hdr->shkey_id);
3905 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3906 return SCTP_IERROR_AUTH_BAD_KEYID;
3909 /* Make sure that the length of the signature matches what
3910 * we expect.
3912 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3913 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3914 if (sig_len != hmac->hmac_len)
3915 return SCTP_IERROR_PROTO_VIOLATION;
3917 /* Now that we've done validation checks, we can compute and
3918 * verify the hmac. The steps involved are:
3919 * 1. Save the digest from the chunk.
3920 * 2. Zero out the digest in the chunk.
3921 * 3. Compute the new digest
3922 * 4. Compare saved and new digests.
3924 digest = auth_hdr->hmac;
3925 skb_pull(chunk->skb, sig_len);
3927 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3928 if (!save_digest)
3929 goto nomem;
3931 memset(digest, 0, sig_len);
3933 sctp_auth_calculate_hmac(asoc, chunk->skb,
3934 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3935 GFP_ATOMIC);
3937 /* Discard the packet if the digests do not match */
3938 if (memcmp(save_digest, digest, sig_len)) {
3939 kfree(save_digest);
3940 return SCTP_IERROR_BAD_SIG;
3943 kfree(save_digest);
3944 chunk->auth = 1;
3946 return SCTP_IERROR_NO_ERROR;
3947 nomem:
3948 return SCTP_IERROR_NOMEM;
3951 sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3952 const struct sctp_association *asoc,
3953 const sctp_subtype_t type,
3954 void *arg,
3955 sctp_cmd_seq_t *commands)
3957 struct sctp_authhdr *auth_hdr;
3958 struct sctp_chunk *chunk = arg;
3959 struct sctp_chunk *err_chunk;
3960 sctp_ierror_t error;
3962 /* Make sure that the peer has AUTH capable */
3963 if (!asoc->peer.auth_capable)
3964 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3966 if (!sctp_vtag_verify(chunk, asoc)) {
3967 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3968 SCTP_NULL());
3969 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3972 /* Make sure that the AUTH chunk has valid length. */
3973 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3974 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3975 commands);
3977 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3978 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3979 switch (error) {
3980 case SCTP_IERROR_AUTH_BAD_HMAC:
3981 /* Generate the ERROR chunk and discard the rest
3982 * of the packet
3984 err_chunk = sctp_make_op_error(asoc, chunk,
3985 SCTP_ERROR_UNSUP_HMAC,
3986 &auth_hdr->hmac_id,
3987 sizeof(__u16), 0);
3988 if (err_chunk) {
3989 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3990 SCTP_CHUNK(err_chunk));
3992 /* Fall Through */
3993 case SCTP_IERROR_AUTH_BAD_KEYID:
3994 case SCTP_IERROR_BAD_SIG:
3995 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3996 break;
3997 case SCTP_IERROR_PROTO_VIOLATION:
3998 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3999 commands);
4000 break;
4001 case SCTP_IERROR_NOMEM:
4002 return SCTP_DISPOSITION_NOMEM;
4003 default:
4004 break;
4007 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4008 struct sctp_ulpevent *ev;
4010 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4011 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4013 if (!ev)
4014 return -ENOMEM;
4016 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4017 SCTP_ULPEVENT(ev));
4020 return SCTP_DISPOSITION_CONSUME;
4024 * Process an unknown chunk.
4026 * Section: 3.2. Also, 2.1 in the implementor's guide.
4028 * Chunk Types are encoded such that the highest-order two bits specify
4029 * the action that must be taken if the processing endpoint does not
4030 * recognize the Chunk Type.
4032 * 00 - Stop processing this SCTP packet and discard it, do not process
4033 * any further chunks within it.
4035 * 01 - Stop processing this SCTP packet and discard it, do not process
4036 * any further chunks within it, and report the unrecognized
4037 * chunk in an 'Unrecognized Chunk Type'.
4039 * 10 - Skip this chunk and continue processing.
4041 * 11 - Skip this chunk and continue processing, but report in an ERROR
4042 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4044 * The return value is the disposition of the chunk.
4046 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4047 const struct sctp_association *asoc,
4048 const sctp_subtype_t type,
4049 void *arg,
4050 sctp_cmd_seq_t *commands)
4052 struct sctp_chunk *unk_chunk = arg;
4053 struct sctp_chunk *err_chunk;
4054 sctp_chunkhdr_t *hdr;
4056 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4058 if (!sctp_vtag_verify(unk_chunk, asoc))
4059 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4061 /* Make sure that the chunk has a valid length.
4062 * Since we don't know the chunk type, we use a general
4063 * chunkhdr structure to make a comparison.
4065 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4066 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4067 commands);
4069 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4070 case SCTP_CID_ACTION_DISCARD:
4071 /* Discard the packet. */
4072 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4073 break;
4074 case SCTP_CID_ACTION_DISCARD_ERR:
4075 /* Generate an ERROR chunk as response. */
4076 hdr = unk_chunk->chunk_hdr;
4077 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4078 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4079 WORD_ROUND(ntohs(hdr->length)),
4081 if (err_chunk) {
4082 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4083 SCTP_CHUNK(err_chunk));
4086 /* Discard the packet. */
4087 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4088 return SCTP_DISPOSITION_CONSUME;
4089 break;
4090 case SCTP_CID_ACTION_SKIP:
4091 /* Skip the chunk. */
4092 return SCTP_DISPOSITION_DISCARD;
4093 break;
4094 case SCTP_CID_ACTION_SKIP_ERR:
4095 /* Generate an ERROR chunk as response. */
4096 hdr = unk_chunk->chunk_hdr;
4097 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4098 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4099 WORD_ROUND(ntohs(hdr->length)),
4101 if (err_chunk) {
4102 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4103 SCTP_CHUNK(err_chunk));
4105 /* Skip the chunk. */
4106 return SCTP_DISPOSITION_CONSUME;
4107 break;
4108 default:
4109 break;
4112 return SCTP_DISPOSITION_DISCARD;
4116 * Discard the chunk.
4118 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4119 * [Too numerous to mention...]
4120 * Verification Tag: No verification needed.
4121 * Inputs
4122 * (endpoint, asoc, chunk)
4124 * Outputs
4125 * (asoc, reply_msg, msg_up, timers, counters)
4127 * The return value is the disposition of the chunk.
4129 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4130 const struct sctp_association *asoc,
4131 const sctp_subtype_t type,
4132 void *arg,
4133 sctp_cmd_seq_t *commands)
4135 struct sctp_chunk *chunk = arg;
4137 /* Make sure that the chunk has a valid length.
4138 * Since we don't know the chunk type, we use a general
4139 * chunkhdr structure to make a comparison.
4141 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4142 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4143 commands);
4145 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4146 return SCTP_DISPOSITION_DISCARD;
4150 * Discard the whole packet.
4152 * Section: 8.4 2)
4154 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4155 * silently discard the OOTB packet and take no further action.
4157 * Verification Tag: No verification necessary
4159 * Inputs
4160 * (endpoint, asoc, chunk)
4162 * Outputs
4163 * (asoc, reply_msg, msg_up, timers, counters)
4165 * The return value is the disposition of the chunk.
4167 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4168 const struct sctp_association *asoc,
4169 const sctp_subtype_t type,
4170 void *arg,
4171 sctp_cmd_seq_t *commands)
4173 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
4174 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4176 return SCTP_DISPOSITION_CONSUME;
4181 * The other end is violating protocol.
4183 * Section: Not specified
4184 * Verification Tag: Not specified
4185 * Inputs
4186 * (endpoint, asoc, chunk)
4188 * Outputs
4189 * (asoc, reply_msg, msg_up, timers, counters)
4191 * We simply tag the chunk as a violation. The state machine will log
4192 * the violation and continue.
4194 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4195 const struct sctp_association *asoc,
4196 const sctp_subtype_t type,
4197 void *arg,
4198 sctp_cmd_seq_t *commands)
4200 struct sctp_chunk *chunk = arg;
4202 /* Make sure that the chunk has a valid length. */
4203 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4204 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4205 commands);
4207 return SCTP_DISPOSITION_VIOLATION;
4211 * Common function to handle a protocol violation.
4213 static sctp_disposition_t sctp_sf_abort_violation(
4214 const struct sctp_endpoint *ep,
4215 const struct sctp_association *asoc,
4216 void *arg,
4217 sctp_cmd_seq_t *commands,
4218 const __u8 *payload,
4219 const size_t paylen)
4221 struct sctp_packet *packet = NULL;
4222 struct sctp_chunk *chunk = arg;
4223 struct sctp_chunk *abort = NULL;
4225 /* SCTP-AUTH, Section 6.3:
4226 * It should be noted that if the receiver wants to tear
4227 * down an association in an authenticated way only, the
4228 * handling of malformed packets should not result in
4229 * tearing down the association.
4231 * This means that if we only want to abort associations
4232 * in an authenticated way (i.e AUTH+ABORT), then we
4233 * can't destroy this association just becuase the packet
4234 * was malformed.
4236 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4237 goto discard;
4239 /* Make the abort chunk. */
4240 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4241 if (!abort)
4242 goto nomem;
4244 if (asoc) {
4245 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4246 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4247 !asoc->peer.i.init_tag) {
4248 sctp_initack_chunk_t *initack;
4250 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4251 if (!sctp_chunk_length_valid(chunk,
4252 sizeof(sctp_initack_chunk_t)))
4253 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4254 else {
4255 unsigned int inittag;
4257 inittag = ntohl(initack->init_hdr.init_tag);
4258 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4259 SCTP_U32(inittag));
4263 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4264 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4266 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4268 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4269 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4270 SCTP_ERROR(ECONNREFUSED));
4271 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4272 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4273 } else {
4274 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4275 SCTP_ERROR(ECONNABORTED));
4276 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4277 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4278 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4280 } else {
4281 packet = sctp_ootb_pkt_new(asoc, chunk);
4283 if (!packet)
4284 goto nomem_pkt;
4286 if (sctp_test_T_bit(abort))
4287 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4289 abort->skb->sk = ep->base.sk;
4291 sctp_packet_append_chunk(packet, abort);
4293 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4294 SCTP_PACKET(packet));
4296 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4299 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4301 discard:
4302 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4303 return SCTP_DISPOSITION_ABORT;
4305 nomem_pkt:
4306 sctp_chunk_free(abort);
4307 nomem:
4308 return SCTP_DISPOSITION_NOMEM;
4312 * Handle a protocol violation when the chunk length is invalid.
4313 * "Invalid" length is identified as smaller than the minimal length a
4314 * given chunk can be. For example, a SACK chunk has invalid length
4315 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4317 * We inform the other end by sending an ABORT with a Protocol Violation
4318 * error code.
4320 * Section: Not specified
4321 * Verification Tag: Nothing to do
4322 * Inputs
4323 * (endpoint, asoc, chunk)
4325 * Outputs
4326 * (reply_msg, msg_up, counters)
4328 * Generate an ABORT chunk and terminate the association.
4330 static sctp_disposition_t sctp_sf_violation_chunklen(
4331 const struct sctp_endpoint *ep,
4332 const struct sctp_association *asoc,
4333 const sctp_subtype_t type,
4334 void *arg,
4335 sctp_cmd_seq_t *commands)
4337 static const char err_str[]="The following chunk had invalid length:";
4339 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4340 sizeof(err_str));
4344 * Handle a protocol violation when the parameter length is invalid.
4345 * "Invalid" length is identified as smaller than the minimal length a
4346 * given parameter can be.
4348 static sctp_disposition_t sctp_sf_violation_paramlen(
4349 const struct sctp_endpoint *ep,
4350 const struct sctp_association *asoc,
4351 const sctp_subtype_t type,
4352 void *arg, void *ext,
4353 sctp_cmd_seq_t *commands)
4355 struct sctp_chunk *chunk = arg;
4356 struct sctp_paramhdr *param = ext;
4357 struct sctp_chunk *abort = NULL;
4359 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4360 goto discard;
4362 /* Make the abort chunk. */
4363 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4364 if (!abort)
4365 goto nomem;
4367 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4368 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4370 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4371 SCTP_ERROR(ECONNABORTED));
4372 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4373 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4374 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4375 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4377 discard:
4378 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4379 return SCTP_DISPOSITION_ABORT;
4380 nomem:
4381 return SCTP_DISPOSITION_NOMEM;
4384 /* Handle a protocol violation when the peer trying to advance the
4385 * cumulative tsn ack to a point beyond the max tsn currently sent.
4387 * We inform the other end by sending an ABORT with a Protocol Violation
4388 * error code.
4390 static sctp_disposition_t sctp_sf_violation_ctsn(
4391 const struct sctp_endpoint *ep,
4392 const struct sctp_association *asoc,
4393 const sctp_subtype_t type,
4394 void *arg,
4395 sctp_cmd_seq_t *commands)
4397 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4399 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4400 sizeof(err_str));
4403 /* Handle protocol violation of an invalid chunk bundling. For example,
4404 * when we have an association and we recieve bundled INIT-ACK, or
4405 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4406 * statement from the specs. Additinally, there might be an attacker
4407 * on the path and we may not want to continue this communication.
4409 static sctp_disposition_t sctp_sf_violation_chunk(
4410 const struct sctp_endpoint *ep,
4411 const struct sctp_association *asoc,
4412 const sctp_subtype_t type,
4413 void *arg,
4414 sctp_cmd_seq_t *commands)
4416 static const char err_str[]="The following chunk violates protocol:";
4418 if (!asoc)
4419 return sctp_sf_violation(ep, asoc, type, arg, commands);
4421 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4422 sizeof(err_str));
4424 /***************************************************************************
4425 * These are the state functions for handling primitive (Section 10) events.
4426 ***************************************************************************/
4428 * sctp_sf_do_prm_asoc
4430 * Section: 10.1 ULP-to-SCTP
4431 * B) Associate
4433 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4434 * outbound stream count)
4435 * -> association id [,destination transport addr list] [,outbound stream
4436 * count]
4438 * This primitive allows the upper layer to initiate an association to a
4439 * specific peer endpoint.
4441 * The peer endpoint shall be specified by one of the transport addresses
4442 * which defines the endpoint (see Section 1.4). If the local SCTP
4443 * instance has not been initialized, the ASSOCIATE is considered an
4444 * error.
4445 * [This is not relevant for the kernel implementation since we do all
4446 * initialization at boot time. It we hadn't initialized we wouldn't
4447 * get anywhere near this code.]
4449 * An association id, which is a local handle to the SCTP association,
4450 * will be returned on successful establishment of the association. If
4451 * SCTP is not able to open an SCTP association with the peer endpoint,
4452 * an error is returned.
4453 * [In the kernel implementation, the struct sctp_association needs to
4454 * be created BEFORE causing this primitive to run.]
4456 * Other association parameters may be returned, including the
4457 * complete destination transport addresses of the peer as well as the
4458 * outbound stream count of the local endpoint. One of the transport
4459 * address from the returned destination addresses will be selected by
4460 * the local endpoint as default primary path for sending SCTP packets
4461 * to this peer. The returned "destination transport addr list" can
4462 * be used by the ULP to change the default primary path or to force
4463 * sending a packet to a specific transport address. [All of this
4464 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4465 * function.]
4467 * Mandatory attributes:
4469 * o local SCTP instance name - obtained from the INITIALIZE operation.
4470 * [This is the argument asoc.]
4471 * o destination transport addr - specified as one of the transport
4472 * addresses of the peer endpoint with which the association is to be
4473 * established.
4474 * [This is asoc->peer.active_path.]
4475 * o outbound stream count - the number of outbound streams the ULP
4476 * would like to open towards this peer endpoint.
4477 * [BUG: This is not currently implemented.]
4478 * Optional attributes:
4480 * None.
4482 * The return value is a disposition.
4484 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4485 const struct sctp_association *asoc,
4486 const sctp_subtype_t type,
4487 void *arg,
4488 sctp_cmd_seq_t *commands)
4490 struct sctp_chunk *repl;
4491 struct sctp_association* my_asoc;
4493 /* The comment below says that we enter COOKIE-WAIT AFTER
4494 * sending the INIT, but that doesn't actually work in our
4495 * implementation...
4497 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4498 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4500 /* RFC 2960 5.1 Normal Establishment of an Association
4502 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4503 * must provide its Verification Tag (Tag_A) in the Initiate
4504 * Tag field. Tag_A SHOULD be a random number in the range of
4505 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4508 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4509 if (!repl)
4510 goto nomem;
4512 /* Cast away the const modifier, as we want to just
4513 * rerun it through as a sideffect.
4515 my_asoc = (struct sctp_association *)asoc;
4516 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4518 /* Choose transport for INIT. */
4519 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4520 SCTP_CHUNK(repl));
4522 /* After sending the INIT, "A" starts the T1-init timer and
4523 * enters the COOKIE-WAIT state.
4525 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4526 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4527 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4528 return SCTP_DISPOSITION_CONSUME;
4530 nomem:
4531 return SCTP_DISPOSITION_NOMEM;
4535 * Process the SEND primitive.
4537 * Section: 10.1 ULP-to-SCTP
4538 * E) Send
4540 * Format: SEND(association id, buffer address, byte count [,context]
4541 * [,stream id] [,life time] [,destination transport address]
4542 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4543 * -> result
4545 * This is the main method to send user data via SCTP.
4547 * Mandatory attributes:
4549 * o association id - local handle to the SCTP association
4551 * o buffer address - the location where the user message to be
4552 * transmitted is stored;
4554 * o byte count - The size of the user data in number of bytes;
4556 * Optional attributes:
4558 * o context - an optional 32 bit integer that will be carried in the
4559 * sending failure notification to the ULP if the transportation of
4560 * this User Message fails.
4562 * o stream id - to indicate which stream to send the data on. If not
4563 * specified, stream 0 will be used.
4565 * o life time - specifies the life time of the user data. The user data
4566 * will not be sent by SCTP after the life time expires. This
4567 * parameter can be used to avoid efforts to transmit stale
4568 * user messages. SCTP notifies the ULP if the data cannot be
4569 * initiated to transport (i.e. sent to the destination via SCTP's
4570 * send primitive) within the life time variable. However, the
4571 * user data will be transmitted if SCTP has attempted to transmit a
4572 * chunk before the life time expired.
4574 * o destination transport address - specified as one of the destination
4575 * transport addresses of the peer endpoint to which this packet
4576 * should be sent. Whenever possible, SCTP should use this destination
4577 * transport address for sending the packets, instead of the current
4578 * primary path.
4580 * o unorder flag - this flag, if present, indicates that the user
4581 * would like the data delivered in an unordered fashion to the peer
4582 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4583 * message).
4585 * o no-bundle flag - instructs SCTP not to bundle this user data with
4586 * other outbound DATA chunks. SCTP MAY still bundle even when
4587 * this flag is present, when faced with network congestion.
4589 * o payload protocol-id - A 32 bit unsigned integer that is to be
4590 * passed to the peer indicating the type of payload protocol data
4591 * being transmitted. This value is passed as opaque data by SCTP.
4593 * The return value is the disposition.
4595 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4596 const struct sctp_association *asoc,
4597 const sctp_subtype_t type,
4598 void *arg,
4599 sctp_cmd_seq_t *commands)
4601 struct sctp_datamsg *msg = arg;
4603 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4604 return SCTP_DISPOSITION_CONSUME;
4608 * Process the SHUTDOWN primitive.
4610 * Section: 10.1:
4611 * C) Shutdown
4613 * Format: SHUTDOWN(association id)
4614 * -> result
4616 * Gracefully closes an association. Any locally queued user data
4617 * will be delivered to the peer. The association will be terminated only
4618 * after the peer acknowledges all the SCTP packets sent. A success code
4619 * will be returned on successful termination of the association. If
4620 * attempting to terminate the association results in a failure, an error
4621 * code shall be returned.
4623 * Mandatory attributes:
4625 * o association id - local handle to the SCTP association
4627 * Optional attributes:
4629 * None.
4631 * The return value is the disposition.
4633 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4634 const struct sctp_endpoint *ep,
4635 const struct sctp_association *asoc,
4636 const sctp_subtype_t type,
4637 void *arg,
4638 sctp_cmd_seq_t *commands)
4640 int disposition;
4642 /* From 9.2 Shutdown of an Association
4643 * Upon receipt of the SHUTDOWN primitive from its upper
4644 * layer, the endpoint enters SHUTDOWN-PENDING state and
4645 * remains there until all outstanding data has been
4646 * acknowledged by its peer. The endpoint accepts no new data
4647 * from its upper layer, but retransmits data to the far end
4648 * if necessary to fill gaps.
4650 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4651 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4653 disposition = SCTP_DISPOSITION_CONSUME;
4654 if (sctp_outq_is_empty(&asoc->outqueue)) {
4655 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4656 arg, commands);
4658 return disposition;
4662 * Process the ABORT primitive.
4664 * Section: 10.1:
4665 * C) Abort
4667 * Format: Abort(association id [, cause code])
4668 * -> result
4670 * Ungracefully closes an association. Any locally queued user data
4671 * will be discarded and an ABORT chunk is sent to the peer. A success code
4672 * will be returned on successful abortion of the association. If
4673 * attempting to abort the association results in a failure, an error
4674 * code shall be returned.
4676 * Mandatory attributes:
4678 * o association id - local handle to the SCTP association
4680 * Optional attributes:
4682 * o cause code - reason of the abort to be passed to the peer
4684 * None.
4686 * The return value is the disposition.
4688 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4689 const struct sctp_endpoint *ep,
4690 const struct sctp_association *asoc,
4691 const sctp_subtype_t type,
4692 void *arg,
4693 sctp_cmd_seq_t *commands)
4695 /* From 9.1 Abort of an Association
4696 * Upon receipt of the ABORT primitive from its upper
4697 * layer, the endpoint enters CLOSED state and
4698 * discard all outstanding data has been
4699 * acknowledged by its peer. The endpoint accepts no new data
4700 * from its upper layer, but retransmits data to the far end
4701 * if necessary to fill gaps.
4703 struct sctp_chunk *abort = arg;
4704 sctp_disposition_t retval;
4706 retval = SCTP_DISPOSITION_CONSUME;
4708 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4710 /* Even if we can't send the ABORT due to low memory delete the
4711 * TCB. This is a departure from our typical NOMEM handling.
4714 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4715 SCTP_ERROR(ECONNABORTED));
4716 /* Delete the established association. */
4717 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4718 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4720 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4721 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4723 return retval;
4726 /* We tried an illegal operation on an association which is closed. */
4727 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4728 const struct sctp_association *asoc,
4729 const sctp_subtype_t type,
4730 void *arg,
4731 sctp_cmd_seq_t *commands)
4733 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4734 return SCTP_DISPOSITION_CONSUME;
4737 /* We tried an illegal operation on an association which is shutting
4738 * down.
4740 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4741 const struct sctp_association *asoc,
4742 const sctp_subtype_t type,
4743 void *arg,
4744 sctp_cmd_seq_t *commands)
4746 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4747 SCTP_ERROR(-ESHUTDOWN));
4748 return SCTP_DISPOSITION_CONSUME;
4752 * sctp_cookie_wait_prm_shutdown
4754 * Section: 4 Note: 2
4755 * Verification Tag:
4756 * Inputs
4757 * (endpoint, asoc)
4759 * The RFC does not explicitly address this issue, but is the route through the
4760 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4762 * Outputs
4763 * (timers)
4765 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4766 const struct sctp_endpoint *ep,
4767 const struct sctp_association *asoc,
4768 const sctp_subtype_t type,
4769 void *arg,
4770 sctp_cmd_seq_t *commands)
4772 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4773 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4775 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4776 SCTP_STATE(SCTP_STATE_CLOSED));
4778 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4780 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4782 return SCTP_DISPOSITION_DELETE_TCB;
4786 * sctp_cookie_echoed_prm_shutdown
4788 * Section: 4 Note: 2
4789 * Verification Tag:
4790 * Inputs
4791 * (endpoint, asoc)
4793 * The RFC does not explcitly address this issue, but is the route through the
4794 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4796 * Outputs
4797 * (timers)
4799 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4800 const struct sctp_endpoint *ep,
4801 const struct sctp_association *asoc,
4802 const sctp_subtype_t type,
4803 void *arg, sctp_cmd_seq_t *commands)
4805 /* There is a single T1 timer, so we should be able to use
4806 * common function with the COOKIE-WAIT state.
4808 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4812 * sctp_sf_cookie_wait_prm_abort
4814 * Section: 4 Note: 2
4815 * Verification Tag:
4816 * Inputs
4817 * (endpoint, asoc)
4819 * The RFC does not explicitly address this issue, but is the route through the
4820 * state table when someone issues an abort while in COOKIE_WAIT state.
4822 * Outputs
4823 * (timers)
4825 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4826 const struct sctp_endpoint *ep,
4827 const struct sctp_association *asoc,
4828 const sctp_subtype_t type,
4829 void *arg,
4830 sctp_cmd_seq_t *commands)
4832 struct sctp_chunk *abort = arg;
4833 sctp_disposition_t retval;
4835 /* Stop T1-init timer */
4836 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4837 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4838 retval = SCTP_DISPOSITION_CONSUME;
4840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4842 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4843 SCTP_STATE(SCTP_STATE_CLOSED));
4845 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4847 /* Even if we can't send the ABORT due to low memory delete the
4848 * TCB. This is a departure from our typical NOMEM handling.
4851 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4852 SCTP_ERROR(ECONNREFUSED));
4853 /* Delete the established association. */
4854 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4855 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4857 return retval;
4861 * sctp_sf_cookie_echoed_prm_abort
4863 * Section: 4 Note: 3
4864 * Verification Tag:
4865 * Inputs
4866 * (endpoint, asoc)
4868 * The RFC does not explcitly address this issue, but is the route through the
4869 * state table when someone issues an abort while in COOKIE_ECHOED state.
4871 * Outputs
4872 * (timers)
4874 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4875 const struct sctp_endpoint *ep,
4876 const struct sctp_association *asoc,
4877 const sctp_subtype_t type,
4878 void *arg,
4879 sctp_cmd_seq_t *commands)
4881 /* There is a single T1 timer, so we should be able to use
4882 * common function with the COOKIE-WAIT state.
4884 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4888 * sctp_sf_shutdown_pending_prm_abort
4890 * Inputs
4891 * (endpoint, asoc)
4893 * The RFC does not explicitly address this issue, but is the route through the
4894 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4896 * Outputs
4897 * (timers)
4899 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4900 const struct sctp_endpoint *ep,
4901 const struct sctp_association *asoc,
4902 const sctp_subtype_t type,
4903 void *arg,
4904 sctp_cmd_seq_t *commands)
4906 /* Stop the T5-shutdown guard timer. */
4907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4908 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4910 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4914 * sctp_sf_shutdown_sent_prm_abort
4916 * Inputs
4917 * (endpoint, asoc)
4919 * The RFC does not explicitly address this issue, but is the route through the
4920 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4922 * Outputs
4923 * (timers)
4925 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4926 const struct sctp_endpoint *ep,
4927 const struct sctp_association *asoc,
4928 const sctp_subtype_t type,
4929 void *arg,
4930 sctp_cmd_seq_t *commands)
4932 /* Stop the T2-shutdown timer. */
4933 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4934 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4936 /* Stop the T5-shutdown guard timer. */
4937 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4938 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4940 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4944 * sctp_sf_cookie_echoed_prm_abort
4946 * Inputs
4947 * (endpoint, asoc)
4949 * The RFC does not explcitly address this issue, but is the route through the
4950 * state table when someone issues an abort while in COOKIE_ECHOED state.
4952 * Outputs
4953 * (timers)
4955 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4956 const struct sctp_endpoint *ep,
4957 const struct sctp_association *asoc,
4958 const sctp_subtype_t type,
4959 void *arg,
4960 sctp_cmd_seq_t *commands)
4962 /* The same T2 timer, so we should be able to use
4963 * common function with the SHUTDOWN-SENT state.
4965 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4969 * Process the REQUESTHEARTBEAT primitive
4971 * 10.1 ULP-to-SCTP
4972 * J) Request Heartbeat
4974 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4976 * -> result
4978 * Instructs the local endpoint to perform a HeartBeat on the specified
4979 * destination transport address of the given association. The returned
4980 * result should indicate whether the transmission of the HEARTBEAT
4981 * chunk to the destination address is successful.
4983 * Mandatory attributes:
4985 * o association id - local handle to the SCTP association
4987 * o destination transport address - the transport address of the
4988 * association on which a heartbeat should be issued.
4990 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4991 const struct sctp_endpoint *ep,
4992 const struct sctp_association *asoc,
4993 const sctp_subtype_t type,
4994 void *arg,
4995 sctp_cmd_seq_t *commands)
4997 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4998 (struct sctp_transport *)arg, commands))
4999 return SCTP_DISPOSITION_NOMEM;
5002 * RFC 2960 (bis), section 8.3
5004 * D) Request an on-demand HEARTBEAT on a specific destination
5005 * transport address of a given association.
5007 * The endpoint should increment the respective error counter of
5008 * the destination transport address each time a HEARTBEAT is sent
5009 * to that address and not acknowledged within one RTO.
5012 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5013 SCTP_TRANSPORT(arg));
5014 return SCTP_DISPOSITION_CONSUME;
5018 * ADDIP Section 4.1 ASCONF Chunk Procedures
5019 * When an endpoint has an ASCONF signaled change to be sent to the
5020 * remote endpoint it should do A1 to A9
5022 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5023 const struct sctp_association *asoc,
5024 const sctp_subtype_t type,
5025 void *arg,
5026 sctp_cmd_seq_t *commands)
5028 struct sctp_chunk *chunk = arg;
5030 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5031 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5032 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5033 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5034 return SCTP_DISPOSITION_CONSUME;
5038 * Ignore the primitive event
5040 * The return value is the disposition of the primitive.
5042 sctp_disposition_t sctp_sf_ignore_primitive(
5043 const struct sctp_endpoint *ep,
5044 const struct sctp_association *asoc,
5045 const sctp_subtype_t type,
5046 void *arg,
5047 sctp_cmd_seq_t *commands)
5049 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5050 return SCTP_DISPOSITION_DISCARD;
5053 /***************************************************************************
5054 * These are the state functions for the OTHER events.
5055 ***************************************************************************/
5058 * Start the shutdown negotiation.
5060 * From Section 9.2:
5061 * Once all its outstanding data has been acknowledged, the endpoint
5062 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5063 * TSN Ack field the last sequential TSN it has received from the peer.
5064 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5065 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5066 * with the updated last sequential TSN received from its peer.
5068 * The return value is the disposition.
5070 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5071 const struct sctp_endpoint *ep,
5072 const struct sctp_association *asoc,
5073 const sctp_subtype_t type,
5074 void *arg,
5075 sctp_cmd_seq_t *commands)
5077 struct sctp_chunk *reply;
5079 /* Once all its outstanding data has been acknowledged, the
5080 * endpoint shall send a SHUTDOWN chunk to its peer including
5081 * in the Cumulative TSN Ack field the last sequential TSN it
5082 * has received from the peer.
5084 reply = sctp_make_shutdown(asoc, NULL);
5085 if (!reply)
5086 goto nomem;
5088 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5089 * T2-shutdown timer.
5091 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5093 /* It shall then start the T2-shutdown timer */
5094 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5095 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5097 /* RFC 4960 Section 9.2
5098 * The sender of the SHUTDOWN MAY also start an overall guard timer
5099 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5101 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5102 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5104 if (asoc->autoclose)
5105 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5106 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5108 /* and enter the SHUTDOWN-SENT state. */
5109 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5110 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5112 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5114 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5115 * or SHUTDOWN-ACK.
5117 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5119 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5121 return SCTP_DISPOSITION_CONSUME;
5123 nomem:
5124 return SCTP_DISPOSITION_NOMEM;
5128 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5130 * From Section 9.2:
5132 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5133 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5134 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5135 * endpoint must re-send the SHUTDOWN ACK.
5137 * The return value is the disposition.
5139 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5140 const struct sctp_endpoint *ep,
5141 const struct sctp_association *asoc,
5142 const sctp_subtype_t type,
5143 void *arg,
5144 sctp_cmd_seq_t *commands)
5146 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5147 struct sctp_chunk *reply;
5149 /* There are 2 ways of getting here:
5150 * 1) called in response to a SHUTDOWN chunk
5151 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5153 * For the case (2), the arg parameter is set to NULL. We need
5154 * to check that we have a chunk before accessing it's fields.
5156 if (chunk) {
5157 if (!sctp_vtag_verify(chunk, asoc))
5158 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5160 /* Make sure that the SHUTDOWN chunk has a valid length. */
5161 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5162 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5163 commands);
5166 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5167 * shall send a SHUTDOWN ACK ...
5169 reply = sctp_make_shutdown_ack(asoc, chunk);
5170 if (!reply)
5171 goto nomem;
5173 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5174 * the T2-shutdown timer.
5176 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5178 /* and start/restart a T2-shutdown timer of its own, */
5179 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5180 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5182 if (asoc->autoclose)
5183 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5184 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5186 /* Enter the SHUTDOWN-ACK-SENT state. */
5187 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5188 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5190 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5192 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5193 * or SHUTDOWN-ACK.
5195 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5197 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5199 return SCTP_DISPOSITION_CONSUME;
5201 nomem:
5202 return SCTP_DISPOSITION_NOMEM;
5206 * Ignore the event defined as other
5208 * The return value is the disposition of the event.
5210 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5211 const struct sctp_association *asoc,
5212 const sctp_subtype_t type,
5213 void *arg,
5214 sctp_cmd_seq_t *commands)
5216 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5217 return SCTP_DISPOSITION_DISCARD;
5220 /************************************************************
5221 * These are the state functions for handling timeout events.
5222 ************************************************************/
5225 * RTX Timeout
5227 * Section: 6.3.3 Handle T3-rtx Expiration
5229 * Whenever the retransmission timer T3-rtx expires for a destination
5230 * address, do the following:
5231 * [See below]
5233 * The return value is the disposition of the chunk.
5235 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5236 const struct sctp_association *asoc,
5237 const sctp_subtype_t type,
5238 void *arg,
5239 sctp_cmd_seq_t *commands)
5241 struct sctp_transport *transport = arg;
5243 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5245 if (asoc->overall_error_count >= asoc->max_retrans) {
5246 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5247 SCTP_ERROR(ETIMEDOUT));
5248 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5249 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5250 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5251 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5252 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5253 return SCTP_DISPOSITION_DELETE_TCB;
5256 /* E1) For the destination address for which the timer
5257 * expires, adjust its ssthresh with rules defined in Section
5258 * 7.2.3 and set the cwnd <- MTU.
5261 /* E2) For the destination address for which the timer
5262 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5263 * maximum value discussed in rule C7 above (RTO.max) may be
5264 * used to provide an upper bound to this doubling operation.
5267 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5268 * outstanding DATA chunks for the address for which the
5269 * T3-rtx has expired will fit into a single packet, subject
5270 * to the MTU constraint for the path corresponding to the
5271 * destination transport address to which the retransmission
5272 * is being sent (this may be different from the address for
5273 * which the timer expires [see Section 6.4]). Call this
5274 * value K. Bundle and retransmit those K DATA chunks in a
5275 * single packet to the destination endpoint.
5277 * Note: Any DATA chunks that were sent to the address for
5278 * which the T3-rtx timer expired but did not fit in one MTU
5279 * (rule E3 above), should be marked for retransmission and
5280 * sent as soon as cwnd allows (normally when a SACK arrives).
5283 /* Do some failure management (Section 8.2). */
5284 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5286 /* NB: Rules E4 and F1 are implicit in R1. */
5287 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5289 return SCTP_DISPOSITION_CONSUME;
5293 * Generate delayed SACK on timeout
5295 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5297 * The guidelines on delayed acknowledgement algorithm specified in
5298 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5299 * acknowledgement SHOULD be generated for at least every second packet
5300 * (not every second DATA chunk) received, and SHOULD be generated
5301 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5302 * some situations it may be beneficial for an SCTP transmitter to be
5303 * more conservative than the algorithms detailed in this document
5304 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5305 * the following algorithms allow.
5307 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5308 const struct sctp_association *asoc,
5309 const sctp_subtype_t type,
5310 void *arg,
5311 sctp_cmd_seq_t *commands)
5313 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
5314 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5315 return SCTP_DISPOSITION_CONSUME;
5319 * sctp_sf_t1_init_timer_expire
5321 * Section: 4 Note: 2
5322 * Verification Tag:
5323 * Inputs
5324 * (endpoint, asoc)
5326 * RFC 2960 Section 4 Notes
5327 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5328 * and re-start the T1-init timer without changing state. This MUST
5329 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5330 * endpoint MUST abort the initialization process and report the
5331 * error to SCTP user.
5333 * Outputs
5334 * (timers, events)
5337 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5338 const struct sctp_association *asoc,
5339 const sctp_subtype_t type,
5340 void *arg,
5341 sctp_cmd_seq_t *commands)
5343 struct sctp_chunk *repl = NULL;
5344 struct sctp_bind_addr *bp;
5345 int attempts = asoc->init_err_counter + 1;
5347 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5348 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
5350 if (attempts <= asoc->max_init_attempts) {
5351 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5352 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5353 if (!repl)
5354 return SCTP_DISPOSITION_NOMEM;
5356 /* Choose transport for INIT. */
5357 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5358 SCTP_CHUNK(repl));
5360 /* Issue a sideeffect to do the needed accounting. */
5361 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5362 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5364 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5365 } else {
5366 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5367 " max_init_attempts: %d\n",
5368 attempts, asoc->max_init_attempts);
5369 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5370 SCTP_ERROR(ETIMEDOUT));
5371 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5372 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5373 return SCTP_DISPOSITION_DELETE_TCB;
5376 return SCTP_DISPOSITION_CONSUME;
5380 * sctp_sf_t1_cookie_timer_expire
5382 * Section: 4 Note: 2
5383 * Verification Tag:
5384 * Inputs
5385 * (endpoint, asoc)
5387 * RFC 2960 Section 4 Notes
5388 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5389 * COOKIE ECHO and re-start the T1-cookie timer without changing
5390 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5391 * After that, the endpoint MUST abort the initialization process and
5392 * report the error to SCTP user.
5394 * Outputs
5395 * (timers, events)
5398 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
5399 const struct sctp_association *asoc,
5400 const sctp_subtype_t type,
5401 void *arg,
5402 sctp_cmd_seq_t *commands)
5404 struct sctp_chunk *repl = NULL;
5405 int attempts = asoc->init_err_counter + 1;
5407 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5408 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
5410 if (attempts <= asoc->max_init_attempts) {
5411 repl = sctp_make_cookie_echo(asoc, NULL);
5412 if (!repl)
5413 return SCTP_DISPOSITION_NOMEM;
5415 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5416 SCTP_CHUNK(repl));
5417 /* Issue a sideeffect to do the needed accounting. */
5418 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5419 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5421 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5422 } else {
5423 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5424 SCTP_ERROR(ETIMEDOUT));
5425 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5426 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5427 return SCTP_DISPOSITION_DELETE_TCB;
5430 return SCTP_DISPOSITION_CONSUME;
5433 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5434 * with the updated last sequential TSN received from its peer.
5436 * An endpoint should limit the number of retransmissions of the
5437 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5438 * If this threshold is exceeded the endpoint should destroy the TCB and
5439 * MUST report the peer endpoint unreachable to the upper layer (and
5440 * thus the association enters the CLOSED state). The reception of any
5441 * packet from its peer (i.e. as the peer sends all of its queued DATA
5442 * chunks) should clear the endpoint's retransmission count and restart
5443 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5444 * all of its queued DATA chunks that have not yet been sent.
5446 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5447 const struct sctp_association *asoc,
5448 const sctp_subtype_t type,
5449 void *arg,
5450 sctp_cmd_seq_t *commands)
5452 struct sctp_chunk *reply = NULL;
5454 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5455 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5457 ((struct sctp_association *)asoc)->shutdown_retries++;
5459 if (asoc->overall_error_count >= asoc->max_retrans) {
5460 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5461 SCTP_ERROR(ETIMEDOUT));
5462 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5463 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5464 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5465 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5466 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5467 return SCTP_DISPOSITION_DELETE_TCB;
5470 switch (asoc->state) {
5471 case SCTP_STATE_SHUTDOWN_SENT:
5472 reply = sctp_make_shutdown(asoc, NULL);
5473 break;
5475 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5476 reply = sctp_make_shutdown_ack(asoc, NULL);
5477 break;
5479 default:
5480 BUG();
5481 break;
5484 if (!reply)
5485 goto nomem;
5487 /* Do some failure management (Section 8.2).
5488 * If we remove the transport an SHUTDOWN was last sent to, don't
5489 * do failure management.
5491 if (asoc->shutdown_last_sent_to)
5492 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5493 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5495 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5496 * the T2-shutdown timer.
5498 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5500 /* Restart the T2-shutdown timer. */
5501 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5502 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5503 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5504 return SCTP_DISPOSITION_CONSUME;
5506 nomem:
5507 return SCTP_DISPOSITION_NOMEM;
5511 * ADDIP Section 4.1 ASCONF CHunk Procedures
5512 * If the T4 RTO timer expires the endpoint should do B1 to B5
5514 sctp_disposition_t sctp_sf_t4_timer_expire(
5515 const struct sctp_endpoint *ep,
5516 const struct sctp_association *asoc,
5517 const sctp_subtype_t type,
5518 void *arg,
5519 sctp_cmd_seq_t *commands)
5521 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5522 struct sctp_transport *transport = chunk->transport;
5524 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5526 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5527 * detection on the appropriate destination address as defined in
5528 * RFC2960 [5] section 8.1 and 8.2.
5530 if (transport)
5531 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5532 SCTP_TRANSPORT(transport));
5534 /* Reconfig T4 timer and transport. */
5535 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5537 /* ADDIP 4.1 B2) Increment the association error counters and perform
5538 * endpoint failure detection on the association as defined in
5539 * RFC2960 [5] section 8.1 and 8.2.
5540 * association error counter is incremented in SCTP_CMD_STRIKE.
5542 if (asoc->overall_error_count >= asoc->max_retrans) {
5543 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5544 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5545 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5546 SCTP_ERROR(ETIMEDOUT));
5547 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5548 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5549 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5550 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5551 return SCTP_DISPOSITION_ABORT;
5554 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5555 * the ASCONF chunk was sent by doubling the RTO timer value.
5556 * This is done in SCTP_CMD_STRIKE.
5559 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5560 * choose an alternate destination address (please refer to RFC2960
5561 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5562 * chunk, it MUST be the same (including its serial number) as the last
5563 * ASCONF sent.
5565 sctp_chunk_hold(asoc->addip_last_asconf);
5566 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5567 SCTP_CHUNK(asoc->addip_last_asconf));
5569 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5570 * destination is selected, then the RTO used will be that of the new
5571 * destination address.
5573 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5574 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5576 return SCTP_DISPOSITION_CONSUME;
5579 /* sctpimpguide-05 Section 2.12.2
5580 * The sender of the SHUTDOWN MAY also start an overall guard timer
5581 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5582 * At the expiration of this timer the sender SHOULD abort the association
5583 * by sending an ABORT chunk.
5585 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5586 const struct sctp_association *asoc,
5587 const sctp_subtype_t type,
5588 void *arg,
5589 sctp_cmd_seq_t *commands)
5591 struct sctp_chunk *reply = NULL;
5593 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5594 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5596 reply = sctp_make_abort(asoc, NULL, 0);
5597 if (!reply)
5598 goto nomem;
5600 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5601 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5602 SCTP_ERROR(ETIMEDOUT));
5603 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5604 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5606 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5607 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5609 return SCTP_DISPOSITION_DELETE_TCB;
5610 nomem:
5611 return SCTP_DISPOSITION_NOMEM;
5614 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5615 * the association is automatically closed by starting the shutdown process.
5616 * The work that needs to be done is same as when SHUTDOWN is initiated by
5617 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5619 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5620 const struct sctp_endpoint *ep,
5621 const struct sctp_association *asoc,
5622 const sctp_subtype_t type,
5623 void *arg,
5624 sctp_cmd_seq_t *commands)
5626 int disposition;
5628 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5630 /* From 9.2 Shutdown of an Association
5631 * Upon receipt of the SHUTDOWN primitive from its upper
5632 * layer, the endpoint enters SHUTDOWN-PENDING state and
5633 * remains there until all outstanding data has been
5634 * acknowledged by its peer. The endpoint accepts no new data
5635 * from its upper layer, but retransmits data to the far end
5636 * if necessary to fill gaps.
5638 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5639 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5641 disposition = SCTP_DISPOSITION_CONSUME;
5642 if (sctp_outq_is_empty(&asoc->outqueue)) {
5643 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5644 arg, commands);
5646 return disposition;
5649 /*****************************************************************************
5650 * These are sa state functions which could apply to all types of events.
5651 ****************************************************************************/
5654 * This table entry is not implemented.
5656 * Inputs
5657 * (endpoint, asoc, chunk)
5659 * The return value is the disposition of the chunk.
5661 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5662 const struct sctp_association *asoc,
5663 const sctp_subtype_t type,
5664 void *arg,
5665 sctp_cmd_seq_t *commands)
5667 return SCTP_DISPOSITION_NOT_IMPL;
5671 * This table entry represents a bug.
5673 * Inputs
5674 * (endpoint, asoc, chunk)
5676 * The return value is the disposition of the chunk.
5678 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5679 const struct sctp_association *asoc,
5680 const sctp_subtype_t type,
5681 void *arg,
5682 sctp_cmd_seq_t *commands)
5684 return SCTP_DISPOSITION_BUG;
5688 * This table entry represents the firing of a timer in the wrong state.
5689 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5690 * when the association is in the wrong state. This event should
5691 * be ignored, so as to prevent any rearming of the timer.
5693 * Inputs
5694 * (endpoint, asoc, chunk)
5696 * The return value is the disposition of the chunk.
5698 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5699 const struct sctp_association *asoc,
5700 const sctp_subtype_t type,
5701 void *arg,
5702 sctp_cmd_seq_t *commands)
5704 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5705 return SCTP_DISPOSITION_CONSUME;
5708 /********************************************************************
5709 * 2nd Level Abstractions
5710 ********************************************************************/
5712 /* Pull the SACK chunk based on the SACK header. */
5713 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5715 struct sctp_sackhdr *sack;
5716 unsigned int len;
5717 __u16 num_blocks;
5718 __u16 num_dup_tsns;
5720 /* Protect ourselves from reading too far into
5721 * the skb from a bogus sender.
5723 sack = (struct sctp_sackhdr *) chunk->skb->data;
5725 num_blocks = ntohs(sack->num_gap_ack_blocks);
5726 num_dup_tsns = ntohs(sack->num_dup_tsns);
5727 len = sizeof(struct sctp_sackhdr);
5728 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5729 if (len > chunk->skb->len)
5730 return NULL;
5732 skb_pull(chunk->skb, len);
5734 return sack;
5737 /* Create an ABORT packet to be sent as a response, with the specified
5738 * error causes.
5740 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5741 const struct sctp_association *asoc,
5742 struct sctp_chunk *chunk,
5743 const void *payload,
5744 size_t paylen)
5746 struct sctp_packet *packet;
5747 struct sctp_chunk *abort;
5749 packet = sctp_ootb_pkt_new(asoc, chunk);
5751 if (packet) {
5752 /* Make an ABORT.
5753 * The T bit will be set if the asoc is NULL.
5755 abort = sctp_make_abort(asoc, chunk, paylen);
5756 if (!abort) {
5757 sctp_ootb_pkt_free(packet);
5758 return NULL;
5761 /* Reflect vtag if T-Bit is set */
5762 if (sctp_test_T_bit(abort))
5763 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5765 /* Add specified error causes, i.e., payload, to the
5766 * end of the chunk.
5768 sctp_addto_chunk(abort, paylen, payload);
5770 /* Set the skb to the belonging sock for accounting. */
5771 abort->skb->sk = ep->base.sk;
5773 sctp_packet_append_chunk(packet, abort);
5777 return packet;
5780 /* Allocate a packet for responding in the OOTB conditions. */
5781 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5782 const struct sctp_chunk *chunk)
5784 struct sctp_packet *packet;
5785 struct sctp_transport *transport;
5786 __u16 sport;
5787 __u16 dport;
5788 __u32 vtag;
5790 /* Get the source and destination port from the inbound packet. */
5791 sport = ntohs(chunk->sctp_hdr->dest);
5792 dport = ntohs(chunk->sctp_hdr->source);
5794 /* The V-tag is going to be the same as the inbound packet if no
5795 * association exists, otherwise, use the peer's vtag.
5797 if (asoc) {
5798 /* Special case the INIT-ACK as there is no peer's vtag
5799 * yet.
5801 switch(chunk->chunk_hdr->type) {
5802 case SCTP_CID_INIT_ACK:
5804 sctp_initack_chunk_t *initack;
5806 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5807 vtag = ntohl(initack->init_hdr.init_tag);
5808 break;
5810 default:
5811 vtag = asoc->peer.i.init_tag;
5812 break;
5814 } else {
5815 /* Special case the INIT and stale COOKIE_ECHO as there is no
5816 * vtag yet.
5818 switch(chunk->chunk_hdr->type) {
5819 case SCTP_CID_INIT:
5821 sctp_init_chunk_t *init;
5823 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5824 vtag = ntohl(init->init_hdr.init_tag);
5825 break;
5827 default:
5828 vtag = ntohl(chunk->sctp_hdr->vtag);
5829 break;
5833 /* Make a transport for the bucket, Eliza... */
5834 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5835 if (!transport)
5836 goto nomem;
5838 /* Cache a route for the transport with the chunk's destination as
5839 * the source address.
5841 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5842 sctp_sk(sctp_get_ctl_sock()));
5844 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5845 packet = sctp_packet_config(packet, vtag, 0);
5847 return packet;
5849 nomem:
5850 return NULL;
5853 /* Free the packet allocated earlier for responding in the OOTB condition. */
5854 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5856 sctp_transport_free(packet->transport);
5859 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5860 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5861 const struct sctp_association *asoc,
5862 const struct sctp_chunk *chunk,
5863 sctp_cmd_seq_t *commands,
5864 struct sctp_chunk *err_chunk)
5866 struct sctp_packet *packet;
5868 if (err_chunk) {
5869 packet = sctp_ootb_pkt_new(asoc, chunk);
5870 if (packet) {
5871 struct sctp_signed_cookie *cookie;
5873 /* Override the OOTB vtag from the cookie. */
5874 cookie = chunk->subh.cookie_hdr;
5875 packet->vtag = cookie->c.peer_vtag;
5877 /* Set the skb to the belonging sock for accounting. */
5878 err_chunk->skb->sk = ep->base.sk;
5879 sctp_packet_append_chunk(packet, err_chunk);
5880 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5881 SCTP_PACKET(packet));
5882 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5883 } else
5884 sctp_chunk_free (err_chunk);
5889 /* Process a data chunk */
5890 static int sctp_eat_data(const struct sctp_association *asoc,
5891 struct sctp_chunk *chunk,
5892 sctp_cmd_seq_t *commands)
5894 sctp_datahdr_t *data_hdr;
5895 struct sctp_chunk *err;
5896 size_t datalen;
5897 sctp_verb_t deliver;
5898 int tmp;
5899 __u32 tsn;
5900 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5901 struct sock *sk = asoc->base.sk;
5902 u16 ssn;
5903 u16 sid;
5904 u8 ordered = 0;
5906 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5907 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5909 tsn = ntohl(data_hdr->tsn);
5910 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5912 /* ASSERT: Now skb->data is really the user data. */
5914 /* Process ECN based congestion.
5916 * Since the chunk structure is reused for all chunks within
5917 * a packet, we use ecn_ce_done to track if we've already
5918 * done CE processing for this packet.
5920 * We need to do ECN processing even if we plan to discard the
5921 * chunk later.
5924 if (!chunk->ecn_ce_done) {
5925 struct sctp_af *af;
5926 chunk->ecn_ce_done = 1;
5928 af = sctp_get_af_specific(
5929 ipver2af(ip_hdr(chunk->skb)->version));
5931 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5932 /* Do real work as sideffect. */
5933 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5934 SCTP_U32(tsn));
5938 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5939 if (tmp < 0) {
5940 /* The TSN is too high--silently discard the chunk and
5941 * count on it getting retransmitted later.
5943 return SCTP_IERROR_HIGH_TSN;
5944 } else if (tmp > 0) {
5945 /* This is a duplicate. Record it. */
5946 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5947 return SCTP_IERROR_DUP_TSN;
5950 /* This is a new TSN. */
5952 /* Discard if there is no room in the receive window.
5953 * Actually, allow a little bit of overflow (up to a MTU).
5955 datalen = ntohs(chunk->chunk_hdr->length);
5956 datalen -= sizeof(sctp_data_chunk_t);
5958 deliver = SCTP_CMD_CHUNK_ULP;
5960 /* Think about partial delivery. */
5961 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5963 /* Even if we don't accept this chunk there is
5964 * memory pressure.
5966 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5969 /* Spill over rwnd a little bit. Note: While allowed, this spill over
5970 * seems a bit troublesome in that frag_point varies based on
5971 * PMTU. In cases, such as loopback, this might be a rather
5972 * large spill over.
5974 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5975 (datalen > asoc->rwnd + asoc->frag_point))) {
5977 /* If this is the next TSN, consider reneging to make
5978 * room. Note: Playing nice with a confused sender. A
5979 * malicious sender can still eat up all our buffer
5980 * space and in the future we may want to detect and
5981 * do more drastic reneging.
5983 if (sctp_tsnmap_has_gap(map) &&
5984 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5985 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5986 deliver = SCTP_CMD_RENEGE;
5987 } else {
5988 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5989 "rwnd: %d\n", tsn, datalen,
5990 asoc->rwnd);
5991 return SCTP_IERROR_IGNORE_TSN;
5996 * Also try to renege to limit our memory usage in the event that
5997 * we are under memory pressure
5998 * If we can't renege, don't worry about it, the sk_rmem_schedule
5999 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6000 * memory usage too much
6002 if (*sk->sk_prot_creator->memory_pressure) {
6003 if (sctp_tsnmap_has_gap(map) &&
6004 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6005 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6006 deliver = SCTP_CMD_RENEGE;
6011 * Section 3.3.10.9 No User Data (9)
6013 * Cause of error
6014 * ---------------
6015 * No User Data: This error cause is returned to the originator of a
6016 * DATA chunk if a received DATA chunk has no user data.
6018 if (unlikely(0 == datalen)) {
6019 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6020 if (err) {
6021 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6022 SCTP_CHUNK(err));
6024 /* We are going to ABORT, so we might as well stop
6025 * processing the rest of the chunks in the packet.
6027 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6028 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6029 SCTP_ERROR(ECONNABORTED));
6030 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6031 SCTP_PERR(SCTP_ERROR_NO_DATA));
6032 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6033 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6034 return SCTP_IERROR_NO_DATA;
6037 chunk->data_accepted = 1;
6039 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6040 * if we renege and the chunk arrives again.
6042 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6043 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6044 else {
6045 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6046 ordered = 1;
6049 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6051 * If an endpoint receive a DATA chunk with an invalid stream
6052 * identifier, it shall acknowledge the reception of the DATA chunk
6053 * following the normal procedure, immediately send an ERROR chunk
6054 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6055 * and discard the DATA chunk.
6057 sid = ntohs(data_hdr->stream);
6058 if (sid >= asoc->c.sinit_max_instreams) {
6059 /* Mark tsn as received even though we drop it */
6060 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6062 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6063 &data_hdr->stream,
6064 sizeof(data_hdr->stream),
6065 sizeof(u16));
6066 if (err)
6067 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6068 SCTP_CHUNK(err));
6069 return SCTP_IERROR_BAD_STREAM;
6072 /* Check to see if the SSN is possible for this TSN.
6073 * The biggest gap we can record is 4K wide. Since SSNs wrap
6074 * at an unsigned short, there is no way that an SSN can
6075 * wrap and for a valid TSN. We can simply check if the current
6076 * SSN is smaller then the next expected one. If it is, it wrapped
6077 * and is invalid.
6079 ssn = ntohs(data_hdr->ssn);
6080 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6081 return SCTP_IERROR_PROTO_VIOLATION;
6084 /* Send the data up to the user. Note: Schedule the
6085 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6086 * chunk needs the updated rwnd.
6088 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6090 return SCTP_IERROR_NO_ERROR;