initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / net / sctp / sm_statefuns.c
blob2f8e1c8202a22c47960a7fef69a956981b12a249
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;
999 /* Set transport error counter and association error counter
1000 * when sending heartbeat.
1002 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1003 SCTP_TRANSPORT(transport));
1004 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1005 SCTP_TRANSPORT(transport));
1007 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1008 SCTP_TRANSPORT(transport));
1010 return SCTP_DISPOSITION_CONSUME;
1014 * Process an heartbeat request.
1016 * Section: 8.3 Path Heartbeat
1017 * The receiver of the HEARTBEAT should immediately respond with a
1018 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1019 * from the received HEARTBEAT chunk.
1021 * Verification Tag: 8.5 Verification Tag [Normal verification]
1022 * When receiving an SCTP packet, the endpoint MUST ensure that the
1023 * value in the Verification Tag field of the received SCTP packet
1024 * matches its own Tag. If the received Verification Tag value does not
1025 * match the receiver's own tag value, the receiver shall silently
1026 * discard the packet and shall not process it any further except for
1027 * those cases listed in Section 8.5.1 below.
1029 * Inputs
1030 * (endpoint, asoc, chunk)
1032 * Outputs
1033 * (asoc, reply_msg, msg_up, timers, counters)
1035 * The return value is the disposition of the chunk.
1037 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1038 const struct sctp_association *asoc,
1039 const sctp_subtype_t type,
1040 void *arg,
1041 sctp_cmd_seq_t *commands)
1043 struct sctp_chunk *chunk = arg;
1044 struct sctp_chunk *reply;
1045 size_t paylen = 0;
1047 if (!sctp_vtag_verify(chunk, asoc))
1048 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1050 /* Make sure that the HEARTBEAT chunk has a valid length. */
1051 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1052 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1053 commands);
1055 /* 8.3 The receiver of the HEARTBEAT should immediately
1056 * respond with a HEARTBEAT ACK that contains the Heartbeat
1057 * Information field copied from the received HEARTBEAT chunk.
1059 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1060 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1061 if (!pskb_pull(chunk->skb, paylen))
1062 goto nomem;
1064 reply = sctp_make_heartbeat_ack(asoc, chunk,
1065 chunk->subh.hb_hdr, paylen);
1066 if (!reply)
1067 goto nomem;
1069 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1070 return SCTP_DISPOSITION_CONSUME;
1072 nomem:
1073 return SCTP_DISPOSITION_NOMEM;
1077 * Process the returning HEARTBEAT ACK.
1079 * Section: 8.3 Path Heartbeat
1080 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1081 * should clear the error counter of the destination transport
1082 * address to which the HEARTBEAT was sent, and mark the destination
1083 * transport address as active if it is not so marked. The endpoint may
1084 * optionally report to the upper layer when an inactive destination
1085 * address is marked as active due to the reception of the latest
1086 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1087 * clear the association overall error count as well (as defined
1088 * in section 8.1).
1090 * The receiver of the HEARTBEAT ACK should also perform an RTT
1091 * measurement for that destination transport address using the time
1092 * value carried in the HEARTBEAT ACK chunk.
1094 * Verification Tag: 8.5 Verification Tag [Normal verification]
1096 * Inputs
1097 * (endpoint, asoc, chunk)
1099 * Outputs
1100 * (asoc, reply_msg, msg_up, timers, counters)
1102 * The return value is the disposition of the chunk.
1104 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1105 const struct sctp_association *asoc,
1106 const sctp_subtype_t type,
1107 void *arg,
1108 sctp_cmd_seq_t *commands)
1110 struct sctp_chunk *chunk = arg;
1111 union sctp_addr from_addr;
1112 struct sctp_transport *link;
1113 sctp_sender_hb_info_t *hbinfo;
1114 unsigned long max_interval;
1116 if (!sctp_vtag_verify(chunk, asoc))
1117 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1119 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1121 sizeof(sctp_sender_hb_info_t)))
1122 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1123 commands);
1125 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1126 /* Make sure that the length of the parameter is what we expect */
1127 if (ntohs(hbinfo->param_hdr.length) !=
1128 sizeof(sctp_sender_hb_info_t)) {
1129 return SCTP_DISPOSITION_DISCARD;
1132 from_addr = hbinfo->daddr;
1133 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1135 /* This should never happen, but lets log it if so. */
1136 if (unlikely(!link)) {
1137 if (from_addr.sa.sa_family == AF_INET6) {
1138 if (net_ratelimit())
1139 printk(KERN_WARNING
1140 "%s association %p could not find address %pI6\n",
1141 __func__,
1142 asoc,
1143 &from_addr.v6.sin6_addr);
1144 } else {
1145 if (net_ratelimit())
1146 printk(KERN_WARNING
1147 "%s association %p could not find address %pI4\n",
1148 __func__,
1149 asoc,
1150 &from_addr.v4.sin_addr.s_addr);
1152 return SCTP_DISPOSITION_DISCARD;
1155 /* Validate the 64-bit random nonce. */
1156 if (hbinfo->hb_nonce != link->hb_nonce)
1157 return SCTP_DISPOSITION_DISCARD;
1159 max_interval = link->hbinterval + link->rto;
1161 /* Check if the timestamp looks valid. */
1162 if (time_after(hbinfo->sent_at, jiffies) ||
1163 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1164 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1165 "received for transport: %p\n",
1166 __func__, link);
1167 return SCTP_DISPOSITION_DISCARD;
1170 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1171 * the HEARTBEAT should clear the error counter of the
1172 * destination transport address to which the HEARTBEAT was
1173 * sent and mark the destination transport address as active if
1174 * it is not so marked.
1176 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1178 return SCTP_DISPOSITION_CONSUME;
1181 /* Helper function to send out an abort for the restart
1182 * condition.
1184 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1185 struct sctp_chunk *init,
1186 sctp_cmd_seq_t *commands)
1188 int len;
1189 struct sctp_packet *pkt;
1190 union sctp_addr_param *addrparm;
1191 struct sctp_errhdr *errhdr;
1192 struct sctp_endpoint *ep;
1193 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1194 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1196 /* Build the error on the stack. We are way to malloc crazy
1197 * throughout the code today.
1199 errhdr = (struct sctp_errhdr *)buffer;
1200 addrparm = (union sctp_addr_param *)errhdr->variable;
1202 /* Copy into a parm format. */
1203 len = af->to_addr_param(ssa, addrparm);
1204 len += sizeof(sctp_errhdr_t);
1206 errhdr->cause = SCTP_ERROR_RESTART;
1207 errhdr->length = htons(len);
1209 /* Assign to the control socket. */
1210 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1212 /* Association is NULL since this may be a restart attack and we
1213 * want to send back the attacker's vtag.
1215 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1217 if (!pkt)
1218 goto out;
1219 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1221 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1223 /* Discard the rest of the inbound packet. */
1224 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1226 out:
1227 /* Even if there is no memory, treat as a failure so
1228 * the packet will get dropped.
1230 return 0;
1233 /* A restart is occurring, check to make sure no new addresses
1234 * are being added as we may be under a takeover attack.
1236 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1237 const struct sctp_association *asoc,
1238 struct sctp_chunk *init,
1239 sctp_cmd_seq_t *commands)
1241 struct sctp_transport *new_addr, *addr;
1242 int found;
1244 /* Implementor's Guide - Sectin 5.2.2
1245 * ...
1246 * Before responding the endpoint MUST check to see if the
1247 * unexpected INIT adds new addresses to the association. If new
1248 * addresses are added to the association, the endpoint MUST respond
1249 * with an ABORT..
1252 /* Search through all current addresses and make sure
1253 * we aren't adding any new ones.
1255 new_addr = NULL;
1256 found = 0;
1258 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1259 transports) {
1260 found = 0;
1261 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1262 transports) {
1263 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1264 &addr->ipaddr)) {
1265 found = 1;
1266 break;
1269 if (!found)
1270 break;
1273 /* If a new address was added, ABORT the sender. */
1274 if (!found && new_addr) {
1275 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1278 /* Return success if all addresses were found. */
1279 return found;
1282 /* Populate the verification/tie tags based on overlapping INIT
1283 * scenario.
1285 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1287 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1288 const struct sctp_association *asoc)
1290 switch (asoc->state) {
1292 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1294 case SCTP_STATE_COOKIE_WAIT:
1295 new_asoc->c.my_vtag = asoc->c.my_vtag;
1296 new_asoc->c.my_ttag = asoc->c.my_vtag;
1297 new_asoc->c.peer_ttag = 0;
1298 break;
1300 case SCTP_STATE_COOKIE_ECHOED:
1301 new_asoc->c.my_vtag = asoc->c.my_vtag;
1302 new_asoc->c.my_ttag = asoc->c.my_vtag;
1303 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1304 break;
1306 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1307 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1309 default:
1310 new_asoc->c.my_ttag = asoc->c.my_vtag;
1311 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1312 break;
1315 /* Other parameters for the endpoint SHOULD be copied from the
1316 * existing parameters of the association (e.g. number of
1317 * outbound streams) into the INIT ACK and cookie.
1319 new_asoc->rwnd = asoc->rwnd;
1320 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1321 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1322 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1326 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1327 * handling action.
1329 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1331 * Returns value representing action to be taken. These action values
1332 * correspond to Action/Description values in RFC 2960, Table 2.
1334 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1335 const struct sctp_association *asoc)
1337 /* In this case, the peer may have restarted. */
1338 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1339 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1340 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1341 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1342 return 'A';
1344 /* Collision case B. */
1345 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1346 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1347 (0 == asoc->c.peer_vtag))) {
1348 return 'B';
1351 /* Collision case D. */
1352 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1353 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1354 return 'D';
1356 /* Collision case C. */
1357 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1358 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1359 (0 == new_asoc->c.my_ttag) &&
1360 (0 == new_asoc->c.peer_ttag))
1361 return 'C';
1363 /* No match to any of the special cases; discard this packet. */
1364 return 'E';
1367 /* Common helper routine for both duplicate and simulataneous INIT
1368 * chunk handling.
1370 static sctp_disposition_t sctp_sf_do_unexpected_init(
1371 const struct sctp_endpoint *ep,
1372 const struct sctp_association *asoc,
1373 const sctp_subtype_t type,
1374 void *arg, sctp_cmd_seq_t *commands)
1376 sctp_disposition_t retval;
1377 struct sctp_chunk *chunk = arg;
1378 struct sctp_chunk *repl;
1379 struct sctp_association *new_asoc;
1380 struct sctp_chunk *err_chunk;
1381 struct sctp_packet *packet;
1382 sctp_unrecognized_param_t *unk_param;
1383 int len;
1385 /* 6.10 Bundling
1386 * An endpoint MUST NOT bundle INIT, INIT ACK or
1387 * SHUTDOWN COMPLETE with any other chunks.
1389 * IG Section 2.11.2
1390 * Furthermore, we require that the receiver of an INIT chunk MUST
1391 * enforce these rules by silently discarding an arriving packet
1392 * with an INIT chunk that is bundled with other chunks.
1394 if (!chunk->singleton)
1395 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1397 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1398 * Tag.
1400 if (chunk->sctp_hdr->vtag != 0)
1401 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1403 /* Make sure that the INIT chunk has a valid length.
1404 * In this case, we generate a protocol violation since we have
1405 * an association established.
1407 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1408 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1409 commands);
1410 /* Grab the INIT header. */
1411 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1413 /* Tag the variable length parameters. */
1414 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1416 /* Verify the INIT chunk before processing it. */
1417 err_chunk = NULL;
1418 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1419 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1420 &err_chunk)) {
1421 /* This chunk contains fatal error. It is to be discarded.
1422 * Send an ABORT, with causes if there is any.
1424 if (err_chunk) {
1425 packet = sctp_abort_pkt_new(ep, asoc, arg,
1426 (__u8 *)(err_chunk->chunk_hdr) +
1427 sizeof(sctp_chunkhdr_t),
1428 ntohs(err_chunk->chunk_hdr->length) -
1429 sizeof(sctp_chunkhdr_t));
1431 if (packet) {
1432 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1433 SCTP_PACKET(packet));
1434 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1435 retval = SCTP_DISPOSITION_CONSUME;
1436 } else {
1437 retval = SCTP_DISPOSITION_NOMEM;
1439 goto cleanup;
1440 } else {
1441 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1442 commands);
1447 * Other parameters for the endpoint SHOULD be copied from the
1448 * existing parameters of the association (e.g. number of
1449 * outbound streams) into the INIT ACK and cookie.
1450 * FIXME: We are copying parameters from the endpoint not the
1451 * association.
1453 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1454 if (!new_asoc)
1455 goto nomem;
1457 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1458 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1459 goto nomem;
1461 /* In the outbound INIT ACK the endpoint MUST copy its current
1462 * Verification Tag and Peers Verification tag into a reserved
1463 * place (local tie-tag and per tie-tag) within the state cookie.
1465 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1466 sctp_source(chunk),
1467 (sctp_init_chunk_t *)chunk->chunk_hdr,
1468 GFP_ATOMIC))
1469 goto nomem;
1471 /* Make sure no new addresses are being added during the
1472 * restart. Do not do this check for COOKIE-WAIT state,
1473 * since there are no peer addresses to check against.
1474 * Upon return an ABORT will have been sent if needed.
1476 if (!sctp_state(asoc, COOKIE_WAIT)) {
1477 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1478 commands)) {
1479 retval = SCTP_DISPOSITION_CONSUME;
1480 goto nomem_retval;
1484 sctp_tietags_populate(new_asoc, asoc);
1486 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1488 /* If there are errors need to be reported for unknown parameters,
1489 * make sure to reserve enough room in the INIT ACK for them.
1491 len = 0;
1492 if (err_chunk) {
1493 len = ntohs(err_chunk->chunk_hdr->length) -
1494 sizeof(sctp_chunkhdr_t);
1497 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1498 if (!repl)
1499 goto nomem;
1501 /* If there are errors need to be reported for unknown parameters,
1502 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1503 * parameter.
1505 if (err_chunk) {
1506 /* Get the "Unrecognized parameter" parameter(s) out of the
1507 * ERROR chunk generated by sctp_verify_init(). Since the
1508 * error cause code for "unknown parameter" and the
1509 * "Unrecognized parameter" type is the same, we can
1510 * construct the parameters in INIT ACK by copying the
1511 * ERROR causes over.
1513 unk_param = (sctp_unrecognized_param_t *)
1514 ((__u8 *)(err_chunk->chunk_hdr) +
1515 sizeof(sctp_chunkhdr_t));
1516 /* Replace the cause code with the "Unrecognized parameter"
1517 * parameter type.
1519 sctp_addto_chunk(repl, len, unk_param);
1522 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1523 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1526 * Note: After sending out INIT ACK with the State Cookie parameter,
1527 * "Z" MUST NOT allocate any resources for this new association.
1528 * Otherwise, "Z" will be vulnerable to resource attacks.
1530 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1531 retval = SCTP_DISPOSITION_CONSUME;
1533 return retval;
1535 nomem:
1536 retval = SCTP_DISPOSITION_NOMEM;
1537 nomem_retval:
1538 if (new_asoc)
1539 sctp_association_free(new_asoc);
1540 cleanup:
1541 if (err_chunk)
1542 sctp_chunk_free(err_chunk);
1543 return retval;
1547 * Handle simultanous INIT.
1548 * This means we started an INIT and then we got an INIT request from
1549 * our peer.
1551 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1552 * This usually indicates an initialization collision, i.e., each
1553 * endpoint is attempting, at about the same time, to establish an
1554 * association with the other endpoint.
1556 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1557 * endpoint MUST respond with an INIT ACK using the same parameters it
1558 * sent in its original INIT chunk (including its Verification Tag,
1559 * unchanged). These original parameters are combined with those from the
1560 * newly received INIT chunk. The endpoint shall also generate a State
1561 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1562 * INIT to calculate the State Cookie.
1564 * After that, the endpoint MUST NOT change its state, the T1-init
1565 * timer shall be left running and the corresponding TCB MUST NOT be
1566 * destroyed. The normal procedures for handling State Cookies when
1567 * a TCB exists will resolve the duplicate INITs to a single association.
1569 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1570 * its Tie-Tags with the Tag information of itself and its peer (see
1571 * section 5.2.2 for a description of the Tie-Tags).
1573 * Verification Tag: Not explicit, but an INIT can not have a valid
1574 * verification tag, so we skip the check.
1576 * Inputs
1577 * (endpoint, asoc, chunk)
1579 * Outputs
1580 * (asoc, reply_msg, msg_up, timers, counters)
1582 * The return value is the disposition of the chunk.
1584 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1585 const struct sctp_association *asoc,
1586 const sctp_subtype_t type,
1587 void *arg,
1588 sctp_cmd_seq_t *commands)
1590 /* Call helper to do the real work for both simulataneous and
1591 * duplicate INIT chunk handling.
1593 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1597 * Handle duplicated INIT messages. These are usually delayed
1598 * restransmissions.
1600 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1601 * COOKIE-ECHOED and COOKIE-WAIT
1603 * Unless otherwise stated, upon reception of an unexpected INIT for
1604 * this association, the endpoint shall generate an INIT ACK with a
1605 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1606 * current Verification Tag and peer's Verification Tag into a reserved
1607 * place within the state cookie. We shall refer to these locations as
1608 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1609 * containing this INIT ACK MUST carry a Verification Tag value equal to
1610 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1611 * MUST contain a new Initiation Tag (randomly generated see Section
1612 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1613 * existing parameters of the association (e.g. number of outbound
1614 * streams) into the INIT ACK and cookie.
1616 * After sending out the INIT ACK, the endpoint shall take no further
1617 * actions, i.e., the existing association, including its current state,
1618 * and the corresponding TCB MUST NOT be changed.
1620 * Note: Only when a TCB exists and the association is not in a COOKIE-
1621 * WAIT state are the Tie-Tags populated. For a normal association INIT
1622 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1623 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1624 * State Cookie are populated as specified in section 5.2.1.
1626 * Verification Tag: Not specified, but an INIT has no way of knowing
1627 * what the verification tag could be, so we ignore it.
1629 * Inputs
1630 * (endpoint, asoc, chunk)
1632 * Outputs
1633 * (asoc, reply_msg, msg_up, timers, counters)
1635 * The return value is the disposition of the chunk.
1637 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1638 const struct sctp_association *asoc,
1639 const sctp_subtype_t type,
1640 void *arg,
1641 sctp_cmd_seq_t *commands)
1643 /* Call helper to do the real work for both simulataneous and
1644 * duplicate INIT chunk handling.
1646 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1651 * Unexpected INIT-ACK handler.
1653 * Section 5.2.3
1654 * If an INIT ACK received by an endpoint in any state other than the
1655 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1656 * An unexpected INIT ACK usually indicates the processing of an old or
1657 * duplicated INIT chunk.
1659 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1660 const struct sctp_association *asoc,
1661 const sctp_subtype_t type,
1662 void *arg, sctp_cmd_seq_t *commands)
1664 /* Per the above section, we'll discard the chunk if we have an
1665 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1667 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
1668 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1669 else
1670 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1673 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1675 * Section 5.2.4
1676 * A) In this case, the peer may have restarted.
1678 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1679 const struct sctp_association *asoc,
1680 struct sctp_chunk *chunk,
1681 sctp_cmd_seq_t *commands,
1682 struct sctp_association *new_asoc)
1684 sctp_init_chunk_t *peer_init;
1685 struct sctp_ulpevent *ev;
1686 struct sctp_chunk *repl;
1687 struct sctp_chunk *err;
1688 sctp_disposition_t disposition;
1690 /* new_asoc is a brand-new association, so these are not yet
1691 * side effects--it is safe to run them here.
1693 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1695 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1696 sctp_source(chunk), peer_init,
1697 GFP_ATOMIC))
1698 goto nomem;
1700 /* Make sure no new addresses are being added during the
1701 * restart. Though this is a pretty complicated attack
1702 * since you'd have to get inside the cookie.
1704 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1705 return SCTP_DISPOSITION_CONSUME;
1708 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1709 * the peer has restarted (Action A), it MUST NOT setup a new
1710 * association but instead resend the SHUTDOWN ACK and send an ERROR
1711 * chunk with a "Cookie Received while Shutting Down" error cause to
1712 * its peer.
1714 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1715 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1716 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1717 chunk, commands);
1718 if (SCTP_DISPOSITION_NOMEM == disposition)
1719 goto nomem;
1721 err = sctp_make_op_error(asoc, chunk,
1722 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1723 NULL, 0);
1724 if (err)
1725 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1726 SCTP_CHUNK(err));
1728 return SCTP_DISPOSITION_CONSUME;
1731 /* For now, fail any unsent/unacked data. Consider the optional
1732 * choice of resending of this data.
1734 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1736 repl = sctp_make_cookie_ack(new_asoc, chunk);
1737 if (!repl)
1738 goto nomem;
1740 /* Report association restart to upper layer. */
1741 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1742 new_asoc->c.sinit_num_ostreams,
1743 new_asoc->c.sinit_max_instreams,
1744 NULL, GFP_ATOMIC);
1745 if (!ev)
1746 goto nomem_ev;
1748 /* Update the content of current association. */
1749 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1750 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1751 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1752 return SCTP_DISPOSITION_CONSUME;
1754 nomem_ev:
1755 sctp_chunk_free(repl);
1756 nomem:
1757 return SCTP_DISPOSITION_NOMEM;
1760 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1762 * Section 5.2.4
1763 * B) In this case, both sides may be attempting to start an association
1764 * at about the same time but the peer endpoint started its INIT
1765 * after responding to the local endpoint's INIT
1767 /* This case represents an initialization collision. */
1768 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1769 const struct sctp_association *asoc,
1770 struct sctp_chunk *chunk,
1771 sctp_cmd_seq_t *commands,
1772 struct sctp_association *new_asoc)
1774 sctp_init_chunk_t *peer_init;
1775 struct sctp_chunk *repl;
1777 /* new_asoc is a brand-new association, so these are not yet
1778 * side effects--it is safe to run them here.
1780 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1781 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1782 sctp_source(chunk), peer_init,
1783 GFP_ATOMIC))
1784 goto nomem;
1786 /* Update the content of current association. */
1787 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1788 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1789 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1790 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1791 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1793 repl = sctp_make_cookie_ack(new_asoc, chunk);
1794 if (!repl)
1795 goto nomem;
1797 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1799 /* RFC 2960 5.1 Normal Establishment of an Association
1801 * D) IMPLEMENTATION NOTE: An implementation may choose to
1802 * send the Communication Up notification to the SCTP user
1803 * upon reception of a valid COOKIE ECHO chunk.
1805 * Sadly, this needs to be implemented as a side-effect, because
1806 * we are not guaranteed to have set the association id of the real
1807 * association and so these notifications need to be delayed until
1808 * the association id is allocated.
1811 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1813 /* Sockets API Draft Section 5.3.1.6
1814 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1815 * delivers this notification to inform the application that of the
1816 * peers requested adaptation layer.
1818 * This also needs to be done as a side effect for the same reason as
1819 * above.
1821 if (asoc->peer.adaptation_ind)
1822 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1824 return SCTP_DISPOSITION_CONSUME;
1826 nomem:
1827 return SCTP_DISPOSITION_NOMEM;
1830 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1832 * Section 5.2.4
1833 * C) In this case, the local endpoint's cookie has arrived late.
1834 * Before it arrived, the local endpoint sent an INIT and received an
1835 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1836 * but a new tag of its own.
1838 /* This case represents an initialization collision. */
1839 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1840 const struct sctp_association *asoc,
1841 struct sctp_chunk *chunk,
1842 sctp_cmd_seq_t *commands,
1843 struct sctp_association *new_asoc)
1845 /* The cookie should be silently discarded.
1846 * The endpoint SHOULD NOT change states and should leave
1847 * any timers running.
1849 return SCTP_DISPOSITION_DISCARD;
1852 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1854 * Section 5.2.4
1856 * D) When both local and remote tags match the endpoint should always
1857 * enter the ESTABLISHED state, if it has not already done so.
1859 /* This case represents an initialization collision. */
1860 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1861 const struct sctp_association *asoc,
1862 struct sctp_chunk *chunk,
1863 sctp_cmd_seq_t *commands,
1864 struct sctp_association *new_asoc)
1866 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1867 struct sctp_chunk *repl;
1869 /* Clarification from Implementor's Guide:
1870 * D) When both local and remote tags match the endpoint should
1871 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1872 * It should stop any cookie timer that may be running and send
1873 * a COOKIE ACK.
1876 /* Don't accidentally move back into established state. */
1877 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1878 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1879 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1880 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1881 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1882 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1883 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1884 SCTP_NULL());
1886 /* RFC 2960 5.1 Normal Establishment of an Association
1888 * D) IMPLEMENTATION NOTE: An implementation may choose
1889 * to send the Communication Up notification to the
1890 * SCTP user upon reception of a valid COOKIE
1891 * ECHO chunk.
1893 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1894 SCTP_COMM_UP, 0,
1895 asoc->c.sinit_num_ostreams,
1896 asoc->c.sinit_max_instreams,
1897 NULL, GFP_ATOMIC);
1898 if (!ev)
1899 goto nomem;
1901 /* Sockets API Draft Section 5.3.1.6
1902 * When a peer sends a Adaptation Layer Indication parameter,
1903 * SCTP delivers this notification to inform the application
1904 * that of the peers requested adaptation layer.
1906 if (asoc->peer.adaptation_ind) {
1907 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1908 GFP_ATOMIC);
1909 if (!ai_ev)
1910 goto nomem;
1915 repl = sctp_make_cookie_ack(new_asoc, chunk);
1916 if (!repl)
1917 goto nomem;
1919 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1921 if (ev)
1922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1923 SCTP_ULPEVENT(ev));
1924 if (ai_ev)
1925 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1926 SCTP_ULPEVENT(ai_ev));
1928 return SCTP_DISPOSITION_CONSUME;
1930 nomem:
1931 if (ai_ev)
1932 sctp_ulpevent_free(ai_ev);
1933 if (ev)
1934 sctp_ulpevent_free(ev);
1935 return SCTP_DISPOSITION_NOMEM;
1939 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1940 * chunk was retransmitted and then delayed in the network.
1942 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1944 * Verification Tag: None. Do cookie validation.
1946 * Inputs
1947 * (endpoint, asoc, chunk)
1949 * Outputs
1950 * (asoc, reply_msg, msg_up, timers, counters)
1952 * The return value is the disposition of the chunk.
1954 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1955 const struct sctp_association *asoc,
1956 const sctp_subtype_t type,
1957 void *arg,
1958 sctp_cmd_seq_t *commands)
1960 sctp_disposition_t retval;
1961 struct sctp_chunk *chunk = arg;
1962 struct sctp_association *new_asoc;
1963 int error = 0;
1964 char action;
1965 struct sctp_chunk *err_chk_p;
1967 /* Make sure that the chunk has a valid length from the protocol
1968 * perspective. In this case check to make sure we have at least
1969 * enough for the chunk header. Cookie length verification is
1970 * done later.
1972 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1973 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1974 commands);
1976 /* "Decode" the chunk. We have no optional parameters so we
1977 * are in good shape.
1979 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1980 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1981 sizeof(sctp_chunkhdr_t)))
1982 goto nomem;
1984 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1985 * of a duplicate COOKIE ECHO match the Verification Tags of the
1986 * current association, consider the State Cookie valid even if
1987 * the lifespan is exceeded.
1989 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1990 &err_chk_p);
1992 /* FIXME:
1993 * If the re-build failed, what is the proper error path
1994 * from here?
1996 * [We should abort the association. --piggy]
1998 if (!new_asoc) {
1999 /* FIXME: Several errors are possible. A bad cookie should
2000 * be silently discarded, but think about logging it too.
2002 switch (error) {
2003 case -SCTP_IERROR_NOMEM:
2004 goto nomem;
2006 case -SCTP_IERROR_STALE_COOKIE:
2007 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2008 err_chk_p);
2009 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2010 case -SCTP_IERROR_BAD_SIG:
2011 default:
2012 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2016 /* Compare the tie_tag in cookie with the verification tag of
2017 * current association.
2019 action = sctp_tietags_compare(new_asoc, asoc);
2021 switch (action) {
2022 case 'A': /* Association restart. */
2023 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2024 new_asoc);
2025 break;
2027 case 'B': /* Collision case B. */
2028 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2029 new_asoc);
2030 break;
2032 case 'C': /* Collision case C. */
2033 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2034 new_asoc);
2035 break;
2037 case 'D': /* Collision case D. */
2038 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2039 new_asoc);
2040 break;
2042 default: /* Discard packet for all others. */
2043 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2044 break;
2047 /* Delete the tempory new association. */
2048 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2049 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2051 return retval;
2053 nomem:
2054 return SCTP_DISPOSITION_NOMEM;
2058 * Process an ABORT. (SHUTDOWN-PENDING state)
2060 * See sctp_sf_do_9_1_abort().
2062 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2063 const struct sctp_endpoint *ep,
2064 const struct sctp_association *asoc,
2065 const sctp_subtype_t type,
2066 void *arg,
2067 sctp_cmd_seq_t *commands)
2069 struct sctp_chunk *chunk = arg;
2071 if (!sctp_vtag_verify_either(chunk, asoc))
2072 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2074 /* Make sure that the ABORT chunk has a valid length.
2075 * Since this is an ABORT chunk, we have to discard it
2076 * because of the following text:
2077 * RFC 2960, Section 3.3.7
2078 * If an endpoint receives an ABORT with a format error or for an
2079 * association that doesn't exist, it MUST silently discard it.
2080 * Becasue the length is "invalid", we can't really discard just
2081 * as we do not know its true length. So, to be safe, discard the
2082 * packet.
2084 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2085 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2087 /* ADD-IP: Special case for ABORT chunks
2088 * F4) One special consideration is that ABORT Chunks arriving
2089 * destined to the IP address being deleted MUST be
2090 * ignored (see Section 5.3.1 for further details).
2092 if (SCTP_ADDR_DEL ==
2093 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2094 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2096 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2100 * Process an ABORT. (SHUTDOWN-SENT state)
2102 * See sctp_sf_do_9_1_abort().
2104 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2105 const struct sctp_association *asoc,
2106 const sctp_subtype_t type,
2107 void *arg,
2108 sctp_cmd_seq_t *commands)
2110 struct sctp_chunk *chunk = arg;
2112 if (!sctp_vtag_verify_either(chunk, asoc))
2113 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2115 /* Make sure that the ABORT chunk has a valid length.
2116 * Since this is an ABORT chunk, we have to discard it
2117 * because of the following text:
2118 * RFC 2960, Section 3.3.7
2119 * If an endpoint receives an ABORT with a format error or for an
2120 * association that doesn't exist, it MUST silently discard it.
2121 * Becasue the length is "invalid", we can't really discard just
2122 * as we do not know its true length. So, to be safe, discard the
2123 * packet.
2125 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2126 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2128 /* ADD-IP: Special case for ABORT chunks
2129 * F4) One special consideration is that ABORT Chunks arriving
2130 * destined to the IP address being deleted MUST be
2131 * ignored (see Section 5.3.1 for further details).
2133 if (SCTP_ADDR_DEL ==
2134 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2135 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2137 /* Stop the T2-shutdown timer. */
2138 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2139 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2141 /* Stop the T5-shutdown guard timer. */
2142 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2143 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2145 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2149 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2151 * See sctp_sf_do_9_1_abort().
2153 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2154 const struct sctp_endpoint *ep,
2155 const struct sctp_association *asoc,
2156 const sctp_subtype_t type,
2157 void *arg,
2158 sctp_cmd_seq_t *commands)
2160 /* The same T2 timer, so we should be able to use
2161 * common function with the SHUTDOWN-SENT state.
2163 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2167 * Handle an Error received in COOKIE_ECHOED state.
2169 * Only handle the error type of stale COOKIE Error, the other errors will
2170 * be ignored.
2172 * Inputs
2173 * (endpoint, asoc, chunk)
2175 * Outputs
2176 * (asoc, reply_msg, msg_up, timers, counters)
2178 * The return value is the disposition of the chunk.
2180 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2181 const struct sctp_association *asoc,
2182 const sctp_subtype_t type,
2183 void *arg,
2184 sctp_cmd_seq_t *commands)
2186 struct sctp_chunk *chunk = arg;
2187 sctp_errhdr_t *err;
2189 if (!sctp_vtag_verify(chunk, asoc))
2190 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2192 /* Make sure that the ERROR chunk has a valid length.
2193 * The parameter walking depends on this as well.
2195 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2196 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2197 commands);
2199 /* Process the error here */
2200 /* FUTURE FIXME: When PR-SCTP related and other optional
2201 * parms are emitted, this will have to change to handle multiple
2202 * errors.
2204 sctp_walk_errors(err, chunk->chunk_hdr) {
2205 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2206 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
2207 arg, commands);
2210 /* It is possible to have malformed error causes, and that
2211 * will cause us to end the walk early. However, since
2212 * we are discarding the packet, there should be no adverse
2213 * affects.
2215 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2219 * Handle a Stale COOKIE Error
2221 * Section: 5.2.6 Handle Stale COOKIE Error
2222 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2223 * one of the following three alternatives.
2224 * ...
2225 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2226 * Preservative parameter requesting an extension to the lifetime of
2227 * the State Cookie. When calculating the time extension, an
2228 * implementation SHOULD use the RTT information measured based on the
2229 * previous COOKIE ECHO / ERROR exchange, and should add no more
2230 * than 1 second beyond the measured RTT, due to long State Cookie
2231 * lifetimes making the endpoint more subject to a replay attack.
2233 * Verification Tag: Not explicit, but safe to ignore.
2235 * Inputs
2236 * (endpoint, asoc, chunk)
2238 * Outputs
2239 * (asoc, reply_msg, msg_up, timers, counters)
2241 * The return value is the disposition of the chunk.
2243 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2244 const struct sctp_association *asoc,
2245 const sctp_subtype_t type,
2246 void *arg,
2247 sctp_cmd_seq_t *commands)
2249 struct sctp_chunk *chunk = arg;
2250 time_t stale;
2251 sctp_cookie_preserve_param_t bht;
2252 sctp_errhdr_t *err;
2253 struct sctp_chunk *reply;
2254 struct sctp_bind_addr *bp;
2255 int attempts = asoc->init_err_counter + 1;
2257 if (attempts > asoc->max_init_attempts) {
2258 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2259 SCTP_ERROR(ETIMEDOUT));
2260 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2261 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2262 return SCTP_DISPOSITION_DELETE_TCB;
2265 err = (sctp_errhdr_t *)(chunk->skb->data);
2267 /* When calculating the time extension, an implementation
2268 * SHOULD use the RTT information measured based on the
2269 * previous COOKIE ECHO / ERROR exchange, and should add no
2270 * more than 1 second beyond the measured RTT, due to long
2271 * State Cookie lifetimes making the endpoint more subject to
2272 * a replay attack.
2273 * Measure of Staleness's unit is usec. (1/1000000 sec)
2274 * Suggested Cookie Life-span Increment's unit is msec.
2275 * (1/1000 sec)
2276 * In general, if you use the suggested cookie life, the value
2277 * found in the field of measure of staleness should be doubled
2278 * to give ample time to retransmit the new cookie and thus
2279 * yield a higher probability of success on the reattempt.
2281 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2282 stale = (stale * 2) / 1000;
2284 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2285 bht.param_hdr.length = htons(sizeof(bht));
2286 bht.lifespan_increment = htonl(stale);
2288 /* Build that new INIT chunk. */
2289 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2290 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2291 if (!reply)
2292 goto nomem;
2294 sctp_addto_chunk(reply, sizeof(bht), &bht);
2296 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2297 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2299 /* Stop pending T3-rtx and heartbeat timers */
2300 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2301 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2303 /* Delete non-primary peer ip addresses since we are transitioning
2304 * back to the COOKIE-WAIT state
2306 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2308 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2309 * resend
2311 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2312 SCTP_TRANSPORT(asoc->peer.primary_path));
2314 /* Cast away the const modifier, as we want to just
2315 * rerun it through as a sideffect.
2317 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2319 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2320 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2321 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2322 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2323 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2324 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2326 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2328 return SCTP_DISPOSITION_CONSUME;
2330 nomem:
2331 return SCTP_DISPOSITION_NOMEM;
2335 * Process an ABORT.
2337 * Section: 9.1
2338 * After checking the Verification Tag, the receiving endpoint shall
2339 * remove the association from its record, and shall report the
2340 * termination to its upper layer.
2342 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2343 * B) Rules for packet carrying ABORT:
2345 * - The endpoint shall always fill in the Verification Tag field of the
2346 * outbound packet with the destination endpoint's tag value if it
2347 * is known.
2349 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2350 * MUST follow the procedure described in Section 8.4.
2352 * - The receiver MUST accept the packet if the Verification Tag
2353 * matches either its own tag, OR the tag of its peer. Otherwise, the
2354 * receiver MUST silently discard the packet and take no further
2355 * action.
2357 * Inputs
2358 * (endpoint, asoc, chunk)
2360 * Outputs
2361 * (asoc, reply_msg, msg_up, timers, counters)
2363 * The return value is the disposition of the chunk.
2365 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2366 const struct sctp_association *asoc,
2367 const sctp_subtype_t type,
2368 void *arg,
2369 sctp_cmd_seq_t *commands)
2371 struct sctp_chunk *chunk = arg;
2373 if (!sctp_vtag_verify_either(chunk, asoc))
2374 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2376 /* Make sure that the ABORT chunk has a valid length.
2377 * Since this is an ABORT chunk, we have to discard it
2378 * because of the following text:
2379 * RFC 2960, Section 3.3.7
2380 * If an endpoint receives an ABORT with a format error or for an
2381 * association that doesn't exist, it MUST silently discard it.
2382 * Becasue the length is "invalid", we can't really discard just
2383 * as we do not know its true length. So, to be safe, discard the
2384 * packet.
2386 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2387 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2389 /* ADD-IP: Special case for ABORT chunks
2390 * F4) One special consideration is that ABORT Chunks arriving
2391 * destined to the IP address being deleted MUST be
2392 * ignored (see Section 5.3.1 for further details).
2394 if (SCTP_ADDR_DEL ==
2395 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2396 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2398 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2401 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2402 const struct sctp_association *asoc,
2403 const sctp_subtype_t type,
2404 void *arg,
2405 sctp_cmd_seq_t *commands)
2407 struct sctp_chunk *chunk = arg;
2408 unsigned len;
2409 __be16 error = SCTP_ERROR_NO_ERROR;
2411 /* See if we have an error cause code in the chunk. */
2412 len = ntohs(chunk->chunk_hdr->length);
2413 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2414 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2416 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2417 /* ASSOC_FAILED will DELETE_TCB. */
2418 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2419 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2420 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2422 return SCTP_DISPOSITION_ABORT;
2426 * Process an ABORT. (COOKIE-WAIT state)
2428 * See sctp_sf_do_9_1_abort() above.
2430 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2431 const struct sctp_association *asoc,
2432 const sctp_subtype_t type,
2433 void *arg,
2434 sctp_cmd_seq_t *commands)
2436 struct sctp_chunk *chunk = arg;
2437 unsigned len;
2438 __be16 error = SCTP_ERROR_NO_ERROR;
2440 if (!sctp_vtag_verify_either(chunk, asoc))
2441 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2443 /* Make sure that the ABORT chunk has a valid length.
2444 * Since this is an ABORT chunk, we have to discard it
2445 * because of the following text:
2446 * RFC 2960, Section 3.3.7
2447 * If an endpoint receives an ABORT with a format error or for an
2448 * association that doesn't exist, it MUST silently discard it.
2449 * Becasue the length is "invalid", we can't really discard just
2450 * as we do not know its true length. So, to be safe, discard the
2451 * packet.
2453 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2454 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2456 /* See if we have an error cause code in the chunk. */
2457 len = ntohs(chunk->chunk_hdr->length);
2458 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2459 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2461 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2462 chunk->transport);
2466 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2468 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2469 const struct sctp_association *asoc,
2470 const sctp_subtype_t type,
2471 void *arg,
2472 sctp_cmd_seq_t *commands)
2474 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2475 ENOPROTOOPT, asoc,
2476 (struct sctp_transport *)arg);
2480 * Process an ABORT. (COOKIE-ECHOED state)
2482 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2483 const struct sctp_association *asoc,
2484 const sctp_subtype_t type,
2485 void *arg,
2486 sctp_cmd_seq_t *commands)
2488 /* There is a single T1 timer, so we should be able to use
2489 * common function with the COOKIE-WAIT state.
2491 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2495 * Stop T1 timer and abort association with "INIT failed".
2497 * This is common code called by several sctp_sf_*_abort() functions above.
2499 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
2500 __be16 error, int sk_err,
2501 const struct sctp_association *asoc,
2502 struct sctp_transport *transport)
2504 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2505 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2506 SCTP_STATE(SCTP_STATE_CLOSED));
2507 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2508 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2509 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2510 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2511 /* CMD_INIT_FAILED will DELETE_TCB. */
2512 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2513 SCTP_PERR(error));
2514 return SCTP_DISPOSITION_ABORT;
2518 * sctp_sf_do_9_2_shut
2520 * Section: 9.2
2521 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2522 * - enter the SHUTDOWN-RECEIVED state,
2524 * - stop accepting new data from its SCTP user
2526 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2527 * that all its outstanding DATA chunks have been received by the
2528 * SHUTDOWN sender.
2530 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2531 * send a SHUTDOWN in response to a ULP request. And should discard
2532 * subsequent SHUTDOWN chunks.
2534 * If there are still outstanding DATA chunks left, the SHUTDOWN
2535 * receiver shall continue to follow normal data transmission
2536 * procedures defined in Section 6 until all outstanding DATA chunks
2537 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2538 * new data from its SCTP user.
2540 * Verification Tag: 8.5 Verification Tag [Normal verification]
2542 * Inputs
2543 * (endpoint, asoc, chunk)
2545 * Outputs
2546 * (asoc, reply_msg, msg_up, timers, counters)
2548 * The return value is the disposition of the chunk.
2550 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2551 const struct sctp_association *asoc,
2552 const sctp_subtype_t type,
2553 void *arg,
2554 sctp_cmd_seq_t *commands)
2556 struct sctp_chunk *chunk = arg;
2557 sctp_shutdownhdr_t *sdh;
2558 sctp_disposition_t disposition;
2559 struct sctp_ulpevent *ev;
2560 __u32 ctsn;
2562 if (!sctp_vtag_verify(chunk, asoc))
2563 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2565 /* Make sure that the SHUTDOWN chunk has a valid length. */
2566 if (!sctp_chunk_length_valid(chunk,
2567 sizeof(struct sctp_shutdown_chunk_t)))
2568 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2569 commands);
2571 /* Convert the elaborate header. */
2572 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2573 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2574 chunk->subh.shutdown_hdr = sdh;
2575 ctsn = ntohl(sdh->cum_tsn_ack);
2577 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2578 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2579 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2580 return SCTP_DISPOSITION_DISCARD;
2583 /* If Cumulative TSN Ack beyond the max tsn currently
2584 * send, terminating the association and respond to the
2585 * sender with an ABORT.
2587 if (!TSN_lt(ctsn, asoc->next_tsn))
2588 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2590 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2591 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2592 * inform the application that it should cease sending data.
2594 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2595 if (!ev) {
2596 disposition = SCTP_DISPOSITION_NOMEM;
2597 goto out;
2599 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2601 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2602 * - enter the SHUTDOWN-RECEIVED state,
2603 * - stop accepting new data from its SCTP user
2605 * [This is implicit in the new state.]
2607 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2608 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2609 disposition = SCTP_DISPOSITION_CONSUME;
2611 if (sctp_outq_is_empty(&asoc->outqueue)) {
2612 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2613 arg, commands);
2616 if (SCTP_DISPOSITION_NOMEM == disposition)
2617 goto out;
2619 /* - verify, by checking the Cumulative TSN Ack field of the
2620 * chunk, that all its outstanding DATA chunks have been
2621 * received by the SHUTDOWN sender.
2623 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2624 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2626 out:
2627 return disposition;
2631 * sctp_sf_do_9_2_shut_ctsn
2633 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2634 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2635 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2636 * MUST be processed.
2638 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2639 const struct sctp_association *asoc,
2640 const sctp_subtype_t type,
2641 void *arg,
2642 sctp_cmd_seq_t *commands)
2644 struct sctp_chunk *chunk = arg;
2645 sctp_shutdownhdr_t *sdh;
2646 __u32 ctsn;
2648 if (!sctp_vtag_verify(chunk, asoc))
2649 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2651 /* Make sure that the SHUTDOWN chunk has a valid length. */
2652 if (!sctp_chunk_length_valid(chunk,
2653 sizeof(struct sctp_shutdown_chunk_t)))
2654 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2655 commands);
2657 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2658 ctsn = ntohl(sdh->cum_tsn_ack);
2660 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2661 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2662 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2663 return SCTP_DISPOSITION_DISCARD;
2666 /* If Cumulative TSN Ack beyond the max tsn currently
2667 * send, terminating the association and respond to the
2668 * sender with an ABORT.
2670 if (!TSN_lt(ctsn, asoc->next_tsn))
2671 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2673 /* verify, by checking the Cumulative TSN Ack field of the
2674 * chunk, that all its outstanding DATA chunks have been
2675 * received by the SHUTDOWN sender.
2677 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2678 SCTP_BE32(sdh->cum_tsn_ack));
2680 return SCTP_DISPOSITION_CONSUME;
2683 /* RFC 2960 9.2
2684 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2685 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2686 * transport addresses (either in the IP addresses or in the INIT chunk)
2687 * that belong to this association, it should discard the INIT chunk and
2688 * retransmit the SHUTDOWN ACK chunk.
2690 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2691 const struct sctp_association *asoc,
2692 const sctp_subtype_t type,
2693 void *arg,
2694 sctp_cmd_seq_t *commands)
2696 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2697 struct sctp_chunk *reply;
2699 /* Make sure that the chunk has a valid length */
2700 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2701 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2702 commands);
2704 /* Since we are not going to really process this INIT, there
2705 * is no point in verifying chunk boundries. Just generate
2706 * the SHUTDOWN ACK.
2708 reply = sctp_make_shutdown_ack(asoc, chunk);
2709 if (NULL == reply)
2710 goto nomem;
2712 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2713 * the T2-SHUTDOWN timer.
2715 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2717 /* and restart the T2-shutdown timer. */
2718 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2719 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2721 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2723 return SCTP_DISPOSITION_CONSUME;
2724 nomem:
2725 return SCTP_DISPOSITION_NOMEM;
2729 * sctp_sf_do_ecn_cwr
2731 * Section: Appendix A: Explicit Congestion Notification
2733 * CWR:
2735 * RFC 2481 details a specific bit for a sender to send in the header of
2736 * its next outbound TCP segment to indicate to its peer that it has
2737 * reduced its congestion window. This is termed the CWR bit. For
2738 * SCTP the same indication is made by including the CWR chunk.
2739 * This chunk contains one data element, i.e. the TSN number that
2740 * was sent in the ECNE chunk. This element represents the lowest
2741 * TSN number in the datagram that was originally marked with the
2742 * CE bit.
2744 * Verification Tag: 8.5 Verification Tag [Normal verification]
2745 * Inputs
2746 * (endpoint, asoc, chunk)
2748 * Outputs
2749 * (asoc, reply_msg, msg_up, timers, counters)
2751 * The return value is the disposition of the chunk.
2753 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2754 const struct sctp_association *asoc,
2755 const sctp_subtype_t type,
2756 void *arg,
2757 sctp_cmd_seq_t *commands)
2759 sctp_cwrhdr_t *cwr;
2760 struct sctp_chunk *chunk = arg;
2761 u32 lowest_tsn;
2763 if (!sctp_vtag_verify(chunk, asoc))
2764 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2766 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2767 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2768 commands);
2770 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2771 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2773 lowest_tsn = ntohl(cwr->lowest_tsn);
2775 /* Does this CWR ack the last sent congestion notification? */
2776 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2777 /* Stop sending ECNE. */
2778 sctp_add_cmd_sf(commands,
2779 SCTP_CMD_ECN_CWR,
2780 SCTP_U32(lowest_tsn));
2782 return SCTP_DISPOSITION_CONSUME;
2786 * sctp_sf_do_ecne
2788 * Section: Appendix A: Explicit Congestion Notification
2790 * ECN-Echo
2792 * RFC 2481 details a specific bit for a receiver to send back in its
2793 * TCP acknowledgements to notify the sender of the Congestion
2794 * Experienced (CE) bit having arrived from the network. For SCTP this
2795 * same indication is made by including the ECNE chunk. This chunk
2796 * contains one data element, i.e. the lowest TSN associated with the IP
2797 * datagram marked with the CE bit.....
2799 * Verification Tag: 8.5 Verification Tag [Normal verification]
2800 * Inputs
2801 * (endpoint, asoc, chunk)
2803 * Outputs
2804 * (asoc, reply_msg, msg_up, timers, counters)
2806 * The return value is the disposition of the chunk.
2808 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2809 const struct sctp_association *asoc,
2810 const sctp_subtype_t type,
2811 void *arg,
2812 sctp_cmd_seq_t *commands)
2814 sctp_ecnehdr_t *ecne;
2815 struct sctp_chunk *chunk = arg;
2817 if (!sctp_vtag_verify(chunk, asoc))
2818 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2820 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2821 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2822 commands);
2824 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2825 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2827 /* If this is a newer ECNE than the last CWR packet we sent out */
2828 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2829 SCTP_U32(ntohl(ecne->lowest_tsn)));
2831 return SCTP_DISPOSITION_CONSUME;
2835 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2837 * The SCTP endpoint MUST always acknowledge the reception of each valid
2838 * DATA chunk.
2840 * The guidelines on delayed acknowledgement algorithm specified in
2841 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2842 * acknowledgement SHOULD be generated for at least every second packet
2843 * (not every second DATA chunk) received, and SHOULD be generated within
2844 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2845 * situations it may be beneficial for an SCTP transmitter to be more
2846 * conservative than the algorithms detailed in this document allow.
2847 * However, an SCTP transmitter MUST NOT be more aggressive than the
2848 * following algorithms allow.
2850 * A SCTP receiver MUST NOT generate more than one SACK for every
2851 * incoming packet, other than to update the offered window as the
2852 * receiving application consumes new data.
2854 * Verification Tag: 8.5 Verification Tag [Normal verification]
2856 * Inputs
2857 * (endpoint, asoc, chunk)
2859 * Outputs
2860 * (asoc, reply_msg, msg_up, timers, counters)
2862 * The return value is the disposition of the chunk.
2864 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2865 const struct sctp_association *asoc,
2866 const sctp_subtype_t type,
2867 void *arg,
2868 sctp_cmd_seq_t *commands)
2870 struct sctp_chunk *chunk = arg;
2871 int error;
2873 if (!sctp_vtag_verify(chunk, asoc)) {
2874 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2875 SCTP_NULL());
2876 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2879 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2880 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2881 commands);
2883 error = sctp_eat_data(asoc, chunk, commands );
2884 switch (error) {
2885 case SCTP_IERROR_NO_ERROR:
2886 break;
2887 case SCTP_IERROR_HIGH_TSN:
2888 case SCTP_IERROR_BAD_STREAM:
2889 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2890 goto discard_noforce;
2891 case SCTP_IERROR_DUP_TSN:
2892 case SCTP_IERROR_IGNORE_TSN:
2893 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2894 goto discard_force;
2895 case SCTP_IERROR_NO_DATA:
2896 goto consume;
2897 case SCTP_IERROR_PROTO_VIOLATION:
2898 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2899 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2900 default:
2901 BUG();
2904 if (asoc->autoclose) {
2905 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2906 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2909 /* If this is the last chunk in a packet, we need to count it
2910 * toward sack generation. Note that we need to SACK every
2911 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2912 * THEM. We elect to NOT generate SACK's if the chunk fails
2913 * the verification tag test.
2915 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2917 * The SCTP endpoint MUST always acknowledge the reception of
2918 * each valid DATA chunk.
2920 * The guidelines on delayed acknowledgement algorithm
2921 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2922 * Specifically, an acknowledgement SHOULD be generated for at
2923 * least every second packet (not every second DATA chunk)
2924 * received, and SHOULD be generated within 200 ms of the
2925 * arrival of any unacknowledged DATA chunk. In some
2926 * situations it may be beneficial for an SCTP transmitter to
2927 * be more conservative than the algorithms detailed in this
2928 * document allow. However, an SCTP transmitter MUST NOT be
2929 * more aggressive than the following algorithms allow.
2931 if (chunk->end_of_packet)
2932 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2934 return SCTP_DISPOSITION_CONSUME;
2936 discard_force:
2937 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2939 * When a packet arrives with duplicate DATA chunk(s) and with
2940 * no new DATA chunk(s), the endpoint MUST immediately send a
2941 * SACK with no delay. If a packet arrives with duplicate
2942 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2943 * MAY immediately send a SACK. Normally receipt of duplicate
2944 * DATA chunks will occur when the original SACK chunk was lost
2945 * and the peer's RTO has expired. The duplicate TSN number(s)
2946 * SHOULD be reported in the SACK as duplicate.
2948 /* In our case, we split the MAY SACK advice up whether or not
2949 * the last chunk is a duplicate.'
2951 if (chunk->end_of_packet)
2952 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2953 return SCTP_DISPOSITION_DISCARD;
2955 discard_noforce:
2956 if (chunk->end_of_packet)
2957 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2959 return SCTP_DISPOSITION_DISCARD;
2960 consume:
2961 return SCTP_DISPOSITION_CONSUME;
2966 * sctp_sf_eat_data_fast_4_4
2968 * Section: 4 (4)
2969 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2970 * DATA chunks without delay.
2972 * Verification Tag: 8.5 Verification Tag [Normal verification]
2973 * Inputs
2974 * (endpoint, asoc, chunk)
2976 * Outputs
2977 * (asoc, reply_msg, msg_up, timers, counters)
2979 * The return value is the disposition of the chunk.
2981 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2982 const struct sctp_association *asoc,
2983 const sctp_subtype_t type,
2984 void *arg,
2985 sctp_cmd_seq_t *commands)
2987 struct sctp_chunk *chunk = arg;
2988 int error;
2990 if (!sctp_vtag_verify(chunk, asoc)) {
2991 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2992 SCTP_NULL());
2993 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2996 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2997 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2998 commands);
3000 error = sctp_eat_data(asoc, chunk, commands );
3001 switch (error) {
3002 case SCTP_IERROR_NO_ERROR:
3003 case SCTP_IERROR_HIGH_TSN:
3004 case SCTP_IERROR_DUP_TSN:
3005 case SCTP_IERROR_IGNORE_TSN:
3006 case SCTP_IERROR_BAD_STREAM:
3007 break;
3008 case SCTP_IERROR_NO_DATA:
3009 goto consume;
3010 case SCTP_IERROR_PROTO_VIOLATION:
3011 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3012 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3013 default:
3014 BUG();
3017 /* Go a head and force a SACK, since we are shutting down. */
3019 /* Implementor's Guide.
3021 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3022 * respond to each received packet containing one or more DATA chunk(s)
3023 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3025 if (chunk->end_of_packet) {
3026 /* We must delay the chunk creation since the cumulative
3027 * TSN has not been updated yet.
3029 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3030 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3031 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3032 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3035 consume:
3036 return SCTP_DISPOSITION_CONSUME;
3040 * Section: 6.2 Processing a Received SACK
3041 * D) Any time a SACK arrives, the endpoint performs the following:
3043 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3044 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3045 * increasing, a SACK whose Cumulative TSN Ack is less than the
3046 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3048 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3049 * of bytes still outstanding after processing the Cumulative TSN Ack
3050 * and the Gap Ack Blocks.
3052 * iii) If the SACK is missing a TSN that was previously
3053 * acknowledged via a Gap Ack Block (e.g., the data receiver
3054 * reneged on the data), then mark the corresponding DATA chunk
3055 * as available for retransmit: Mark it as missing for fast
3056 * retransmit as described in Section 7.2.4 and if no retransmit
3057 * timer is running for the destination address to which the DATA
3058 * chunk was originally transmitted, then T3-rtx is started for
3059 * that destination address.
3061 * Verification Tag: 8.5 Verification Tag [Normal verification]
3063 * Inputs
3064 * (endpoint, asoc, chunk)
3066 * Outputs
3067 * (asoc, reply_msg, msg_up, timers, counters)
3069 * The return value is the disposition of the chunk.
3071 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3072 const struct sctp_association *asoc,
3073 const sctp_subtype_t type,
3074 void *arg,
3075 sctp_cmd_seq_t *commands)
3077 struct sctp_chunk *chunk = arg;
3078 sctp_sackhdr_t *sackh;
3079 __u32 ctsn;
3081 if (!sctp_vtag_verify(chunk, asoc))
3082 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3084 /* Make sure that the SACK chunk has a valid length. */
3085 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3086 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3087 commands);
3089 /* Pull the SACK chunk from the data buffer */
3090 sackh = sctp_sm_pull_sack(chunk);
3091 /* Was this a bogus SACK? */
3092 if (!sackh)
3093 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3094 chunk->subh.sack_hdr = sackh;
3095 ctsn = ntohl(sackh->cum_tsn_ack);
3097 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3098 * Ack Point, then drop the SACK. Since Cumulative TSN
3099 * Ack is monotonically increasing, a SACK whose
3100 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3101 * Point indicates an out-of-order SACK.
3103 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3104 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3105 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3106 return SCTP_DISPOSITION_DISCARD;
3109 /* If Cumulative TSN Ack beyond the max tsn currently
3110 * send, terminating the association and respond to the
3111 * sender with an ABORT.
3113 if (!TSN_lt(ctsn, asoc->next_tsn))
3114 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3116 /* Return this SACK for further processing. */
3117 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3119 /* Note: We do the rest of the work on the PROCESS_SACK
3120 * sideeffect.
3122 return SCTP_DISPOSITION_CONSUME;
3126 * Generate an ABORT in response to a packet.
3128 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3130 * 8) The receiver should respond to the sender of the OOTB packet with
3131 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3132 * MUST fill in the Verification Tag field of the outbound packet
3133 * with the value found in the Verification Tag field of the OOTB
3134 * packet and set the T-bit in the Chunk Flags to indicate that the
3135 * Verification Tag is reflected. After sending this ABORT, the
3136 * receiver of the OOTB packet shall discard the OOTB packet and take
3137 * no further action.
3139 * Verification Tag:
3141 * The return value is the disposition of the chunk.
3143 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
3144 const struct sctp_association *asoc,
3145 const sctp_subtype_t type,
3146 void *arg,
3147 sctp_cmd_seq_t *commands)
3149 struct sctp_packet *packet = NULL;
3150 struct sctp_chunk *chunk = arg;
3151 struct sctp_chunk *abort;
3153 packet = sctp_ootb_pkt_new(asoc, chunk);
3155 if (packet) {
3156 /* Make an ABORT. The T bit will be set if the asoc
3157 * is NULL.
3159 abort = sctp_make_abort(asoc, chunk, 0);
3160 if (!abort) {
3161 sctp_ootb_pkt_free(packet);
3162 return SCTP_DISPOSITION_NOMEM;
3165 /* Reflect vtag if T-Bit is set */
3166 if (sctp_test_T_bit(abort))
3167 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3169 /* Set the skb to the belonging sock for accounting. */
3170 abort->skb->sk = ep->base.sk;
3172 sctp_packet_append_chunk(packet, abort);
3174 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3175 SCTP_PACKET(packet));
3177 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3179 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3180 return SCTP_DISPOSITION_CONSUME;
3183 return SCTP_DISPOSITION_NOMEM;
3187 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3188 * event as ULP notification for each cause included in the chunk.
3190 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3192 * The return value is the disposition of the chunk.
3194 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3195 const struct sctp_association *asoc,
3196 const sctp_subtype_t type,
3197 void *arg,
3198 sctp_cmd_seq_t *commands)
3200 struct sctp_chunk *chunk = arg;
3202 if (!sctp_vtag_verify(chunk, asoc))
3203 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3205 /* Make sure that the ERROR chunk has a valid length. */
3206 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3207 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3208 commands);
3210 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3211 SCTP_CHUNK(chunk));
3213 return SCTP_DISPOSITION_CONSUME;
3217 * Process an inbound SHUTDOWN ACK.
3219 * From Section 9.2:
3220 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3221 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3222 * peer, and remove all record of the association.
3224 * The return value is the disposition.
3226 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3227 const struct sctp_association *asoc,
3228 const sctp_subtype_t type,
3229 void *arg,
3230 sctp_cmd_seq_t *commands)
3232 struct sctp_chunk *chunk = arg;
3233 struct sctp_chunk *reply;
3234 struct sctp_ulpevent *ev;
3236 if (!sctp_vtag_verify(chunk, asoc))
3237 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3239 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3240 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3241 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3242 commands);
3243 /* 10.2 H) SHUTDOWN COMPLETE notification
3245 * When SCTP completes the shutdown procedures (section 9.2) this
3246 * notification is passed to the upper layer.
3248 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3249 0, 0, 0, NULL, GFP_ATOMIC);
3250 if (!ev)
3251 goto nomem;
3253 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3254 reply = sctp_make_shutdown_complete(asoc, chunk);
3255 if (!reply)
3256 goto nomem_chunk;
3258 /* Do all the commands now (after allocation), so that we
3259 * have consistent state if memory allocation failes
3261 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3263 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3264 * stop the T2-shutdown timer,
3266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3267 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3269 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3270 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3272 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3273 SCTP_STATE(SCTP_STATE_CLOSED));
3274 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3275 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3276 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3278 /* ...and remove all record of the association. */
3279 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3280 return SCTP_DISPOSITION_DELETE_TCB;
3282 nomem_chunk:
3283 sctp_ulpevent_free(ev);
3284 nomem:
3285 return SCTP_DISPOSITION_NOMEM;
3289 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3291 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3292 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3293 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3294 * packet must fill in the Verification Tag field of the outbound
3295 * packet with the Verification Tag received in the SHUTDOWN ACK and
3296 * set the T-bit in the Chunk Flags to indicate that the Verification
3297 * Tag is reflected.
3299 * 8) The receiver should respond to the sender of the OOTB packet with
3300 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3301 * MUST fill in the Verification Tag field of the outbound packet
3302 * with the value found in the Verification Tag field of the OOTB
3303 * packet and set the T-bit in the Chunk Flags to indicate that the
3304 * Verification Tag is reflected. After sending this ABORT, the
3305 * receiver of the OOTB packet shall discard the OOTB packet and take
3306 * no further action.
3308 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3309 const struct sctp_association *asoc,
3310 const sctp_subtype_t type,
3311 void *arg,
3312 sctp_cmd_seq_t *commands)
3314 struct sctp_chunk *chunk = arg;
3315 struct sk_buff *skb = chunk->skb;
3316 sctp_chunkhdr_t *ch;
3317 __u8 *ch_end;
3318 int ootb_shut_ack = 0;
3320 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3322 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3323 do {
3324 /* Report violation if the chunk is less then minimal */
3325 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3326 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3327 commands);
3329 /* Now that we know we at least have a chunk header,
3330 * do things that are type appropriate.
3332 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3333 ootb_shut_ack = 1;
3335 /* RFC 2960, Section 3.3.7
3336 * Moreover, under any circumstances, an endpoint that
3337 * receives an ABORT MUST NOT respond to that ABORT by
3338 * sending an ABORT of its own.
3340 if (SCTP_CID_ABORT == ch->type)
3341 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3343 /* Report violation if chunk len overflows */
3344 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3345 if (ch_end > skb_tail_pointer(skb))
3346 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3347 commands);
3349 ch = (sctp_chunkhdr_t *) ch_end;
3350 } while (ch_end < skb_tail_pointer(skb));
3352 if (ootb_shut_ack)
3353 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3354 else
3355 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3359 * Handle an "Out of the blue" SHUTDOWN ACK.
3361 * Section: 8.4 5, sctpimpguide 2.41.
3363 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3364 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3365 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3366 * packet must fill in the Verification Tag field of the outbound
3367 * packet with the Verification Tag received in the SHUTDOWN ACK and
3368 * set the T-bit in the Chunk Flags to indicate that the Verification
3369 * Tag is reflected.
3371 * Inputs
3372 * (endpoint, asoc, type, arg, commands)
3374 * Outputs
3375 * (sctp_disposition_t)
3377 * The return value is the disposition of the chunk.
3379 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3380 const struct sctp_association *asoc,
3381 const sctp_subtype_t type,
3382 void *arg,
3383 sctp_cmd_seq_t *commands)
3385 struct sctp_packet *packet = NULL;
3386 struct sctp_chunk *chunk = arg;
3387 struct sctp_chunk *shut;
3389 packet = sctp_ootb_pkt_new(asoc, chunk);
3391 if (packet) {
3392 /* Make an SHUTDOWN_COMPLETE.
3393 * The T bit will be set if the asoc is NULL.
3395 shut = sctp_make_shutdown_complete(asoc, chunk);
3396 if (!shut) {
3397 sctp_ootb_pkt_free(packet);
3398 return SCTP_DISPOSITION_NOMEM;
3401 /* Reflect vtag if T-Bit is set */
3402 if (sctp_test_T_bit(shut))
3403 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3405 /* Set the skb to the belonging sock for accounting. */
3406 shut->skb->sk = ep->base.sk;
3408 sctp_packet_append_chunk(packet, shut);
3410 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3411 SCTP_PACKET(packet));
3413 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3415 /* If the chunk length is invalid, we don't want to process
3416 * the reset of the packet.
3418 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3419 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3421 /* We need to discard the rest of the packet to prevent
3422 * potential bomming attacks from additional bundled chunks.
3423 * This is documented in SCTP Threats ID.
3425 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3428 return SCTP_DISPOSITION_NOMEM;
3432 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3434 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3435 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3436 * procedures in section 8.4 SHOULD be followed, in other words it
3437 * should be treated as an Out Of The Blue packet.
3438 * [This means that we do NOT check the Verification Tag on these
3439 * chunks. --piggy ]
3442 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3443 const struct sctp_association *asoc,
3444 const sctp_subtype_t type,
3445 void *arg,
3446 sctp_cmd_seq_t *commands)
3448 struct sctp_chunk *chunk = arg;
3450 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3451 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3452 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3453 commands);
3455 /* Although we do have an association in this case, it corresponds
3456 * to a restarted association. So the packet is treated as an OOTB
3457 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3458 * called with a NULL association.
3460 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3462 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3465 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3466 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3467 const struct sctp_association *asoc,
3468 const sctp_subtype_t type, void *arg,
3469 sctp_cmd_seq_t *commands)
3471 struct sctp_chunk *chunk = arg;
3472 struct sctp_chunk *asconf_ack = NULL;
3473 struct sctp_paramhdr *err_param = NULL;
3474 sctp_addiphdr_t *hdr;
3475 union sctp_addr_param *addr_param;
3476 __u32 serial;
3477 int length;
3479 if (!sctp_vtag_verify(chunk, asoc)) {
3480 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3481 SCTP_NULL());
3482 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3485 /* ADD-IP: Section 4.1.1
3486 * This chunk MUST be sent in an authenticated way by using
3487 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3488 * is received unauthenticated it MUST be silently discarded as
3489 * described in [I-D.ietf-tsvwg-sctp-auth].
3491 if (!sctp_addip_noauth && !chunk->auth)
3492 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3494 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3495 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3496 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3497 commands);
3499 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3500 serial = ntohl(hdr->serial);
3502 addr_param = (union sctp_addr_param *)hdr->params;
3503 length = ntohs(addr_param->p.length);
3504 if (length < sizeof(sctp_paramhdr_t))
3505 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3506 (void *)addr_param, commands);
3508 /* Verify the ASCONF chunk before processing it. */
3509 if (!sctp_verify_asconf(asoc,
3510 (sctp_paramhdr_t *)((void *)addr_param + length),
3511 (void *)chunk->chunk_end,
3512 &err_param))
3513 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3514 (void *)err_param, commands);
3516 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3517 * the endpoint stored in a new association variable
3518 * 'Peer-Serial-Number'.
3520 if (serial == asoc->peer.addip_serial + 1) {
3521 /* If this is the first instance of ASCONF in the packet,
3522 * we can clean our old ASCONF-ACKs.
3524 if (!chunk->has_asconf)
3525 sctp_assoc_clean_asconf_ack_cache(asoc);
3527 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3528 * expected, process the ASCONF as described below and after
3529 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3530 * the response packet and cache a copy of it (in the event it
3531 * later needs to be retransmitted).
3533 * Essentially, do V1-V5.
3535 asconf_ack = sctp_process_asconf((struct sctp_association *)
3536 asoc, chunk);
3537 if (!asconf_ack)
3538 return SCTP_DISPOSITION_NOMEM;
3539 } else if (serial < asoc->peer.addip_serial + 1) {
3540 /* ADDIP 5.2 E2)
3541 * If the value found in the Sequence Number is less than the
3542 * ('Peer- Sequence-Number' + 1), simply skip to the next
3543 * ASCONF, and include in the outbound response packet
3544 * any previously cached ASCONF-ACK response that was
3545 * sent and saved that matches the Sequence Number of the
3546 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3547 * Chunk exists. This will occur when an older ASCONF
3548 * arrives out of order. In such a case, the receiver
3549 * should skip the ASCONF Chunk and not include ASCONF-ACK
3550 * Chunk for that chunk.
3552 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3553 if (!asconf_ack)
3554 return SCTP_DISPOSITION_DISCARD;
3556 /* Reset the transport so that we select the correct one
3557 * this time around. This is to make sure that we don't
3558 * accidentally use a stale transport that's been removed.
3560 asconf_ack->transport = NULL;
3561 } else {
3562 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3563 * it must be either a stale packet or from an attacker.
3565 return SCTP_DISPOSITION_DISCARD;
3568 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3569 * containing the ASCONF-ACK Chunks MUST be the source address of
3570 * the SCTP packet that held the ASCONF Chunks.
3572 * To do this properly, we'll set the destination address of the chunk
3573 * and at the transmit time, will try look up the transport to use.
3574 * Since ASCONFs may be bundled, the correct transport may not be
3575 * created untill we process the entire packet, thus this workaround.
3577 asconf_ack->dest = chunk->source;
3578 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3580 return SCTP_DISPOSITION_CONSUME;
3584 * ADDIP Section 4.3 General rules for address manipulation
3585 * When building TLV parameters for the ASCONF Chunk that will add or
3586 * delete IP addresses the D0 to D13 rules should be applied:
3588 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3589 const struct sctp_association *asoc,
3590 const sctp_subtype_t type, void *arg,
3591 sctp_cmd_seq_t *commands)
3593 struct sctp_chunk *asconf_ack = arg;
3594 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3595 struct sctp_chunk *abort;
3596 struct sctp_paramhdr *err_param = NULL;
3597 sctp_addiphdr_t *addip_hdr;
3598 __u32 sent_serial, rcvd_serial;
3600 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3601 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3602 SCTP_NULL());
3603 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3606 /* ADD-IP, Section 4.1.2:
3607 * This chunk MUST be sent in an authenticated way by using
3608 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3609 * is received unauthenticated it MUST be silently discarded as
3610 * described in [I-D.ietf-tsvwg-sctp-auth].
3612 if (!sctp_addip_noauth && !asconf_ack->auth)
3613 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3615 /* Make sure that the ADDIP chunk has a valid length. */
3616 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3617 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3618 commands);
3620 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3621 rcvd_serial = ntohl(addip_hdr->serial);
3623 /* Verify the ASCONF-ACK chunk before processing it. */
3624 if (!sctp_verify_asconf(asoc,
3625 (sctp_paramhdr_t *)addip_hdr->params,
3626 (void *)asconf_ack->chunk_end,
3627 &err_param))
3628 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3629 (void *)err_param, commands);
3631 if (last_asconf) {
3632 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3633 sent_serial = ntohl(addip_hdr->serial);
3634 } else {
3635 sent_serial = asoc->addip_serial - 1;
3638 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3639 * equal to the next serial number to be used but no ASCONF chunk is
3640 * outstanding the endpoint MUST ABORT the association. Note that a
3641 * sequence number is greater than if it is no more than 2^^31-1
3642 * larger than the current sequence number (using serial arithmetic).
3644 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3645 !(asoc->addip_last_asconf)) {
3646 abort = sctp_make_abort(asoc, asconf_ack,
3647 sizeof(sctp_errhdr_t));
3648 if (abort) {
3649 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3650 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3651 SCTP_CHUNK(abort));
3653 /* We are going to ABORT, so we might as well stop
3654 * processing the rest of the chunks in the packet.
3656 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3657 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3658 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3659 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3660 SCTP_ERROR(ECONNABORTED));
3661 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3662 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3663 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3664 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3665 return SCTP_DISPOSITION_ABORT;
3668 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3669 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3670 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3672 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3673 asconf_ack)) {
3674 /* Successfully processed ASCONF_ACK. We can
3675 * release the next asconf if we have one.
3677 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3678 SCTP_NULL());
3679 return SCTP_DISPOSITION_CONSUME;
3682 abort = sctp_make_abort(asoc, asconf_ack,
3683 sizeof(sctp_errhdr_t));
3684 if (abort) {
3685 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3686 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3687 SCTP_CHUNK(abort));
3689 /* We are going to ABORT, so we might as well stop
3690 * processing the rest of the chunks in the packet.
3692 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3693 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3694 SCTP_ERROR(ECONNABORTED));
3695 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3696 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3697 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3698 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3699 return SCTP_DISPOSITION_ABORT;
3702 return SCTP_DISPOSITION_DISCARD;
3706 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3708 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3709 * its cumulative TSN point to the value carried in the FORWARD TSN
3710 * chunk, and then MUST further advance its cumulative TSN point locally
3711 * if possible.
3712 * After the above processing, the data receiver MUST stop reporting any
3713 * missing TSNs earlier than or equal to the new cumulative TSN point.
3715 * Verification Tag: 8.5 Verification Tag [Normal verification]
3717 * The return value is the disposition of the chunk.
3719 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3720 const struct sctp_association *asoc,
3721 const sctp_subtype_t type,
3722 void *arg,
3723 sctp_cmd_seq_t *commands)
3725 struct sctp_chunk *chunk = arg;
3726 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3727 struct sctp_fwdtsn_skip *skip;
3728 __u16 len;
3729 __u32 tsn;
3731 if (!sctp_vtag_verify(chunk, asoc)) {
3732 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3733 SCTP_NULL());
3734 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3737 /* Make sure that the FORWARD_TSN chunk has valid length. */
3738 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3739 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3740 commands);
3742 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3743 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3744 len = ntohs(chunk->chunk_hdr->length);
3745 len -= sizeof(struct sctp_chunkhdr);
3746 skb_pull(chunk->skb, len);
3748 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3749 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3751 /* The TSN is too high--silently discard the chunk and count on it
3752 * getting retransmitted later.
3754 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3755 goto discard_noforce;
3757 /* Silently discard the chunk if stream-id is not valid */
3758 sctp_walk_fwdtsn(skip, chunk) {
3759 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3760 goto discard_noforce;
3763 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3764 if (len > sizeof(struct sctp_fwdtsn_hdr))
3765 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3766 SCTP_CHUNK(chunk));
3768 /* Count this as receiving DATA. */
3769 if (asoc->autoclose) {
3770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3771 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3774 /* FIXME: For now send a SACK, but DATA processing may
3775 * send another.
3777 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3779 return SCTP_DISPOSITION_CONSUME;
3781 discard_noforce:
3782 return SCTP_DISPOSITION_DISCARD;
3785 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3786 const struct sctp_endpoint *ep,
3787 const struct sctp_association *asoc,
3788 const sctp_subtype_t type,
3789 void *arg,
3790 sctp_cmd_seq_t *commands)
3792 struct sctp_chunk *chunk = arg;
3793 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3794 struct sctp_fwdtsn_skip *skip;
3795 __u16 len;
3796 __u32 tsn;
3798 if (!sctp_vtag_verify(chunk, asoc)) {
3799 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3800 SCTP_NULL());
3801 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3804 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3805 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3806 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3807 commands);
3809 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3810 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3811 len = ntohs(chunk->chunk_hdr->length);
3812 len -= sizeof(struct sctp_chunkhdr);
3813 skb_pull(chunk->skb, len);
3815 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3816 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3818 /* The TSN is too high--silently discard the chunk and count on it
3819 * getting retransmitted later.
3821 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3822 goto gen_shutdown;
3824 /* Silently discard the chunk if stream-id is not valid */
3825 sctp_walk_fwdtsn(skip, chunk) {
3826 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3827 goto gen_shutdown;
3830 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3831 if (len > sizeof(struct sctp_fwdtsn_hdr))
3832 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3833 SCTP_CHUNK(chunk));
3835 /* Go a head and force a SACK, since we are shutting down. */
3836 gen_shutdown:
3837 /* Implementor's Guide.
3839 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3840 * respond to each received packet containing one or more DATA chunk(s)
3841 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3843 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3844 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3845 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3846 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3848 return SCTP_DISPOSITION_CONSUME;
3852 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3854 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3855 * Identifier field. If this algorithm was not specified by the
3856 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3857 * during association setup, the AUTH chunk and all chunks after it MUST
3858 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3859 * defined in Section 4.1.
3861 * If an endpoint with no shared key receives a Shared Key Identifier
3862 * other than 0, it MUST silently discard all authenticated chunks. If
3863 * the endpoint has at least one endpoint pair shared key for the peer,
3864 * it MUST use the key specified by the Shared Key Identifier if a
3865 * key has been configured for that Shared Key Identifier. If no
3866 * endpoint pair shared key has been configured for that Shared Key
3867 * Identifier, all authenticated chunks MUST be silently discarded.
3869 * Verification Tag: 8.5 Verification Tag [Normal verification]
3871 * The return value is the disposition of the chunk.
3873 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3874 const struct sctp_association *asoc,
3875 const sctp_subtype_t type,
3876 struct sctp_chunk *chunk)
3878 struct sctp_authhdr *auth_hdr;
3879 struct sctp_hmac *hmac;
3880 unsigned int sig_len;
3881 __u16 key_id;
3882 __u8 *save_digest;
3883 __u8 *digest;
3885 /* Pull in the auth header, so we can do some more verification */
3886 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3887 chunk->subh.auth_hdr = auth_hdr;
3888 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3890 /* Make sure that we suport the HMAC algorithm from the auth
3891 * chunk.
3893 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3894 return SCTP_IERROR_AUTH_BAD_HMAC;
3896 /* Make sure that the provided shared key identifier has been
3897 * configured
3899 key_id = ntohs(auth_hdr->shkey_id);
3900 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3901 return SCTP_IERROR_AUTH_BAD_KEYID;
3904 /* Make sure that the length of the signature matches what
3905 * we expect.
3907 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3908 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3909 if (sig_len != hmac->hmac_len)
3910 return SCTP_IERROR_PROTO_VIOLATION;
3912 /* Now that we've done validation checks, we can compute and
3913 * verify the hmac. The steps involved are:
3914 * 1. Save the digest from the chunk.
3915 * 2. Zero out the digest in the chunk.
3916 * 3. Compute the new digest
3917 * 4. Compare saved and new digests.
3919 digest = auth_hdr->hmac;
3920 skb_pull(chunk->skb, sig_len);
3922 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3923 if (!save_digest)
3924 goto nomem;
3926 memset(digest, 0, sig_len);
3928 sctp_auth_calculate_hmac(asoc, chunk->skb,
3929 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3930 GFP_ATOMIC);
3932 /* Discard the packet if the digests do not match */
3933 if (memcmp(save_digest, digest, sig_len)) {
3934 kfree(save_digest);
3935 return SCTP_IERROR_BAD_SIG;
3938 kfree(save_digest);
3939 chunk->auth = 1;
3941 return SCTP_IERROR_NO_ERROR;
3942 nomem:
3943 return SCTP_IERROR_NOMEM;
3946 sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3947 const struct sctp_association *asoc,
3948 const sctp_subtype_t type,
3949 void *arg,
3950 sctp_cmd_seq_t *commands)
3952 struct sctp_authhdr *auth_hdr;
3953 struct sctp_chunk *chunk = arg;
3954 struct sctp_chunk *err_chunk;
3955 sctp_ierror_t error;
3957 /* Make sure that the peer has AUTH capable */
3958 if (!asoc->peer.auth_capable)
3959 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3961 if (!sctp_vtag_verify(chunk, asoc)) {
3962 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3963 SCTP_NULL());
3964 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3967 /* Make sure that the AUTH chunk has valid length. */
3968 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3969 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3970 commands);
3972 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3973 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3974 switch (error) {
3975 case SCTP_IERROR_AUTH_BAD_HMAC:
3976 /* Generate the ERROR chunk and discard the rest
3977 * of the packet
3979 err_chunk = sctp_make_op_error(asoc, chunk,
3980 SCTP_ERROR_UNSUP_HMAC,
3981 &auth_hdr->hmac_id,
3982 sizeof(__u16));
3983 if (err_chunk) {
3984 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3985 SCTP_CHUNK(err_chunk));
3987 /* Fall Through */
3988 case SCTP_IERROR_AUTH_BAD_KEYID:
3989 case SCTP_IERROR_BAD_SIG:
3990 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3991 break;
3992 case SCTP_IERROR_PROTO_VIOLATION:
3993 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3994 commands);
3995 break;
3996 case SCTP_IERROR_NOMEM:
3997 return SCTP_DISPOSITION_NOMEM;
3998 default:
3999 break;
4002 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4003 struct sctp_ulpevent *ev;
4005 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4006 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4008 if (!ev)
4009 return -ENOMEM;
4011 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4012 SCTP_ULPEVENT(ev));
4015 return SCTP_DISPOSITION_CONSUME;
4019 * Process an unknown chunk.
4021 * Section: 3.2. Also, 2.1 in the implementor's guide.
4023 * Chunk Types are encoded such that the highest-order two bits specify
4024 * the action that must be taken if the processing endpoint does not
4025 * recognize the Chunk Type.
4027 * 00 - Stop processing this SCTP packet and discard it, do not process
4028 * any further chunks within it.
4030 * 01 - Stop processing this SCTP packet and discard it, do not process
4031 * any further chunks within it, and report the unrecognized
4032 * chunk in an 'Unrecognized Chunk Type'.
4034 * 10 - Skip this chunk and continue processing.
4036 * 11 - Skip this chunk and continue processing, but report in an ERROR
4037 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4039 * The return value is the disposition of the chunk.
4041 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4042 const struct sctp_association *asoc,
4043 const sctp_subtype_t type,
4044 void *arg,
4045 sctp_cmd_seq_t *commands)
4047 struct sctp_chunk *unk_chunk = arg;
4048 struct sctp_chunk *err_chunk;
4049 sctp_chunkhdr_t *hdr;
4051 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4053 if (!sctp_vtag_verify(unk_chunk, asoc))
4054 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4056 /* Make sure that the chunk has a valid length.
4057 * Since we don't know the chunk type, we use a general
4058 * chunkhdr structure to make a comparison.
4060 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4061 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4062 commands);
4064 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4065 case SCTP_CID_ACTION_DISCARD:
4066 /* Discard the packet. */
4067 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4068 break;
4069 case SCTP_CID_ACTION_DISCARD_ERR:
4070 /* Generate an ERROR chunk as response. */
4071 hdr = unk_chunk->chunk_hdr;
4072 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4073 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4074 WORD_ROUND(ntohs(hdr->length)));
4075 if (err_chunk) {
4076 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4077 SCTP_CHUNK(err_chunk));
4080 /* Discard the packet. */
4081 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4082 return SCTP_DISPOSITION_CONSUME;
4083 break;
4084 case SCTP_CID_ACTION_SKIP:
4085 /* Skip the chunk. */
4086 return SCTP_DISPOSITION_DISCARD;
4087 break;
4088 case SCTP_CID_ACTION_SKIP_ERR:
4089 /* Generate an ERROR chunk as response. */
4090 hdr = unk_chunk->chunk_hdr;
4091 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4092 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4093 WORD_ROUND(ntohs(hdr->length)));
4094 if (err_chunk) {
4095 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4096 SCTP_CHUNK(err_chunk));
4098 /* Skip the chunk. */
4099 return SCTP_DISPOSITION_CONSUME;
4100 break;
4101 default:
4102 break;
4105 return SCTP_DISPOSITION_DISCARD;
4109 * Discard the chunk.
4111 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4112 * [Too numerous to mention...]
4113 * Verification Tag: No verification needed.
4114 * Inputs
4115 * (endpoint, asoc, chunk)
4117 * Outputs
4118 * (asoc, reply_msg, msg_up, timers, counters)
4120 * The return value is the disposition of the chunk.
4122 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4123 const struct sctp_association *asoc,
4124 const sctp_subtype_t type,
4125 void *arg,
4126 sctp_cmd_seq_t *commands)
4128 struct sctp_chunk *chunk = arg;
4130 /* Make sure that the chunk has a valid length.
4131 * Since we don't know the chunk type, we use a general
4132 * chunkhdr structure to make a comparison.
4134 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4135 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4136 commands);
4138 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4139 return SCTP_DISPOSITION_DISCARD;
4143 * Discard the whole packet.
4145 * Section: 8.4 2)
4147 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4148 * silently discard the OOTB packet and take no further action.
4150 * Verification Tag: No verification necessary
4152 * Inputs
4153 * (endpoint, asoc, chunk)
4155 * Outputs
4156 * (asoc, reply_msg, msg_up, timers, counters)
4158 * The return value is the disposition of the chunk.
4160 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4161 const struct sctp_association *asoc,
4162 const sctp_subtype_t type,
4163 void *arg,
4164 sctp_cmd_seq_t *commands)
4166 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
4167 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4169 return SCTP_DISPOSITION_CONSUME;
4174 * The other end is violating protocol.
4176 * Section: Not specified
4177 * Verification Tag: Not specified
4178 * Inputs
4179 * (endpoint, asoc, chunk)
4181 * Outputs
4182 * (asoc, reply_msg, msg_up, timers, counters)
4184 * We simply tag the chunk as a violation. The state machine will log
4185 * the violation and continue.
4187 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4188 const struct sctp_association *asoc,
4189 const sctp_subtype_t type,
4190 void *arg,
4191 sctp_cmd_seq_t *commands)
4193 struct sctp_chunk *chunk = arg;
4195 /* Make sure that the chunk has a valid length. */
4196 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4197 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4198 commands);
4200 return SCTP_DISPOSITION_VIOLATION;
4204 * Common function to handle a protocol violation.
4206 static sctp_disposition_t sctp_sf_abort_violation(
4207 const struct sctp_endpoint *ep,
4208 const struct sctp_association *asoc,
4209 void *arg,
4210 sctp_cmd_seq_t *commands,
4211 const __u8 *payload,
4212 const size_t paylen)
4214 struct sctp_packet *packet = NULL;
4215 struct sctp_chunk *chunk = arg;
4216 struct sctp_chunk *abort = NULL;
4218 /* SCTP-AUTH, Section 6.3:
4219 * It should be noted that if the receiver wants to tear
4220 * down an association in an authenticated way only, the
4221 * handling of malformed packets should not result in
4222 * tearing down the association.
4224 * This means that if we only want to abort associations
4225 * in an authenticated way (i.e AUTH+ABORT), then we
4226 * can't destroy this association just becuase the packet
4227 * was malformed.
4229 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4230 goto discard;
4232 /* Make the abort chunk. */
4233 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4234 if (!abort)
4235 goto nomem;
4237 if (asoc) {
4238 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4239 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4240 !asoc->peer.i.init_tag) {
4241 sctp_initack_chunk_t *initack;
4243 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4244 if (!sctp_chunk_length_valid(chunk,
4245 sizeof(sctp_initack_chunk_t)))
4246 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4247 else {
4248 unsigned int inittag;
4250 inittag = ntohl(initack->init_hdr.init_tag);
4251 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4252 SCTP_U32(inittag));
4256 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4257 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4259 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4261 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4262 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4263 SCTP_ERROR(ECONNREFUSED));
4264 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4265 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4266 } else {
4267 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4268 SCTP_ERROR(ECONNABORTED));
4269 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4270 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4271 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4273 } else {
4274 packet = sctp_ootb_pkt_new(asoc, chunk);
4276 if (!packet)
4277 goto nomem_pkt;
4279 if (sctp_test_T_bit(abort))
4280 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4282 abort->skb->sk = ep->base.sk;
4284 sctp_packet_append_chunk(packet, abort);
4286 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4287 SCTP_PACKET(packet));
4289 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4292 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4294 discard:
4295 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4296 return SCTP_DISPOSITION_ABORT;
4298 nomem_pkt:
4299 sctp_chunk_free(abort);
4300 nomem:
4301 return SCTP_DISPOSITION_NOMEM;
4305 * Handle a protocol violation when the chunk length is invalid.
4306 * "Invalid" length is identified as smaller than the minimal length a
4307 * given chunk can be. For example, a SACK chunk has invalid length
4308 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4310 * We inform the other end by sending an ABORT with a Protocol Violation
4311 * error code.
4313 * Section: Not specified
4314 * Verification Tag: Nothing to do
4315 * Inputs
4316 * (endpoint, asoc, chunk)
4318 * Outputs
4319 * (reply_msg, msg_up, counters)
4321 * Generate an ABORT chunk and terminate the association.
4323 static sctp_disposition_t sctp_sf_violation_chunklen(
4324 const struct sctp_endpoint *ep,
4325 const struct sctp_association *asoc,
4326 const sctp_subtype_t type,
4327 void *arg,
4328 sctp_cmd_seq_t *commands)
4330 static const char err_str[]="The following chunk had invalid length:";
4332 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4333 sizeof(err_str));
4337 * Handle a protocol violation when the parameter length is invalid.
4338 * "Invalid" length is identified as smaller than the minimal length a
4339 * given parameter can be.
4341 static sctp_disposition_t sctp_sf_violation_paramlen(
4342 const struct sctp_endpoint *ep,
4343 const struct sctp_association *asoc,
4344 const sctp_subtype_t type,
4345 void *arg, void *ext,
4346 sctp_cmd_seq_t *commands)
4348 struct sctp_chunk *chunk = arg;
4349 struct sctp_paramhdr *param = ext;
4350 struct sctp_chunk *abort = NULL;
4352 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4353 goto discard;
4355 /* Make the abort chunk. */
4356 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4357 if (!abort)
4358 goto nomem;
4360 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4361 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4363 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4364 SCTP_ERROR(ECONNABORTED));
4365 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4366 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4367 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4368 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4370 discard:
4371 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4372 return SCTP_DISPOSITION_ABORT;
4373 nomem:
4374 return SCTP_DISPOSITION_NOMEM;
4377 /* Handle a protocol violation when the peer trying to advance the
4378 * cumulative tsn ack to a point beyond the max tsn currently sent.
4380 * We inform the other end by sending an ABORT with a Protocol Violation
4381 * error code.
4383 static sctp_disposition_t sctp_sf_violation_ctsn(
4384 const struct sctp_endpoint *ep,
4385 const struct sctp_association *asoc,
4386 const sctp_subtype_t type,
4387 void *arg,
4388 sctp_cmd_seq_t *commands)
4390 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4392 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4393 sizeof(err_str));
4396 /* Handle protocol violation of an invalid chunk bundling. For example,
4397 * when we have an association and we recieve bundled INIT-ACK, or
4398 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4399 * statement from the specs. Additinally, there might be an attacker
4400 * on the path and we may not want to continue this communication.
4402 static sctp_disposition_t sctp_sf_violation_chunk(
4403 const struct sctp_endpoint *ep,
4404 const struct sctp_association *asoc,
4405 const sctp_subtype_t type,
4406 void *arg,
4407 sctp_cmd_seq_t *commands)
4409 static const char err_str[]="The following chunk violates protocol:";
4411 if (!asoc)
4412 return sctp_sf_violation(ep, asoc, type, arg, commands);
4414 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4415 sizeof(err_str));
4417 /***************************************************************************
4418 * These are the state functions for handling primitive (Section 10) events.
4419 ***************************************************************************/
4421 * sctp_sf_do_prm_asoc
4423 * Section: 10.1 ULP-to-SCTP
4424 * B) Associate
4426 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4427 * outbound stream count)
4428 * -> association id [,destination transport addr list] [,outbound stream
4429 * count]
4431 * This primitive allows the upper layer to initiate an association to a
4432 * specific peer endpoint.
4434 * The peer endpoint shall be specified by one of the transport addresses
4435 * which defines the endpoint (see Section 1.4). If the local SCTP
4436 * instance has not been initialized, the ASSOCIATE is considered an
4437 * error.
4438 * [This is not relevant for the kernel implementation since we do all
4439 * initialization at boot time. It we hadn't initialized we wouldn't
4440 * get anywhere near this code.]
4442 * An association id, which is a local handle to the SCTP association,
4443 * will be returned on successful establishment of the association. If
4444 * SCTP is not able to open an SCTP association with the peer endpoint,
4445 * an error is returned.
4446 * [In the kernel implementation, the struct sctp_association needs to
4447 * be created BEFORE causing this primitive to run.]
4449 * Other association parameters may be returned, including the
4450 * complete destination transport addresses of the peer as well as the
4451 * outbound stream count of the local endpoint. One of the transport
4452 * address from the returned destination addresses will be selected by
4453 * the local endpoint as default primary path for sending SCTP packets
4454 * to this peer. The returned "destination transport addr list" can
4455 * be used by the ULP to change the default primary path or to force
4456 * sending a packet to a specific transport address. [All of this
4457 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4458 * function.]
4460 * Mandatory attributes:
4462 * o local SCTP instance name - obtained from the INITIALIZE operation.
4463 * [This is the argument asoc.]
4464 * o destination transport addr - specified as one of the transport
4465 * addresses of the peer endpoint with which the association is to be
4466 * established.
4467 * [This is asoc->peer.active_path.]
4468 * o outbound stream count - the number of outbound streams the ULP
4469 * would like to open towards this peer endpoint.
4470 * [BUG: This is not currently implemented.]
4471 * Optional attributes:
4473 * None.
4475 * The return value is a disposition.
4477 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4478 const struct sctp_association *asoc,
4479 const sctp_subtype_t type,
4480 void *arg,
4481 sctp_cmd_seq_t *commands)
4483 struct sctp_chunk *repl;
4484 struct sctp_association* my_asoc;
4486 /* The comment below says that we enter COOKIE-WAIT AFTER
4487 * sending the INIT, but that doesn't actually work in our
4488 * implementation...
4490 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4491 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4493 /* RFC 2960 5.1 Normal Establishment of an Association
4495 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4496 * must provide its Verification Tag (Tag_A) in the Initiate
4497 * Tag field. Tag_A SHOULD be a random number in the range of
4498 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4501 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4502 if (!repl)
4503 goto nomem;
4505 /* Cast away the const modifier, as we want to just
4506 * rerun it through as a sideffect.
4508 my_asoc = (struct sctp_association *)asoc;
4509 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4511 /* Choose transport for INIT. */
4512 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4513 SCTP_CHUNK(repl));
4515 /* After sending the INIT, "A" starts the T1-init timer and
4516 * enters the COOKIE-WAIT state.
4518 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4519 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4520 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4521 return SCTP_DISPOSITION_CONSUME;
4523 nomem:
4524 return SCTP_DISPOSITION_NOMEM;
4528 * Process the SEND primitive.
4530 * Section: 10.1 ULP-to-SCTP
4531 * E) Send
4533 * Format: SEND(association id, buffer address, byte count [,context]
4534 * [,stream id] [,life time] [,destination transport address]
4535 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4536 * -> result
4538 * This is the main method to send user data via SCTP.
4540 * Mandatory attributes:
4542 * o association id - local handle to the SCTP association
4544 * o buffer address - the location where the user message to be
4545 * transmitted is stored;
4547 * o byte count - The size of the user data in number of bytes;
4549 * Optional attributes:
4551 * o context - an optional 32 bit integer that will be carried in the
4552 * sending failure notification to the ULP if the transportation of
4553 * this User Message fails.
4555 * o stream id - to indicate which stream to send the data on. If not
4556 * specified, stream 0 will be used.
4558 * o life time - specifies the life time of the user data. The user data
4559 * will not be sent by SCTP after the life time expires. This
4560 * parameter can be used to avoid efforts to transmit stale
4561 * user messages. SCTP notifies the ULP if the data cannot be
4562 * initiated to transport (i.e. sent to the destination via SCTP's
4563 * send primitive) within the life time variable. However, the
4564 * user data will be transmitted if SCTP has attempted to transmit a
4565 * chunk before the life time expired.
4567 * o destination transport address - specified as one of the destination
4568 * transport addresses of the peer endpoint to which this packet
4569 * should be sent. Whenever possible, SCTP should use this destination
4570 * transport address for sending the packets, instead of the current
4571 * primary path.
4573 * o unorder flag - this flag, if present, indicates that the user
4574 * would like the data delivered in an unordered fashion to the peer
4575 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4576 * message).
4578 * o no-bundle flag - instructs SCTP not to bundle this user data with
4579 * other outbound DATA chunks. SCTP MAY still bundle even when
4580 * this flag is present, when faced with network congestion.
4582 * o payload protocol-id - A 32 bit unsigned integer that is to be
4583 * passed to the peer indicating the type of payload protocol data
4584 * being transmitted. This value is passed as opaque data by SCTP.
4586 * The return value is the disposition.
4588 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4589 const struct sctp_association *asoc,
4590 const sctp_subtype_t type,
4591 void *arg,
4592 sctp_cmd_seq_t *commands)
4594 struct sctp_datamsg *msg = arg;
4596 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4597 return SCTP_DISPOSITION_CONSUME;
4601 * Process the SHUTDOWN primitive.
4603 * Section: 10.1:
4604 * C) Shutdown
4606 * Format: SHUTDOWN(association id)
4607 * -> result
4609 * Gracefully closes an association. Any locally queued user data
4610 * will be delivered to the peer. The association will be terminated only
4611 * after the peer acknowledges all the SCTP packets sent. A success code
4612 * will be returned on successful termination of the association. If
4613 * attempting to terminate the association results in a failure, an error
4614 * code shall be returned.
4616 * Mandatory attributes:
4618 * o association id - local handle to the SCTP association
4620 * Optional attributes:
4622 * None.
4624 * The return value is the disposition.
4626 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4627 const struct sctp_endpoint *ep,
4628 const struct sctp_association *asoc,
4629 const sctp_subtype_t type,
4630 void *arg,
4631 sctp_cmd_seq_t *commands)
4633 int disposition;
4635 /* From 9.2 Shutdown of an Association
4636 * Upon receipt of the SHUTDOWN primitive from its upper
4637 * layer, the endpoint enters SHUTDOWN-PENDING state and
4638 * remains there until all outstanding data has been
4639 * acknowledged by its peer. The endpoint accepts no new data
4640 * from its upper layer, but retransmits data to the far end
4641 * if necessary to fill gaps.
4643 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4644 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4646 disposition = SCTP_DISPOSITION_CONSUME;
4647 if (sctp_outq_is_empty(&asoc->outqueue)) {
4648 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4649 arg, commands);
4651 return disposition;
4655 * Process the ABORT primitive.
4657 * Section: 10.1:
4658 * C) Abort
4660 * Format: Abort(association id [, cause code])
4661 * -> result
4663 * Ungracefully closes an association. Any locally queued user data
4664 * will be discarded and an ABORT chunk is sent to the peer. A success code
4665 * will be returned on successful abortion of the association. If
4666 * attempting to abort the association results in a failure, an error
4667 * code shall be returned.
4669 * Mandatory attributes:
4671 * o association id - local handle to the SCTP association
4673 * Optional attributes:
4675 * o cause code - reason of the abort to be passed to the peer
4677 * None.
4679 * The return value is the disposition.
4681 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4682 const struct sctp_endpoint *ep,
4683 const struct sctp_association *asoc,
4684 const sctp_subtype_t type,
4685 void *arg,
4686 sctp_cmd_seq_t *commands)
4688 /* From 9.1 Abort of an Association
4689 * Upon receipt of the ABORT primitive from its upper
4690 * layer, the endpoint enters CLOSED state and
4691 * discard all outstanding data has been
4692 * acknowledged by its peer. The endpoint accepts no new data
4693 * from its upper layer, but retransmits data to the far end
4694 * if necessary to fill gaps.
4696 struct sctp_chunk *abort = arg;
4697 sctp_disposition_t retval;
4699 retval = SCTP_DISPOSITION_CONSUME;
4701 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4703 /* Even if we can't send the ABORT due to low memory delete the
4704 * TCB. This is a departure from our typical NOMEM handling.
4707 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4708 SCTP_ERROR(ECONNABORTED));
4709 /* Delete the established association. */
4710 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4711 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4713 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4714 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4716 return retval;
4719 /* We tried an illegal operation on an association which is closed. */
4720 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4721 const struct sctp_association *asoc,
4722 const sctp_subtype_t type,
4723 void *arg,
4724 sctp_cmd_seq_t *commands)
4726 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4727 return SCTP_DISPOSITION_CONSUME;
4730 /* We tried an illegal operation on an association which is shutting
4731 * down.
4733 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4734 const struct sctp_association *asoc,
4735 const sctp_subtype_t type,
4736 void *arg,
4737 sctp_cmd_seq_t *commands)
4739 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4740 SCTP_ERROR(-ESHUTDOWN));
4741 return SCTP_DISPOSITION_CONSUME;
4745 * sctp_cookie_wait_prm_shutdown
4747 * Section: 4 Note: 2
4748 * Verification Tag:
4749 * Inputs
4750 * (endpoint, asoc)
4752 * The RFC does not explicitly address this issue, but is the route through the
4753 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4755 * Outputs
4756 * (timers)
4758 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4759 const struct sctp_endpoint *ep,
4760 const struct sctp_association *asoc,
4761 const sctp_subtype_t type,
4762 void *arg,
4763 sctp_cmd_seq_t *commands)
4765 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4766 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4768 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4769 SCTP_STATE(SCTP_STATE_CLOSED));
4771 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4773 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4775 return SCTP_DISPOSITION_DELETE_TCB;
4779 * sctp_cookie_echoed_prm_shutdown
4781 * Section: 4 Note: 2
4782 * Verification Tag:
4783 * Inputs
4784 * (endpoint, asoc)
4786 * The RFC does not explcitly address this issue, but is the route through the
4787 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4789 * Outputs
4790 * (timers)
4792 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4793 const struct sctp_endpoint *ep,
4794 const struct sctp_association *asoc,
4795 const sctp_subtype_t type,
4796 void *arg, sctp_cmd_seq_t *commands)
4798 /* There is a single T1 timer, so we should be able to use
4799 * common function with the COOKIE-WAIT state.
4801 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4805 * sctp_sf_cookie_wait_prm_abort
4807 * Section: 4 Note: 2
4808 * Verification Tag:
4809 * Inputs
4810 * (endpoint, asoc)
4812 * The RFC does not explicitly address this issue, but is the route through the
4813 * state table when someone issues an abort while in COOKIE_WAIT state.
4815 * Outputs
4816 * (timers)
4818 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4819 const struct sctp_endpoint *ep,
4820 const struct sctp_association *asoc,
4821 const sctp_subtype_t type,
4822 void *arg,
4823 sctp_cmd_seq_t *commands)
4825 struct sctp_chunk *abort = arg;
4826 sctp_disposition_t retval;
4828 /* Stop T1-init timer */
4829 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4830 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4831 retval = SCTP_DISPOSITION_CONSUME;
4833 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4835 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4836 SCTP_STATE(SCTP_STATE_CLOSED));
4838 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4840 /* Even if we can't send the ABORT due to low memory delete the
4841 * TCB. This is a departure from our typical NOMEM handling.
4844 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4845 SCTP_ERROR(ECONNREFUSED));
4846 /* Delete the established association. */
4847 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4848 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4850 return retval;
4854 * sctp_sf_cookie_echoed_prm_abort
4856 * Section: 4 Note: 3
4857 * Verification Tag:
4858 * Inputs
4859 * (endpoint, asoc)
4861 * The RFC does not explcitly address this issue, but is the route through the
4862 * state table when someone issues an abort while in COOKIE_ECHOED state.
4864 * Outputs
4865 * (timers)
4867 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4868 const struct sctp_endpoint *ep,
4869 const struct sctp_association *asoc,
4870 const sctp_subtype_t type,
4871 void *arg,
4872 sctp_cmd_seq_t *commands)
4874 /* There is a single T1 timer, so we should be able to use
4875 * common function with the COOKIE-WAIT state.
4877 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4881 * sctp_sf_shutdown_pending_prm_abort
4883 * Inputs
4884 * (endpoint, asoc)
4886 * The RFC does not explicitly address this issue, but is the route through the
4887 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4889 * Outputs
4890 * (timers)
4892 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4893 const struct sctp_endpoint *ep,
4894 const struct sctp_association *asoc,
4895 const sctp_subtype_t type,
4896 void *arg,
4897 sctp_cmd_seq_t *commands)
4899 /* Stop the T5-shutdown guard timer. */
4900 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4901 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4903 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4907 * sctp_sf_shutdown_sent_prm_abort
4909 * Inputs
4910 * (endpoint, asoc)
4912 * The RFC does not explicitly address this issue, but is the route through the
4913 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4915 * Outputs
4916 * (timers)
4918 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4919 const struct sctp_endpoint *ep,
4920 const struct sctp_association *asoc,
4921 const sctp_subtype_t type,
4922 void *arg,
4923 sctp_cmd_seq_t *commands)
4925 /* Stop the T2-shutdown timer. */
4926 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4927 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4929 /* Stop the T5-shutdown guard timer. */
4930 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4931 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4933 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4937 * sctp_sf_cookie_echoed_prm_abort
4939 * Inputs
4940 * (endpoint, asoc)
4942 * The RFC does not explcitly address this issue, but is the route through the
4943 * state table when someone issues an abort while in COOKIE_ECHOED state.
4945 * Outputs
4946 * (timers)
4948 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4949 const struct sctp_endpoint *ep,
4950 const struct sctp_association *asoc,
4951 const sctp_subtype_t type,
4952 void *arg,
4953 sctp_cmd_seq_t *commands)
4955 /* The same T2 timer, so we should be able to use
4956 * common function with the SHUTDOWN-SENT state.
4958 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4962 * Process the REQUESTHEARTBEAT primitive
4964 * 10.1 ULP-to-SCTP
4965 * J) Request Heartbeat
4967 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4969 * -> result
4971 * Instructs the local endpoint to perform a HeartBeat on the specified
4972 * destination transport address of the given association. The returned
4973 * result should indicate whether the transmission of the HEARTBEAT
4974 * chunk to the destination address is successful.
4976 * Mandatory attributes:
4978 * o association id - local handle to the SCTP association
4980 * o destination transport address - the transport address of the
4981 * association on which a heartbeat should be issued.
4983 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4984 const struct sctp_endpoint *ep,
4985 const struct sctp_association *asoc,
4986 const sctp_subtype_t type,
4987 void *arg,
4988 sctp_cmd_seq_t *commands)
4990 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4991 (struct sctp_transport *)arg, commands))
4992 return SCTP_DISPOSITION_NOMEM;
4995 * RFC 2960 (bis), section 8.3
4997 * D) Request an on-demand HEARTBEAT on a specific destination
4998 * transport address of a given association.
5000 * The endpoint should increment the respective error counter of
5001 * the destination transport address each time a HEARTBEAT is sent
5002 * to that address and not acknowledged within one RTO.
5005 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5006 SCTP_TRANSPORT(arg));
5007 return SCTP_DISPOSITION_CONSUME;
5011 * ADDIP Section 4.1 ASCONF Chunk Procedures
5012 * When an endpoint has an ASCONF signaled change to be sent to the
5013 * remote endpoint it should do A1 to A9
5015 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5016 const struct sctp_association *asoc,
5017 const sctp_subtype_t type,
5018 void *arg,
5019 sctp_cmd_seq_t *commands)
5021 struct sctp_chunk *chunk = arg;
5023 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5024 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5025 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5026 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5027 return SCTP_DISPOSITION_CONSUME;
5031 * Ignore the primitive event
5033 * The return value is the disposition of the primitive.
5035 sctp_disposition_t sctp_sf_ignore_primitive(
5036 const struct sctp_endpoint *ep,
5037 const struct sctp_association *asoc,
5038 const sctp_subtype_t type,
5039 void *arg,
5040 sctp_cmd_seq_t *commands)
5042 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5043 return SCTP_DISPOSITION_DISCARD;
5046 /***************************************************************************
5047 * These are the state functions for the OTHER events.
5048 ***************************************************************************/
5051 * Start the shutdown negotiation.
5053 * From Section 9.2:
5054 * Once all its outstanding data has been acknowledged, the endpoint
5055 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5056 * TSN Ack field the last sequential TSN it has received from the peer.
5057 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5058 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5059 * with the updated last sequential TSN received from its peer.
5061 * The return value is the disposition.
5063 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5064 const struct sctp_endpoint *ep,
5065 const struct sctp_association *asoc,
5066 const sctp_subtype_t type,
5067 void *arg,
5068 sctp_cmd_seq_t *commands)
5070 struct sctp_chunk *reply;
5072 /* Once all its outstanding data has been acknowledged, the
5073 * endpoint shall send a SHUTDOWN chunk to its peer including
5074 * in the Cumulative TSN Ack field the last sequential TSN it
5075 * has received from the peer.
5077 reply = sctp_make_shutdown(asoc, NULL);
5078 if (!reply)
5079 goto nomem;
5081 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5082 * T2-shutdown timer.
5084 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5086 /* It shall then start the T2-shutdown timer */
5087 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5088 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5090 /* RFC 4960 Section 9.2
5091 * The sender of the SHUTDOWN MAY also start an overall guard timer
5092 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5094 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5095 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5097 if (asoc->autoclose)
5098 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5099 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5101 /* and enter the SHUTDOWN-SENT state. */
5102 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5103 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5105 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5107 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5108 * or SHUTDOWN-ACK.
5110 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5112 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5114 return SCTP_DISPOSITION_CONSUME;
5116 nomem:
5117 return SCTP_DISPOSITION_NOMEM;
5121 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5123 * From Section 9.2:
5125 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5126 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5127 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5128 * endpoint must re-send the SHUTDOWN ACK.
5130 * The return value is the disposition.
5132 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5133 const struct sctp_endpoint *ep,
5134 const struct sctp_association *asoc,
5135 const sctp_subtype_t type,
5136 void *arg,
5137 sctp_cmd_seq_t *commands)
5139 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5140 struct sctp_chunk *reply;
5142 /* There are 2 ways of getting here:
5143 * 1) called in response to a SHUTDOWN chunk
5144 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5146 * For the case (2), the arg parameter is set to NULL. We need
5147 * to check that we have a chunk before accessing it's fields.
5149 if (chunk) {
5150 if (!sctp_vtag_verify(chunk, asoc))
5151 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5153 /* Make sure that the SHUTDOWN chunk has a valid length. */
5154 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5155 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5156 commands);
5159 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5160 * shall send a SHUTDOWN ACK ...
5162 reply = sctp_make_shutdown_ack(asoc, chunk);
5163 if (!reply)
5164 goto nomem;
5166 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5167 * the T2-shutdown timer.
5169 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5171 /* and start/restart a T2-shutdown timer of its own, */
5172 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5173 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5175 if (asoc->autoclose)
5176 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5177 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5179 /* Enter the SHUTDOWN-ACK-SENT state. */
5180 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5181 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5183 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5185 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5186 * or SHUTDOWN-ACK.
5188 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5190 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5192 return SCTP_DISPOSITION_CONSUME;
5194 nomem:
5195 return SCTP_DISPOSITION_NOMEM;
5199 * Ignore the event defined as other
5201 * The return value is the disposition of the event.
5203 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5204 const struct sctp_association *asoc,
5205 const sctp_subtype_t type,
5206 void *arg,
5207 sctp_cmd_seq_t *commands)
5209 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5210 return SCTP_DISPOSITION_DISCARD;
5213 /************************************************************
5214 * These are the state functions for handling timeout events.
5215 ************************************************************/
5218 * RTX Timeout
5220 * Section: 6.3.3 Handle T3-rtx Expiration
5222 * Whenever the retransmission timer T3-rtx expires for a destination
5223 * address, do the following:
5224 * [See below]
5226 * The return value is the disposition of the chunk.
5228 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5229 const struct sctp_association *asoc,
5230 const sctp_subtype_t type,
5231 void *arg,
5232 sctp_cmd_seq_t *commands)
5234 struct sctp_transport *transport = arg;
5236 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5238 if (asoc->overall_error_count >= asoc->max_retrans) {
5239 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5240 SCTP_ERROR(ETIMEDOUT));
5241 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5242 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5243 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5244 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5245 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5246 return SCTP_DISPOSITION_DELETE_TCB;
5249 /* E1) For the destination address for which the timer
5250 * expires, adjust its ssthresh with rules defined in Section
5251 * 7.2.3 and set the cwnd <- MTU.
5254 /* E2) For the destination address for which the timer
5255 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5256 * maximum value discussed in rule C7 above (RTO.max) may be
5257 * used to provide an upper bound to this doubling operation.
5260 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5261 * outstanding DATA chunks for the address for which the
5262 * T3-rtx has expired will fit into a single packet, subject
5263 * to the MTU constraint for the path corresponding to the
5264 * destination transport address to which the retransmission
5265 * is being sent (this may be different from the address for
5266 * which the timer expires [see Section 6.4]). Call this
5267 * value K. Bundle and retransmit those K DATA chunks in a
5268 * single packet to the destination endpoint.
5270 * Note: Any DATA chunks that were sent to the address for
5271 * which the T3-rtx timer expired but did not fit in one MTU
5272 * (rule E3 above), should be marked for retransmission and
5273 * sent as soon as cwnd allows (normally when a SACK arrives).
5276 /* Do some failure management (Section 8.2). */
5277 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5279 /* NB: Rules E4 and F1 are implicit in R1. */
5280 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5282 return SCTP_DISPOSITION_CONSUME;
5286 * Generate delayed SACK on timeout
5288 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5290 * The guidelines on delayed acknowledgement algorithm specified in
5291 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5292 * acknowledgement SHOULD be generated for at least every second packet
5293 * (not every second DATA chunk) received, and SHOULD be generated
5294 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5295 * some situations it may be beneficial for an SCTP transmitter to be
5296 * more conservative than the algorithms detailed in this document
5297 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5298 * the following algorithms allow.
5300 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5301 const struct sctp_association *asoc,
5302 const sctp_subtype_t type,
5303 void *arg,
5304 sctp_cmd_seq_t *commands)
5306 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
5307 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5308 return SCTP_DISPOSITION_CONSUME;
5312 * sctp_sf_t1_init_timer_expire
5314 * Section: 4 Note: 2
5315 * Verification Tag:
5316 * Inputs
5317 * (endpoint, asoc)
5319 * RFC 2960 Section 4 Notes
5320 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5321 * and re-start the T1-init timer without changing state. This MUST
5322 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5323 * endpoint MUST abort the initialization process and report the
5324 * error to SCTP user.
5326 * Outputs
5327 * (timers, events)
5330 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5331 const struct sctp_association *asoc,
5332 const sctp_subtype_t type,
5333 void *arg,
5334 sctp_cmd_seq_t *commands)
5336 struct sctp_chunk *repl = NULL;
5337 struct sctp_bind_addr *bp;
5338 int attempts = asoc->init_err_counter + 1;
5340 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5341 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
5343 if (attempts <= asoc->max_init_attempts) {
5344 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5345 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5346 if (!repl)
5347 return SCTP_DISPOSITION_NOMEM;
5349 /* Choose transport for INIT. */
5350 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5351 SCTP_CHUNK(repl));
5353 /* Issue a sideeffect to do the needed accounting. */
5354 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5355 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5357 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5358 } else {
5359 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5360 " max_init_attempts: %d\n",
5361 attempts, asoc->max_init_attempts);
5362 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5363 SCTP_ERROR(ETIMEDOUT));
5364 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5365 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5366 return SCTP_DISPOSITION_DELETE_TCB;
5369 return SCTP_DISPOSITION_CONSUME;
5373 * sctp_sf_t1_cookie_timer_expire
5375 * Section: 4 Note: 2
5376 * Verification Tag:
5377 * Inputs
5378 * (endpoint, asoc)
5380 * RFC 2960 Section 4 Notes
5381 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5382 * COOKIE ECHO and re-start the T1-cookie timer without changing
5383 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5384 * After that, the endpoint MUST abort the initialization process and
5385 * report the error to SCTP user.
5387 * Outputs
5388 * (timers, events)
5391 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
5392 const struct sctp_association *asoc,
5393 const sctp_subtype_t type,
5394 void *arg,
5395 sctp_cmd_seq_t *commands)
5397 struct sctp_chunk *repl = NULL;
5398 int attempts = asoc->init_err_counter + 1;
5400 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5401 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
5403 if (attempts <= asoc->max_init_attempts) {
5404 repl = sctp_make_cookie_echo(asoc, NULL);
5405 if (!repl)
5406 return SCTP_DISPOSITION_NOMEM;
5408 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5409 SCTP_CHUNK(repl));
5410 /* Issue a sideeffect to do the needed accounting. */
5411 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5412 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5414 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5415 } else {
5416 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5417 SCTP_ERROR(ETIMEDOUT));
5418 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5419 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5420 return SCTP_DISPOSITION_DELETE_TCB;
5423 return SCTP_DISPOSITION_CONSUME;
5426 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5427 * with the updated last sequential TSN received from its peer.
5429 * An endpoint should limit the number of retransmissions of the
5430 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5431 * If this threshold is exceeded the endpoint should destroy the TCB and
5432 * MUST report the peer endpoint unreachable to the upper layer (and
5433 * thus the association enters the CLOSED state). The reception of any
5434 * packet from its peer (i.e. as the peer sends all of its queued DATA
5435 * chunks) should clear the endpoint's retransmission count and restart
5436 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5437 * all of its queued DATA chunks that have not yet been sent.
5439 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5440 const struct sctp_association *asoc,
5441 const sctp_subtype_t type,
5442 void *arg,
5443 sctp_cmd_seq_t *commands)
5445 struct sctp_chunk *reply = NULL;
5447 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5448 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5450 ((struct sctp_association *)asoc)->shutdown_retries++;
5452 if (asoc->overall_error_count >= asoc->max_retrans) {
5453 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5454 SCTP_ERROR(ETIMEDOUT));
5455 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5456 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5457 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5458 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5459 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5460 return SCTP_DISPOSITION_DELETE_TCB;
5463 switch (asoc->state) {
5464 case SCTP_STATE_SHUTDOWN_SENT:
5465 reply = sctp_make_shutdown(asoc, NULL);
5466 break;
5468 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5469 reply = sctp_make_shutdown_ack(asoc, NULL);
5470 break;
5472 default:
5473 BUG();
5474 break;
5477 if (!reply)
5478 goto nomem;
5480 /* Do some failure management (Section 8.2).
5481 * If we remove the transport an SHUTDOWN was last sent to, don't
5482 * do failure management.
5484 if (asoc->shutdown_last_sent_to)
5485 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5486 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5488 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5489 * the T2-shutdown timer.
5491 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5493 /* Restart the T2-shutdown timer. */
5494 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5495 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5496 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5497 return SCTP_DISPOSITION_CONSUME;
5499 nomem:
5500 return SCTP_DISPOSITION_NOMEM;
5504 * ADDIP Section 4.1 ASCONF CHunk Procedures
5505 * If the T4 RTO timer expires the endpoint should do B1 to B5
5507 sctp_disposition_t sctp_sf_t4_timer_expire(
5508 const struct sctp_endpoint *ep,
5509 const struct sctp_association *asoc,
5510 const sctp_subtype_t type,
5511 void *arg,
5512 sctp_cmd_seq_t *commands)
5514 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5515 struct sctp_transport *transport = chunk->transport;
5517 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5519 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5520 * detection on the appropriate destination address as defined in
5521 * RFC2960 [5] section 8.1 and 8.2.
5523 if (transport)
5524 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5525 SCTP_TRANSPORT(transport));
5527 /* Reconfig T4 timer and transport. */
5528 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5530 /* ADDIP 4.1 B2) Increment the association error counters and perform
5531 * endpoint failure detection on the association as defined in
5532 * RFC2960 [5] section 8.1 and 8.2.
5533 * association error counter is incremented in SCTP_CMD_STRIKE.
5535 if (asoc->overall_error_count >= asoc->max_retrans) {
5536 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5537 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5538 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5539 SCTP_ERROR(ETIMEDOUT));
5540 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5541 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5542 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5543 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5544 return SCTP_DISPOSITION_ABORT;
5547 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5548 * the ASCONF chunk was sent by doubling the RTO timer value.
5549 * This is done in SCTP_CMD_STRIKE.
5552 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5553 * choose an alternate destination address (please refer to RFC2960
5554 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5555 * chunk, it MUST be the same (including its serial number) as the last
5556 * ASCONF sent.
5558 sctp_chunk_hold(asoc->addip_last_asconf);
5559 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5560 SCTP_CHUNK(asoc->addip_last_asconf));
5562 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5563 * destination is selected, then the RTO used will be that of the new
5564 * destination address.
5566 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5567 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5569 return SCTP_DISPOSITION_CONSUME;
5572 /* sctpimpguide-05 Section 2.12.2
5573 * The sender of the SHUTDOWN MAY also start an overall guard timer
5574 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5575 * At the expiration of this timer the sender SHOULD abort the association
5576 * by sending an ABORT chunk.
5578 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5579 const struct sctp_association *asoc,
5580 const sctp_subtype_t type,
5581 void *arg,
5582 sctp_cmd_seq_t *commands)
5584 struct sctp_chunk *reply = NULL;
5586 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5587 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5589 reply = sctp_make_abort(asoc, NULL, 0);
5590 if (!reply)
5591 goto nomem;
5593 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5594 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5595 SCTP_ERROR(ETIMEDOUT));
5596 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5597 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5599 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5600 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5602 return SCTP_DISPOSITION_DELETE_TCB;
5603 nomem:
5604 return SCTP_DISPOSITION_NOMEM;
5607 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5608 * the association is automatically closed by starting the shutdown process.
5609 * The work that needs to be done is same as when SHUTDOWN is initiated by
5610 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5612 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5613 const struct sctp_endpoint *ep,
5614 const struct sctp_association *asoc,
5615 const sctp_subtype_t type,
5616 void *arg,
5617 sctp_cmd_seq_t *commands)
5619 int disposition;
5621 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5623 /* From 9.2 Shutdown of an Association
5624 * Upon receipt of the SHUTDOWN primitive from its upper
5625 * layer, the endpoint enters SHUTDOWN-PENDING state and
5626 * remains there until all outstanding data has been
5627 * acknowledged by its peer. The endpoint accepts no new data
5628 * from its upper layer, but retransmits data to the far end
5629 * if necessary to fill gaps.
5631 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5632 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5634 disposition = SCTP_DISPOSITION_CONSUME;
5635 if (sctp_outq_is_empty(&asoc->outqueue)) {
5636 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5637 arg, commands);
5639 return disposition;
5642 /*****************************************************************************
5643 * These are sa state functions which could apply to all types of events.
5644 ****************************************************************************/
5647 * This table entry is not implemented.
5649 * Inputs
5650 * (endpoint, asoc, chunk)
5652 * The return value is the disposition of the chunk.
5654 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5655 const struct sctp_association *asoc,
5656 const sctp_subtype_t type,
5657 void *arg,
5658 sctp_cmd_seq_t *commands)
5660 return SCTP_DISPOSITION_NOT_IMPL;
5664 * This table entry represents a bug.
5666 * Inputs
5667 * (endpoint, asoc, chunk)
5669 * The return value is the disposition of the chunk.
5671 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5672 const struct sctp_association *asoc,
5673 const sctp_subtype_t type,
5674 void *arg,
5675 sctp_cmd_seq_t *commands)
5677 return SCTP_DISPOSITION_BUG;
5681 * This table entry represents the firing of a timer in the wrong state.
5682 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5683 * when the association is in the wrong state. This event should
5684 * be ignored, so as to prevent any rearming of the timer.
5686 * Inputs
5687 * (endpoint, asoc, chunk)
5689 * The return value is the disposition of the chunk.
5691 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5692 const struct sctp_association *asoc,
5693 const sctp_subtype_t type,
5694 void *arg,
5695 sctp_cmd_seq_t *commands)
5697 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5698 return SCTP_DISPOSITION_CONSUME;
5701 /********************************************************************
5702 * 2nd Level Abstractions
5703 ********************************************************************/
5705 /* Pull the SACK chunk based on the SACK header. */
5706 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5708 struct sctp_sackhdr *sack;
5709 unsigned int len;
5710 __u16 num_blocks;
5711 __u16 num_dup_tsns;
5713 /* Protect ourselves from reading too far into
5714 * the skb from a bogus sender.
5716 sack = (struct sctp_sackhdr *) chunk->skb->data;
5718 num_blocks = ntohs(sack->num_gap_ack_blocks);
5719 num_dup_tsns = ntohs(sack->num_dup_tsns);
5720 len = sizeof(struct sctp_sackhdr);
5721 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5722 if (len > chunk->skb->len)
5723 return NULL;
5725 skb_pull(chunk->skb, len);
5727 return sack;
5730 /* Create an ABORT packet to be sent as a response, with the specified
5731 * error causes.
5733 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5734 const struct sctp_association *asoc,
5735 struct sctp_chunk *chunk,
5736 const void *payload,
5737 size_t paylen)
5739 struct sctp_packet *packet;
5740 struct sctp_chunk *abort;
5742 packet = sctp_ootb_pkt_new(asoc, chunk);
5744 if (packet) {
5745 /* Make an ABORT.
5746 * The T bit will be set if the asoc is NULL.
5748 abort = sctp_make_abort(asoc, chunk, paylen);
5749 if (!abort) {
5750 sctp_ootb_pkt_free(packet);
5751 return NULL;
5754 /* Reflect vtag if T-Bit is set */
5755 if (sctp_test_T_bit(abort))
5756 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5758 /* Add specified error causes, i.e., payload, to the
5759 * end of the chunk.
5761 sctp_addto_chunk(abort, paylen, payload);
5763 /* Set the skb to the belonging sock for accounting. */
5764 abort->skb->sk = ep->base.sk;
5766 sctp_packet_append_chunk(packet, abort);
5770 return packet;
5773 /* Allocate a packet for responding in the OOTB conditions. */
5774 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5775 const struct sctp_chunk *chunk)
5777 struct sctp_packet *packet;
5778 struct sctp_transport *transport;
5779 __u16 sport;
5780 __u16 dport;
5781 __u32 vtag;
5783 /* Get the source and destination port from the inbound packet. */
5784 sport = ntohs(chunk->sctp_hdr->dest);
5785 dport = ntohs(chunk->sctp_hdr->source);
5787 /* The V-tag is going to be the same as the inbound packet if no
5788 * association exists, otherwise, use the peer's vtag.
5790 if (asoc) {
5791 /* Special case the INIT-ACK as there is no peer's vtag
5792 * yet.
5794 switch(chunk->chunk_hdr->type) {
5795 case SCTP_CID_INIT_ACK:
5797 sctp_initack_chunk_t *initack;
5799 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5800 vtag = ntohl(initack->init_hdr.init_tag);
5801 break;
5803 default:
5804 vtag = asoc->peer.i.init_tag;
5805 break;
5807 } else {
5808 /* Special case the INIT and stale COOKIE_ECHO as there is no
5809 * vtag yet.
5811 switch(chunk->chunk_hdr->type) {
5812 case SCTP_CID_INIT:
5814 sctp_init_chunk_t *init;
5816 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5817 vtag = ntohl(init->init_hdr.init_tag);
5818 break;
5820 default:
5821 vtag = ntohl(chunk->sctp_hdr->vtag);
5822 break;
5826 /* Make a transport for the bucket, Eliza... */
5827 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5828 if (!transport)
5829 goto nomem;
5831 /* Cache a route for the transport with the chunk's destination as
5832 * the source address.
5834 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5835 sctp_sk(sctp_get_ctl_sock()));
5837 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5838 packet = sctp_packet_config(packet, vtag, 0);
5840 return packet;
5842 nomem:
5843 return NULL;
5846 /* Free the packet allocated earlier for responding in the OOTB condition. */
5847 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5849 sctp_transport_free(packet->transport);
5852 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5853 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5854 const struct sctp_association *asoc,
5855 const struct sctp_chunk *chunk,
5856 sctp_cmd_seq_t *commands,
5857 struct sctp_chunk *err_chunk)
5859 struct sctp_packet *packet;
5861 if (err_chunk) {
5862 packet = sctp_ootb_pkt_new(asoc, chunk);
5863 if (packet) {
5864 struct sctp_signed_cookie *cookie;
5866 /* Override the OOTB vtag from the cookie. */
5867 cookie = chunk->subh.cookie_hdr;
5868 packet->vtag = cookie->c.peer_vtag;
5870 /* Set the skb to the belonging sock for accounting. */
5871 err_chunk->skb->sk = ep->base.sk;
5872 sctp_packet_append_chunk(packet, err_chunk);
5873 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5874 SCTP_PACKET(packet));
5875 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5876 } else
5877 sctp_chunk_free (err_chunk);
5882 /* Process a data chunk */
5883 static int sctp_eat_data(const struct sctp_association *asoc,
5884 struct sctp_chunk *chunk,
5885 sctp_cmd_seq_t *commands)
5887 sctp_datahdr_t *data_hdr;
5888 struct sctp_chunk *err;
5889 size_t datalen;
5890 sctp_verb_t deliver;
5891 int tmp;
5892 __u32 tsn;
5893 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5894 struct sock *sk = asoc->base.sk;
5895 u16 ssn;
5896 u16 sid;
5897 u8 ordered = 0;
5899 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5900 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5902 tsn = ntohl(data_hdr->tsn);
5903 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5905 /* ASSERT: Now skb->data is really the user data. */
5907 /* Process ECN based congestion.
5909 * Since the chunk structure is reused for all chunks within
5910 * a packet, we use ecn_ce_done to track if we've already
5911 * done CE processing for this packet.
5913 * We need to do ECN processing even if we plan to discard the
5914 * chunk later.
5917 if (!chunk->ecn_ce_done) {
5918 struct sctp_af *af;
5919 chunk->ecn_ce_done = 1;
5921 af = sctp_get_af_specific(
5922 ipver2af(ip_hdr(chunk->skb)->version));
5924 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5925 /* Do real work as sideffect. */
5926 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5927 SCTP_U32(tsn));
5931 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5932 if (tmp < 0) {
5933 /* The TSN is too high--silently discard the chunk and
5934 * count on it getting retransmitted later.
5936 return SCTP_IERROR_HIGH_TSN;
5937 } else if (tmp > 0) {
5938 /* This is a duplicate. Record it. */
5939 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5940 return SCTP_IERROR_DUP_TSN;
5943 /* This is a new TSN. */
5945 /* Discard if there is no room in the receive window.
5946 * Actually, allow a little bit of overflow (up to a MTU).
5948 datalen = ntohs(chunk->chunk_hdr->length);
5949 datalen -= sizeof(sctp_data_chunk_t);
5951 deliver = SCTP_CMD_CHUNK_ULP;
5953 /* Think about partial delivery. */
5954 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5956 /* Even if we don't accept this chunk there is
5957 * memory pressure.
5959 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5962 /* Spill over rwnd a little bit. Note: While allowed, this spill over
5963 * seems a bit troublesome in that frag_point varies based on
5964 * PMTU. In cases, such as loopback, this might be a rather
5965 * large spill over.
5967 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5968 (datalen > asoc->rwnd + asoc->frag_point))) {
5970 /* If this is the next TSN, consider reneging to make
5971 * room. Note: Playing nice with a confused sender. A
5972 * malicious sender can still eat up all our buffer
5973 * space and in the future we may want to detect and
5974 * do more drastic reneging.
5976 if (sctp_tsnmap_has_gap(map) &&
5977 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5978 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5979 deliver = SCTP_CMD_RENEGE;
5980 } else {
5981 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5982 "rwnd: %d\n", tsn, datalen,
5983 asoc->rwnd);
5984 return SCTP_IERROR_IGNORE_TSN;
5989 * Also try to renege to limit our memory usage in the event that
5990 * we are under memory pressure
5991 * If we can't renege, don't worry about it, the sk_rmem_schedule
5992 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5993 * memory usage too much
5995 if (*sk->sk_prot_creator->memory_pressure) {
5996 if (sctp_tsnmap_has_gap(map) &&
5997 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5998 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5999 deliver = SCTP_CMD_RENEGE;
6004 * Section 3.3.10.9 No User Data (9)
6006 * Cause of error
6007 * ---------------
6008 * No User Data: This error cause is returned to the originator of a
6009 * DATA chunk if a received DATA chunk has no user data.
6011 if (unlikely(0 == datalen)) {
6012 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6013 if (err) {
6014 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6015 SCTP_CHUNK(err));
6017 /* We are going to ABORT, so we might as well stop
6018 * processing the rest of the chunks in the packet.
6020 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6021 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6022 SCTP_ERROR(ECONNABORTED));
6023 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6024 SCTP_PERR(SCTP_ERROR_NO_DATA));
6025 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6026 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6027 return SCTP_IERROR_NO_DATA;
6030 chunk->data_accepted = 1;
6032 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6033 * if we renege and the chunk arrives again.
6035 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6036 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6037 else {
6038 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6039 ordered = 1;
6042 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6044 * If an endpoint receive a DATA chunk with an invalid stream
6045 * identifier, it shall acknowledge the reception of the DATA chunk
6046 * following the normal procedure, immediately send an ERROR chunk
6047 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6048 * and discard the DATA chunk.
6050 sid = ntohs(data_hdr->stream);
6051 if (sid >= asoc->c.sinit_max_instreams) {
6052 /* Mark tsn as received even though we drop it */
6053 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6055 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6056 &data_hdr->stream,
6057 sizeof(data_hdr->stream));
6058 if (err)
6059 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6060 SCTP_CHUNK(err));
6061 return SCTP_IERROR_BAD_STREAM;
6064 /* Check to see if the SSN is possible for this TSN.
6065 * The biggest gap we can record is 4K wide. Since SSNs wrap
6066 * at an unsigned short, there is no way that an SSN can
6067 * wrap and for a valid TSN. We can simply check if the current
6068 * SSN is smaller then the next expected one. If it is, it wrapped
6069 * and is invalid.
6071 ssn = ntohs(data_hdr->ssn);
6072 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6073 return SCTP_IERROR_PROTO_VIOLATION;
6076 /* Send the data up to the user. Note: Schedule the
6077 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6078 * chunk needs the updated rwnd.
6080 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6082 return SCTP_IERROR_NO_ERROR;