]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sctp/sm_statefuns.c
sctp: Fix SCTP_MAXSEG socket option to comply to spec.
[mirror_ubuntu-bionic-kernel.git] / net / sctp / sm_statefuns.c
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
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.
7 *
60c778b2 8 * This is part of the SCTP Linux Kernel Implementation.
1da177e4
LT
9 *
10 * These are the state functions for the state machine.
11 *
60c778b2 12 * This SCTP implementation is free software;
1da177e4
LT
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.
17 *
60c778b2 18 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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.
23 *
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.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
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>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
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>
65
66static 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);
71static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 sctp_cmd_seq_t *commands);
74static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75 const struct sctp_chunk *chunk);
76static 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);
81static 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);
86static 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);
ece25dfa
VY
91static 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);
1da177e4
LT
96static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
97
52c1da39 98static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
f94c0198 99 __be16 error, int sk_err,
52c1da39
AB
100 const struct sctp_association *asoc,
101 struct sctp_transport *transport);
102
aecedeab 103static sctp_disposition_t sctp_sf_abort_violation(
ece25dfa 104 const struct sctp_endpoint *ep,
aecedeab
WY
105 const struct sctp_association *asoc,
106 void *arg,
107 sctp_cmd_seq_t *commands,
108 const __u8 *payload,
109 const size_t paylen);
110
52c1da39
AB
111static 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);
1da177e4 117
6f4c618d
WY
118static 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,
ba016670 122 void *arg, void *ext,
6f4c618d
WY
123 sctp_cmd_seq_t *commands);
124
aecedeab
WY
125static 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);
131
ece25dfa
VY
132static 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);
138
bbd0d598
VY
139static 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);
143
75205f47
VY
144static 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);
149
1da177e4
LT
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
155 *
156 */
157static inline int
158sctp_chunk_length_valid(struct sctp_chunk *chunk,
159 __u16 required_length)
160{
161 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
162
163 if (unlikely(chunk_length < required_length))
164 return 0;
165
166 return 1;
167}
168
169/**********************************************************
170 * These are the state functions for handling chunk events.
171 **********************************************************/
172
173/*
174 * Process the final SHUTDOWN COMPLETE.
175 *
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).
182 *
047a2428 183 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
1da177e4
LT
184 * C) Rules for packet carrying SHUTDOWN COMPLETE:
185 * ...
047a2428
JF
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.
1da177e4
LT
195 *
196 * Inputs
197 * (endpoint, asoc, chunk)
198 *
199 * Outputs
200 * (asoc, reply_msg, msg_up, timers, counters)
201 *
202 * The return value is the disposition of the chunk.
203 */
204sctp_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)
209{
210 struct sctp_chunk *chunk = arg;
211 struct sctp_ulpevent *ev;
212
ece25dfa
VY
213 if (!sctp_vtag_verify_either(chunk, asoc))
214 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
215
1da177e4
LT
216 /* RFC 2960 6.10 Bundling
217 *
218 * An endpoint MUST NOT bundle INIT, INIT ACK or
219 * SHUTDOWN COMPLETE with any other chunks.
220 */
221 if (!chunk->singleton)
ece25dfa 222 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
1da177e4 223
ece25dfa
VY
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);
1da177e4
LT
228
229 /* RFC 2960 10.2 SCTP-to-ULP
230 *
231 * H) SHUTDOWN COMPLETE notification
232 *
233 * When SCTP completes the shutdown procedures (section 9.2) this
234 * notification is passed to the upper layer.
235 */
236 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
a5a35e76 237 0, 0, 0, NULL, GFP_ATOMIC);
df7deeb5
VY
238 if (ev)
239 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
d808ad9a 240 SCTP_ULPEVENT(ev));
1da177e4
LT
241
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).
249 */
250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
252
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
255
256 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257 SCTP_STATE(SCTP_STATE_CLOSED));
258
259 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
261
262 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
263
264 return SCTP_DISPOSITION_DELETE_TCB;
1da177e4
LT
265}
266
267/*
268 * Respond to a normal INIT chunk.
269 * We are the side that is being asked for an association.
270 *
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.
278 *
d808ad9a 279 * Verification Tag: Must be 0.
1da177e4
LT
280 *
281 * Inputs
282 * (endpoint, asoc, chunk)
283 *
284 * Outputs
285 * (asoc, reply_msg, msg_up, timers, counters)
286 *
287 * The return value is the disposition of the chunk.
288 */
289sctp_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)
294{
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;
1da177e4
LT
301 int len;
302
303 /* 6.10 Bundling
304 * An endpoint MUST NOT bundle INIT, INIT ACK or
305 * SHUTDOWN COMPLETE with any other chunks.
d808ad9a 306 *
1da177e4
LT
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.
311 */
312 if (!chunk->singleton)
313 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
314
315 /* If the packet is an OOTB packet which is temporarily on the
316 * control endpoint, respond with an ABORT.
317 */
8190f89d
WY
318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
1da177e4 320 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
8190f89d 321 }
1da177e4 322
1da177e4 323 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
d808ad9a 324 * Tag.
1da177e4
LT
325 */
326 if (chunk->sctp_hdr->vtag != 0)
327 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
328
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.
333 */
334 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
335 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
336
bec9640b
VY
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
342 */
343 if (sctp_sstate(ep->base.sk, CLOSING))
344 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
345
1da177e4
LT
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.
353 */
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));
360
361 sctp_chunk_free(err_chunk);
362
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;
370 }
371 } else {
372 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
373 commands);
374 }
375 }
376
d808ad9a 377 /* Grab the INIT header. */
1da177e4
LT
378 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
379
380 /* Tag the variable length parameters. */
381 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
382
383 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
384 if (!new_asoc)
385 goto nomem;
386
387 /* The call, sctp_process_init(), can fail on memory allocation. */
388 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
389 sctp_source(chunk),
390 (sctp_init_chunk_t *)chunk->chunk_hdr,
391 GFP_ATOMIC))
392 goto nomem_init;
393
1da177e4
LT
394 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
395
396 /* If there are errors need to be reported for unknown parameters,
397 * make sure to reserve enough room in the INIT ACK for them.
398 */
399 len = 0;
400 if (err_chunk)
401 len = ntohs(err_chunk->chunk_hdr->length) -
402 sizeof(sctp_chunkhdr_t);
403
404 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
df7deeb5 405 goto nomem_init;
1da177e4
LT
406
407 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
408 if (!repl)
df7deeb5 409 goto nomem_init;
1da177e4
LT
410
411 /* If there are errors need to be reported for unknown parameters,
412 * include them in the outgoing INIT ACK as "Unrecognized parameter"
413 * parameter.
414 */
415 if (err_chunk) {
416 /* Get the "Unrecognized parameter" parameter(s) out of the
417 * ERROR chunk generated by sctp_verify_init(). Since the
418 * error cause code for "unknown parameter" and the
419 * "Unrecognized parameter" type is the same, we can
420 * construct the parameters in INIT ACK by copying the
421 * ERROR causes over.
422 */
423 unk_param = (sctp_unrecognized_param_t *)
424 ((__u8 *)(err_chunk->chunk_hdr) +
425 sizeof(sctp_chunkhdr_t));
426 /* Replace the cause code with the "Unrecognized parameter"
427 * parameter type.
428 */
429 sctp_addto_chunk(repl, len, unk_param);
430 sctp_chunk_free(err_chunk);
431 }
432
df7deeb5
VY
433 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
434
1da177e4
LT
435 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
436
437 /*
438 * Note: After sending out INIT ACK with the State Cookie parameter,
439 * "Z" MUST NOT allocate any resources, nor keep any states for the
440 * new association. Otherwise, "Z" will be vulnerable to resource
441 * attacks.
442 */
443 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
444
445 return SCTP_DISPOSITION_DELETE_TCB;
446
1da177e4
LT
447nomem_init:
448 sctp_association_free(new_asoc);
449nomem:
df7deeb5
VY
450 if (err_chunk)
451 sctp_chunk_free(err_chunk);
1da177e4
LT
452 return SCTP_DISPOSITION_NOMEM;
453}
454
455/*
456 * Respond to a normal INIT ACK chunk.
457 * We are the side that is initiating the association.
458 *
459 * Section: 5.1 Normal Establishment of an Association, C
460 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
461 * timer and leave COOKIE-WAIT state. "A" shall then send the State
462 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
463 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
464 *
465 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
466 * DATA chunks, but it MUST be the first chunk in the packet and
467 * until the COOKIE ACK is returned the sender MUST NOT send any
468 * other packets to the peer.
469 *
470 * Verification Tag: 3.3.3
471 * If the value of the Initiate Tag in a received INIT ACK chunk is
472 * found to be 0, the receiver MUST treat it as an error and close the
473 * association by transmitting an ABORT.
474 *
475 * Inputs
476 * (endpoint, asoc, chunk)
477 *
478 * Outputs
479 * (asoc, reply_msg, msg_up, timers, counters)
480 *
481 * The return value is the disposition of the chunk.
482 */
483sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
484 const struct sctp_association *asoc,
485 const sctp_subtype_t type,
486 void *arg,
487 sctp_cmd_seq_t *commands)
488{
489 struct sctp_chunk *chunk = arg;
490 sctp_init_chunk_t *initchunk;
1da177e4
LT
491 struct sctp_chunk *err_chunk;
492 struct sctp_packet *packet;
1da177e4
LT
493
494 if (!sctp_vtag_verify(chunk, asoc))
495 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
496
1da177e4
LT
497 /* 6.10 Bundling
498 * An endpoint MUST NOT bundle INIT, INIT ACK or
499 * SHUTDOWN COMPLETE with any other chunks.
500 */
501 if (!chunk->singleton)
ece25dfa 502 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
1da177e4 503
ece25dfa
VY
504 /* Make sure that the INIT-ACK chunk has a valid length */
505 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
506 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
507 commands);
1da177e4
LT
508 /* Grab the INIT header. */
509 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
510
1da177e4
LT
511 /* Verify the INIT chunk before processing it. */
512 err_chunk = NULL;
513 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
514 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
515 &err_chunk)) {
516
853f4b50
VY
517 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
518
1da177e4 519 /* This chunk contains fatal error. It is to be discarded.
d6701191
VY
520 * Send an ABORT, with causes. If there are no causes,
521 * then there wasn't enough memory. Just terminate
522 * the association.
1da177e4
LT
523 */
524 if (err_chunk) {
525 packet = sctp_abort_pkt_new(ep, asoc, arg,
526 (__u8 *)(err_chunk->chunk_hdr) +
527 sizeof(sctp_chunkhdr_t),
528 ntohs(err_chunk->chunk_hdr->length) -
529 sizeof(sctp_chunkhdr_t));
530
531 sctp_chunk_free(err_chunk);
532
533 if (packet) {
534 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
535 SCTP_PACKET(packet));
536 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
8de8c873 537 error = SCTP_ERROR_INV_PARAM;
1da177e4 538 }
1da177e4 539 }
bbd0d598
VY
540
541 /* SCTP-AUTH, Section 6.3:
542 * It should be noted that if the receiver wants to tear
543 * down an association in an authenticated way only, the
544 * handling of malformed packets should not result in
545 * tearing down the association.
546 *
547 * This means that if we only want to abort associations
548 * in an authenticated way (i.e AUTH+ABORT), then we
ac461a03 549 * can't destroy this association just becuase the packet
bbd0d598
VY
550 * was malformed.
551 */
552 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
553 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
554
555 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
8de8c873
SS
556 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
557 asoc, chunk->transport);
1da177e4
LT
558 }
559
560 /* Tag the variable length parameters. Note that we never
561 * convert the parameters in an INIT chunk.
562 */
563 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
564
565 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
566
567 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
568 SCTP_PEER_INIT(initchunk));
569
3f7a87d2
FF
570 /* Reset init error count upon receipt of INIT-ACK. */
571 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
572
1da177e4
LT
573 /* 5.1 C) "A" shall stop the T1-init timer and leave
574 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
575 * timer, and enter the COOKIE-ECHOED state.
576 */
577 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
578 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
579 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
580 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
581 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
582 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
583
730fc3d0
VY
584 /* SCTP-AUTH: genereate the assocition shared keys so that
585 * we can potentially signe the COOKIE-ECHO.
586 */
587 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
588
1da177e4
LT
589 /* 5.1 C) "A" shall then send the State Cookie received in the
590 * INIT ACK chunk in a COOKIE ECHO chunk, ...
591 */
592 /* If there is any errors to report, send the ERROR chunk generated
593 * for unknown parameters as well.
594 */
595 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
596 SCTP_CHUNK(err_chunk));
597
598 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
599}
600
601/*
602 * Respond to a normal COOKIE ECHO chunk.
603 * We are the side that is being asked for an association.
604 *
605 * Section: 5.1 Normal Establishment of an Association, D
606 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
607 * with a COOKIE ACK chunk after building a TCB and moving to
608 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
609 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
610 * chunk MUST be the first chunk in the packet.
611 *
612 * IMPLEMENTATION NOTE: An implementation may choose to send the
613 * Communication Up notification to the SCTP user upon reception
614 * of a valid COOKIE ECHO chunk.
615 *
616 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
617 * D) Rules for packet carrying a COOKIE ECHO
618 *
619 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
620 * Initial Tag received in the INIT ACK.
621 *
622 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
623 *
624 * Inputs
625 * (endpoint, asoc, chunk)
626 *
627 * Outputs
628 * (asoc, reply_msg, msg_up, timers, counters)
629 *
630 * The return value is the disposition of the chunk.
631 */
632sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
633 const struct sctp_association *asoc,
634 const sctp_subtype_t type, void *arg,
635 sctp_cmd_seq_t *commands)
636{
637 struct sctp_chunk *chunk = arg;
638 struct sctp_association *new_asoc;
639 sctp_init_chunk_t *peer_init;
640 struct sctp_chunk *repl;
df7deeb5 641 struct sctp_ulpevent *ev, *ai_ev = NULL;
1da177e4
LT
642 int error = 0;
643 struct sctp_chunk *err_chk_p;
609ee467 644 struct sock *sk;
1da177e4
LT
645
646 /* If the packet is an OOTB packet which is temporarily on the
647 * control endpoint, respond with an ABORT.
648 */
8190f89d
WY
649 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
650 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
ece25dfa 651 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
8190f89d 652 }
1da177e4
LT
653
654 /* Make sure that the COOKIE_ECHO chunk has a valid length.
655 * In this case, we check that we have enough for at least a
656 * chunk header. More detailed verification is done
657 * in sctp_unpack_cookie().
658 */
659 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
660 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
661
609ee467
VY
662 /* If the endpoint is not listening or if the number of associations
663 * on the TCP-style socket exceed the max backlog, respond with an
664 * ABORT.
665 */
666 sk = ep->base.sk;
667 if (!sctp_sstate(sk, LISTENING) ||
668 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
669 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
670
1da177e4
LT
671 /* "Decode" the chunk. We have no optional parameters so we
672 * are in good shape.
673 */
d808ad9a 674 chunk->subh.cookie_hdr =
1da177e4 675 (struct sctp_signed_cookie *)chunk->skb->data;
62b08083
SS
676 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
677 sizeof(sctp_chunkhdr_t)))
678 goto nomem;
1da177e4
LT
679
680 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
681 * "Z" will reply with a COOKIE ACK chunk after building a TCB
682 * and moving to the ESTABLISHED state.
683 */
684 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
685 &err_chk_p);
686
687 /* FIXME:
688 * If the re-build failed, what is the proper error path
689 * from here?
690 *
691 * [We should abort the association. --piggy]
692 */
693 if (!new_asoc) {
694 /* FIXME: Several errors are possible. A bad cookie should
695 * be silently discarded, but think about logging it too.
696 */
697 switch (error) {
698 case -SCTP_IERROR_NOMEM:
699 goto nomem;
700
701 case -SCTP_IERROR_STALE_COOKIE:
702 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
703 err_chk_p);
704 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
705
706 case -SCTP_IERROR_BAD_SIG:
707 default:
708 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3ff50b79 709 }
1da177e4
LT
710 }
711
1da177e4 712
df7deeb5
VY
713 /* Delay state machine commands until later.
714 *
715 * Re-build the bind address for the association is done in
1da177e4
LT
716 * the sctp_unpack_cookie() already.
717 */
718 /* This is a brand-new association, so these are not yet side
719 * effects--it is safe to run them here.
720 */
721 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
722
723 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
6a1e5f33 724 &chunk->subh.cookie_hdr->c.peer_addr,
1da177e4
LT
725 peer_init, GFP_ATOMIC))
726 goto nomem_init;
727
730fc3d0
VY
728 /* SCTP-AUTH: Now that we've populate required fields in
729 * sctp_process_init, set up the assocaition shared keys as
730 * necessary so that we can potentially authenticate the ACK
731 */
732 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
733 if (error)
734 goto nomem_init;
735
bbd0d598
VY
736 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
737 * is supposed to be authenticated and we have to do delayed
738 * authentication. We've just recreated the association using
739 * the information in the cookie and now it's much easier to
740 * do the authentication.
741 */
742 if (chunk->auth_chunk) {
743 struct sctp_chunk auth;
744 sctp_ierror_t ret;
745
746 /* set-up our fake chunk so that we can process it */
747 auth.skb = chunk->auth_chunk;
748 auth.asoc = chunk->asoc;
749 auth.sctp_hdr = chunk->sctp_hdr;
750 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
751 sizeof(sctp_chunkhdr_t));
752 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
753 auth.transport = chunk->transport;
754
755 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
756
757 /* We can now safely free the auth_chunk clone */
758 kfree_skb(chunk->auth_chunk);
759
760 if (ret != SCTP_IERROR_NO_ERROR) {
761 sctp_association_free(new_asoc);
762 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
763 }
764 }
765
1da177e4
LT
766 repl = sctp_make_cookie_ack(new_asoc, chunk);
767 if (!repl)
df7deeb5 768 goto nomem_init;
1da177e4
LT
769
770 /* RFC 2960 5.1 Normal Establishment of an Association
771 *
772 * D) IMPLEMENTATION NOTE: An implementation may choose to
773 * send the Communication Up notification to the SCTP user
774 * upon reception of a valid COOKIE ECHO chunk.
775 */
776 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
777 new_asoc->c.sinit_num_ostreams,
778 new_asoc->c.sinit_max_instreams,
a5a35e76 779 NULL, GFP_ATOMIC);
1da177e4
LT
780 if (!ev)
781 goto nomem_ev;
782
d808ad9a 783 /* Sockets API Draft Section 5.3.1.6
0f3fffd8 784 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1da177e4 785 * delivers this notification to inform the application that of the
0f3fffd8 786 * peers requested adaptation layer.
1da177e4 787 */
0f3fffd8
ISJ
788 if (new_asoc->peer.adaptation_ind) {
789 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
1da177e4 790 GFP_ATOMIC);
df7deeb5
VY
791 if (!ai_ev)
792 goto nomem_aiev;
793 }
794
795 /* Add all the state machine commands now since we've created
796 * everything. This way we don't introduce memory corruptions
797 * during side-effect processing and correclty count established
798 * associations.
799 */
800 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
801 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
802 SCTP_STATE(SCTP_STATE_ESTABLISHED));
803 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
804 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
805 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
806
807 if (new_asoc->autoclose)
808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
809 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
810
df7deeb5
VY
811 /* This will send the COOKIE ACK */
812 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
813
814 /* Queue the ASSOC_CHANGE event */
815 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
816
817 /* Send up the Adaptation Layer Indication event */
818 if (ai_ev)
1da177e4 819 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
df7deeb5 820 SCTP_ULPEVENT(ai_ev));
1da177e4
LT
821
822 return SCTP_DISPOSITION_CONSUME;
823
df7deeb5
VY
824nomem_aiev:
825 sctp_ulpevent_free(ev);
1da177e4
LT
826nomem_ev:
827 sctp_chunk_free(repl);
1da177e4
LT
828nomem_init:
829 sctp_association_free(new_asoc);
830nomem:
831 return SCTP_DISPOSITION_NOMEM;
832}
833
834/*
835 * Respond to a normal COOKIE ACK chunk.
836 * We are the side that is being asked for an association.
837 *
838 * RFC 2960 5.1 Normal Establishment of an Association
839 *
840 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
841 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
842 * timer. It may also notify its ULP about the successful
843 * establishment of the association with a Communication Up
844 * notification (see Section 10).
845 *
846 * Verification Tag:
847 * Inputs
848 * (endpoint, asoc, chunk)
849 *
850 * Outputs
851 * (asoc, reply_msg, msg_up, timers, counters)
852 *
853 * The return value is the disposition of the chunk.
854 */
855sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
856 const struct sctp_association *asoc,
857 const sctp_subtype_t type, void *arg,
858 sctp_cmd_seq_t *commands)
859{
860 struct sctp_chunk *chunk = arg;
861 struct sctp_ulpevent *ev;
862
863 if (!sctp_vtag_verify(chunk, asoc))
864 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
865
866 /* Verify that the chunk length for the COOKIE-ACK is OK.
867 * If we don't do this, any bundled chunks may be junked.
868 */
869 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
870 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
871 commands);
872
873 /* Reset init error count upon receipt of COOKIE-ACK,
874 * to avoid problems with the managemement of this
875 * counter in stale cookie situations when a transition back
876 * from the COOKIE-ECHOED state to the COOKIE-WAIT
877 * state is performed.
878 */
3f7a87d2 879 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
1da177e4
LT
880
881 /* RFC 2960 5.1 Normal Establishment of an Association
882 *
883 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
884 * from the COOKIE-ECHOED state to the ESTABLISHED state,
885 * stopping the T1-cookie timer.
886 */
887 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
888 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
889 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
890 SCTP_STATE(SCTP_STATE_ESTABLISHED));
891 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
892 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
893 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
894 if (asoc->autoclose)
895 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
896 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
1da177e4
LT
897
898 /* It may also notify its ULP about the successful
899 * establishment of the association with a Communication Up
900 * notification (see Section 10).
901 */
902 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
903 0, asoc->c.sinit_num_ostreams,
904 asoc->c.sinit_max_instreams,
a5a35e76 905 NULL, GFP_ATOMIC);
1da177e4
LT
906
907 if (!ev)
908 goto nomem;
909
910 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
911
912 /* Sockets API Draft Section 5.3.1.6
0f3fffd8 913 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1da177e4 914 * delivers this notification to inform the application that of the
0f3fffd8 915 * peers requested adaptation layer.
1da177e4 916 */
0f3fffd8
ISJ
917 if (asoc->peer.adaptation_ind) {
918 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
1da177e4
LT
919 if (!ev)
920 goto nomem;
921
922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
923 SCTP_ULPEVENT(ev));
924 }
925
926 return SCTP_DISPOSITION_CONSUME;
927nomem:
928 return SCTP_DISPOSITION_NOMEM;
929}
930
931/* Generate and sendout a heartbeat packet. */
932static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
933 const struct sctp_association *asoc,
934 const sctp_subtype_t type,
935 void *arg,
936 sctp_cmd_seq_t *commands)
937{
938 struct sctp_transport *transport = (struct sctp_transport *) arg;
939 struct sctp_chunk *reply;
940 sctp_sender_hb_info_t hbinfo;
941 size_t paylen = 0;
942
943 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
944 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
63de08f4 945 hbinfo.daddr = transport->ipaddr;
1da177e4 946 hbinfo.sent_at = jiffies;
ad8fec17 947 hbinfo.hb_nonce = transport->hb_nonce;
1da177e4
LT
948
949 /* Send a heartbeat to our peer. */
950 paylen = sizeof(sctp_sender_hb_info_t);
951 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
952 if (!reply)
953 return SCTP_DISPOSITION_NOMEM;
954
955 /* Set rto_pending indicating that an RTT measurement
956 * is started with this heartbeat chunk.
957 */
958 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
959 SCTP_TRANSPORT(transport));
960
961 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
962 return SCTP_DISPOSITION_CONSUME;
963}
964
965/* Generate a HEARTBEAT packet on the given transport. */
966sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
969 void *arg,
970 sctp_cmd_seq_t *commands)
971{
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
973
b9f84786 974 if (asoc->overall_error_count >= asoc->max_retrans) {
8de8c873
SS
975 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
976 SCTP_ERROR(ETIMEDOUT));
1da177e4
LT
977 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
978 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 979 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4
LT
980 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
981 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
982 return SCTP_DISPOSITION_DELETE_TCB;
983 }
984
985 /* Section 3.3.5.
986 * The Sender-specific Heartbeat Info field should normally include
987 * information about the sender's current time when this HEARTBEAT
988 * chunk is sent and the destination transport address to which this
989 * HEARTBEAT is sent (see Section 8.3).
990 */
991
52ccb8e9 992 if (transport->param_flags & SPP_HB_ENABLE) {
1da177e4
LT
993 if (SCTP_DISPOSITION_NOMEM ==
994 sctp_sf_heartbeat(ep, asoc, type, arg,
995 commands))
996 return SCTP_DISPOSITION_NOMEM;
997 /* Set transport error counter and association error counter
998 * when sending heartbeat.
999 */
7e99013a
VY
1000 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1001 SCTP_TRANSPORT(transport));
1002 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1da177e4
LT
1003 SCTP_TRANSPORT(transport));
1004 }
1005 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1006 SCTP_TRANSPORT(transport));
1007
d808ad9a 1008 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
1009}
1010
1011/*
1012 * Process an heartbeat request.
1013 *
1014 * Section: 8.3 Path Heartbeat
1015 * The receiver of the HEARTBEAT should immediately respond with a
1016 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1017 * from the received HEARTBEAT chunk.
1018 *
1019 * Verification Tag: 8.5 Verification Tag [Normal verification]
1020 * When receiving an SCTP packet, the endpoint MUST ensure that the
1021 * value in the Verification Tag field of the received SCTP packet
1022 * matches its own Tag. If the received Verification Tag value does not
1023 * match the receiver's own tag value, the receiver shall silently
1024 * discard the packet and shall not process it any further except for
1025 * those cases listed in Section 8.5.1 below.
1026 *
1027 * Inputs
1028 * (endpoint, asoc, chunk)
1029 *
1030 * Outputs
1031 * (asoc, reply_msg, msg_up, timers, counters)
1032 *
1033 * The return value is the disposition of the chunk.
1034 */
1035sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1036 const struct sctp_association *asoc,
1037 const sctp_subtype_t type,
1038 void *arg,
1039 sctp_cmd_seq_t *commands)
1040{
1041 struct sctp_chunk *chunk = arg;
1042 struct sctp_chunk *reply;
1043 size_t paylen = 0;
1044
1045 if (!sctp_vtag_verify(chunk, asoc))
1046 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1047
1048 /* Make sure that the HEARTBEAT chunk has a valid length. */
1049 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1050 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1051 commands);
1052
1053 /* 8.3 The receiver of the HEARTBEAT should immediately
1054 * respond with a HEARTBEAT ACK that contains the Heartbeat
1055 * Information field copied from the received HEARTBEAT chunk.
1056 */
1057 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1058 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
62b08083
SS
1059 if (!pskb_pull(chunk->skb, paylen))
1060 goto nomem;
1da177e4
LT
1061
1062 reply = sctp_make_heartbeat_ack(asoc, chunk,
1063 chunk->subh.hb_hdr, paylen);
1064 if (!reply)
1065 goto nomem;
1066
1067 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1068 return SCTP_DISPOSITION_CONSUME;
1069
1070nomem:
1071 return SCTP_DISPOSITION_NOMEM;
1072}
1073
1074/*
1075 * Process the returning HEARTBEAT ACK.
1076 *
1077 * Section: 8.3 Path Heartbeat
1078 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1079 * should clear the error counter of the destination transport
1080 * address to which the HEARTBEAT was sent, and mark the destination
1081 * transport address as active if it is not so marked. The endpoint may
1082 * optionally report to the upper layer when an inactive destination
1083 * address is marked as active due to the reception of the latest
1084 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1085 * clear the association overall error count as well (as defined
1086 * in section 8.1).
1087 *
1088 * The receiver of the HEARTBEAT ACK should also perform an RTT
1089 * measurement for that destination transport address using the time
1090 * value carried in the HEARTBEAT ACK chunk.
1091 *
1092 * Verification Tag: 8.5 Verification Tag [Normal verification]
1093 *
1094 * Inputs
1095 * (endpoint, asoc, chunk)
1096 *
1097 * Outputs
1098 * (asoc, reply_msg, msg_up, timers, counters)
1099 *
1100 * The return value is the disposition of the chunk.
1101 */
1102sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1103 const struct sctp_association *asoc,
1104 const sctp_subtype_t type,
1105 void *arg,
1106 sctp_cmd_seq_t *commands)
1107{
1108 struct sctp_chunk *chunk = arg;
1109 union sctp_addr from_addr;
1110 struct sctp_transport *link;
1111 sctp_sender_hb_info_t *hbinfo;
1112 unsigned long max_interval;
1113
1114 if (!sctp_vtag_verify(chunk, asoc))
1115 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1116
1117 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
dadb50cc
WY
1118 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1119 sizeof(sctp_sender_hb_info_t)))
1da177e4
LT
1120 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1121 commands);
1122
1123 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
a601266e
VY
1124 /* Make sure that the length of the parameter is what we expect */
1125 if (ntohs(hbinfo->param_hdr.length) !=
1126 sizeof(sctp_sender_hb_info_t)) {
1127 return SCTP_DISPOSITION_DISCARD;
1128 }
1129
1da177e4 1130 from_addr = hbinfo->daddr;
63de08f4 1131 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1da177e4
LT
1132
1133 /* This should never happen, but lets log it if so. */
3f7a87d2
FF
1134 if (unlikely(!link)) {
1135 if (from_addr.sa.sa_family == AF_INET6) {
d99fa429
WY
1136 if (net_ratelimit())
1137 printk(KERN_WARNING
5b095d98 1138 "%s association %p could not find address %pI6\n",
0dc47877 1139 __func__,
d99fa429 1140 asoc,
fdb46ee7 1141 &from_addr.v6.sin6_addr);
3f7a87d2 1142 } else {
d99fa429
WY
1143 if (net_ratelimit())
1144 printk(KERN_WARNING
21454aaa 1145 "%s association %p could not find address %pI4\n",
0dc47877 1146 __func__,
d99fa429 1147 asoc,
21454aaa 1148 &from_addr.v4.sin_addr.s_addr);
3f7a87d2 1149 }
1da177e4
LT
1150 return SCTP_DISPOSITION_DISCARD;
1151 }
1152
ad8fec17
SS
1153 /* Validate the 64-bit random nonce. */
1154 if (hbinfo->hb_nonce != link->hb_nonce)
1155 return SCTP_DISPOSITION_DISCARD;
1156
52ccb8e9 1157 max_interval = link->hbinterval + link->rto;
1da177e4
LT
1158
1159 /* Check if the timestamp looks valid. */
1160 if (time_after(hbinfo->sent_at, jiffies) ||
1161 time_after(jiffies, hbinfo->sent_at + max_interval)) {
9ee46f1d 1162 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1da177e4 1163 "received for transport: %p\n",
0dc47877 1164 __func__, link);
1da177e4
LT
1165 return SCTP_DISPOSITION_DISCARD;
1166 }
1167
1168 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1169 * the HEARTBEAT should clear the error counter of the
1170 * destination transport address to which the HEARTBEAT was
1171 * sent and mark the destination transport address as active if
1172 * it is not so marked.
1173 */
1174 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1175
1176 return SCTP_DISPOSITION_CONSUME;
1177}
1178
1179/* Helper function to send out an abort for the restart
1180 * condition.
1181 */
1182static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1183 struct sctp_chunk *init,
1184 sctp_cmd_seq_t *commands)
1185{
1186 int len;
1187 struct sctp_packet *pkt;
1188 union sctp_addr_param *addrparm;
1189 struct sctp_errhdr *errhdr;
1190 struct sctp_endpoint *ep;
1191 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1192 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1193
1194 /* Build the error on the stack. We are way to malloc crazy
1195 * throughout the code today.
1196 */
1197 errhdr = (struct sctp_errhdr *)buffer;
1198 addrparm = (union sctp_addr_param *)errhdr->variable;
1199
1200 /* Copy into a parm format. */
1201 len = af->to_addr_param(ssa, addrparm);
1202 len += sizeof(sctp_errhdr_t);
1203
1204 errhdr->cause = SCTP_ERROR_RESTART;
1205 errhdr->length = htons(len);
1206
1207 /* Assign to the control socket. */
1208 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1209
1210 /* Association is NULL since this may be a restart attack and we
1211 * want to send back the attacker's vtag.
1212 */
1213 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1214
1215 if (!pkt)
1216 goto out;
1217 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1218
1219 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1220
1221 /* Discard the rest of the inbound packet. */
1222 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1223
1224out:
1225 /* Even if there is no memory, treat as a failure so
1226 * the packet will get dropped.
1227 */
1228 return 0;
1229}
1230
1231/* A restart is occurring, check to make sure no new addresses
1232 * are being added as we may be under a takeover attack.
1233 */
1234static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1235 const struct sctp_association *asoc,
1236 struct sctp_chunk *init,
1237 sctp_cmd_seq_t *commands)
1238{
1239 struct sctp_transport *new_addr, *addr;
1da177e4
LT
1240 int found;
1241
1242 /* Implementor's Guide - Sectin 5.2.2
1243 * ...
1244 * Before responding the endpoint MUST check to see if the
1245 * unexpected INIT adds new addresses to the association. If new
1246 * addresses are added to the association, the endpoint MUST respond
1247 * with an ABORT..
1248 */
1249
1250 /* Search through all current addresses and make sure
1251 * we aren't adding any new ones.
1252 */
1253 new_addr = NULL;
1254 found = 0;
1255
9dbc15f0
RD
1256 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1257 transports) {
1da177e4 1258 found = 0;
9dbc15f0
RD
1259 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1260 transports) {
5f242a13
AV
1261 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1262 &addr->ipaddr)) {
1da177e4
LT
1263 found = 1;
1264 break;
1265 }
1266 }
1267 if (!found)
1268 break;
1269 }
1270
1271 /* If a new address was added, ABORT the sender. */
1272 if (!found && new_addr) {
7dd8a582 1273 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1da177e4
LT
1274 }
1275
1276 /* Return success if all addresses were found. */
1277 return found;
1278}
1279
1280/* Populate the verification/tie tags based on overlapping INIT
1281 * scenario.
1282 *
1283 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1284 */
1285static void sctp_tietags_populate(struct sctp_association *new_asoc,
1286 const struct sctp_association *asoc)
1287{
1288 switch (asoc->state) {
1289
1290 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1291
1292 case SCTP_STATE_COOKIE_WAIT:
1293 new_asoc->c.my_vtag = asoc->c.my_vtag;
1294 new_asoc->c.my_ttag = asoc->c.my_vtag;
1295 new_asoc->c.peer_ttag = 0;
1296 break;
1297
1298 case SCTP_STATE_COOKIE_ECHOED:
1299 new_asoc->c.my_vtag = asoc->c.my_vtag;
1300 new_asoc->c.my_ttag = asoc->c.my_vtag;
1301 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1302 break;
1303
1304 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1305 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1306 */
1307 default:
1308 new_asoc->c.my_ttag = asoc->c.my_vtag;
1309 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1310 break;
3ff50b79 1311 }
1da177e4
LT
1312
1313 /* Other parameters for the endpoint SHOULD be copied from the
1314 * existing parameters of the association (e.g. number of
1315 * outbound streams) into the INIT ACK and cookie.
1316 */
1317 new_asoc->rwnd = asoc->rwnd;
1318 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1319 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1320 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1321}
1322
1323/*
1324 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1325 * handling action.
1326 *
1327 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1328 *
1329 * Returns value representing action to be taken. These action values
1330 * correspond to Action/Description values in RFC 2960, Table 2.
1331 */
1332static char sctp_tietags_compare(struct sctp_association *new_asoc,
1333 const struct sctp_association *asoc)
1334{
1335 /* In this case, the peer may have restarted. */
1336 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1337 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1338 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1339 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1340 return 'A';
1341
1342 /* Collision case B. */
1343 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1344 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1345 (0 == asoc->c.peer_vtag))) {
1346 return 'B';
1347 }
1348
1349 /* Collision case D. */
1350 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1351 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1352 return 'D';
1353
1354 /* Collision case C. */
1355 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1356 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1357 (0 == new_asoc->c.my_ttag) &&
1358 (0 == new_asoc->c.peer_ttag))
1359 return 'C';
1360
1361 /* No match to any of the special cases; discard this packet. */
1362 return 'E';
1363}
1364
1365/* Common helper routine for both duplicate and simulataneous INIT
1366 * chunk handling.
1367 */
1368static sctp_disposition_t sctp_sf_do_unexpected_init(
1369 const struct sctp_endpoint *ep,
1370 const struct sctp_association *asoc,
1371 const sctp_subtype_t type,
1372 void *arg, sctp_cmd_seq_t *commands)
1373{
1374 sctp_disposition_t retval;
1375 struct sctp_chunk *chunk = arg;
1376 struct sctp_chunk *repl;
1377 struct sctp_association *new_asoc;
1378 struct sctp_chunk *err_chunk;
1379 struct sctp_packet *packet;
1380 sctp_unrecognized_param_t *unk_param;
1381 int len;
1382
1383 /* 6.10 Bundling
1384 * An endpoint MUST NOT bundle INIT, INIT ACK or
1385 * SHUTDOWN COMPLETE with any other chunks.
1386 *
1387 * IG Section 2.11.2
1388 * Furthermore, we require that the receiver of an INIT chunk MUST
1389 * enforce these rules by silently discarding an arriving packet
1390 * with an INIT chunk that is bundled with other chunks.
1391 */
1392 if (!chunk->singleton)
1393 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1394
1395 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
d808ad9a 1396 * Tag.
1da177e4
LT
1397 */
1398 if (chunk->sctp_hdr->vtag != 0)
1399 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1400
1401 /* Make sure that the INIT chunk has a valid length.
1402 * In this case, we generate a protocol violation since we have
1403 * an association established.
1404 */
1405 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1406 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1407 commands);
1408 /* Grab the INIT header. */
1409 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1410
1411 /* Tag the variable length parameters. */
1412 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1413
1414 /* Verify the INIT chunk before processing it. */
1415 err_chunk = NULL;
1416 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1417 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1418 &err_chunk)) {
1419 /* This chunk contains fatal error. It is to be discarded.
1420 * Send an ABORT, with causes if there is any.
1421 */
1422 if (err_chunk) {
1423 packet = sctp_abort_pkt_new(ep, asoc, arg,
1424 (__u8 *)(err_chunk->chunk_hdr) +
1425 sizeof(sctp_chunkhdr_t),
1426 ntohs(err_chunk->chunk_hdr->length) -
1427 sizeof(sctp_chunkhdr_t));
1428
1429 if (packet) {
1430 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1431 SCTP_PACKET(packet));
1432 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1433 retval = SCTP_DISPOSITION_CONSUME;
1434 } else {
1435 retval = SCTP_DISPOSITION_NOMEM;
1436 }
1437 goto cleanup;
1438 } else {
1439 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1440 commands);
1441 }
1442 }
1443
1444 /*
1445 * Other parameters for the endpoint SHOULD be copied from the
1446 * existing parameters of the association (e.g. number of
1447 * outbound streams) into the INIT ACK and cookie.
1448 * FIXME: We are copying parameters from the endpoint not the
1449 * association.
1450 */
1451 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1452 if (!new_asoc)
1453 goto nomem;
1454
1455 /* In the outbound INIT ACK the endpoint MUST copy its current
1456 * Verification Tag and Peers Verification tag into a reserved
1457 * place (local tie-tag and per tie-tag) within the state cookie.
1458 */
1459 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1460 sctp_source(chunk),
1461 (sctp_init_chunk_t *)chunk->chunk_hdr,
df7deeb5
VY
1462 GFP_ATOMIC))
1463 goto nomem;
1da177e4
LT
1464
1465 /* Make sure no new addresses are being added during the
1466 * restart. Do not do this check for COOKIE-WAIT state,
1467 * since there are no peer addresses to check against.
1468 * Upon return an ABORT will have been sent if needed.
1469 */
1470 if (!sctp_state(asoc, COOKIE_WAIT)) {
1471 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1472 commands)) {
1473 retval = SCTP_DISPOSITION_CONSUME;
df7deeb5 1474 goto nomem_retval;
1da177e4
LT
1475 }
1476 }
1477
1478 sctp_tietags_populate(new_asoc, asoc);
1479
1480 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1481
1482 /* If there are errors need to be reported for unknown parameters,
1483 * make sure to reserve enough room in the INIT ACK for them.
1484 */
1485 len = 0;
1486 if (err_chunk) {
1487 len = ntohs(err_chunk->chunk_hdr->length) -
1488 sizeof(sctp_chunkhdr_t);
1489 }
1490
1491 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1492 goto nomem;
1493
1494 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1495 if (!repl)
1496 goto nomem;
1497
1498 /* If there are errors need to be reported for unknown parameters,
1499 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1500 * parameter.
1501 */
1502 if (err_chunk) {
1503 /* Get the "Unrecognized parameter" parameter(s) out of the
1504 * ERROR chunk generated by sctp_verify_init(). Since the
1505 * error cause code for "unknown parameter" and the
1506 * "Unrecognized parameter" type is the same, we can
1507 * construct the parameters in INIT ACK by copying the
1508 * ERROR causes over.
1509 */
1510 unk_param = (sctp_unrecognized_param_t *)
1511 ((__u8 *)(err_chunk->chunk_hdr) +
1512 sizeof(sctp_chunkhdr_t));
1513 /* Replace the cause code with the "Unrecognized parameter"
1514 * parameter type.
1515 */
1516 sctp_addto_chunk(repl, len, unk_param);
1517 }
1518
1519 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1520 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1521
1522 /*
1523 * Note: After sending out INIT ACK with the State Cookie parameter,
1524 * "Z" MUST NOT allocate any resources for this new association.
1525 * Otherwise, "Z" will be vulnerable to resource attacks.
1526 */
1527 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1528 retval = SCTP_DISPOSITION_CONSUME;
1529
df7deeb5
VY
1530 return retval;
1531
1532nomem:
1533 retval = SCTP_DISPOSITION_NOMEM;
1534nomem_retval:
1535 if (new_asoc)
1536 sctp_association_free(new_asoc);
1da177e4
LT
1537cleanup:
1538 if (err_chunk)
1539 sctp_chunk_free(err_chunk);
1540 return retval;
1da177e4
LT
1541}
1542
1543/*
1544 * Handle simultanous INIT.
1545 * This means we started an INIT and then we got an INIT request from
1546 * our peer.
1547 *
1548 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1549 * This usually indicates an initialization collision, i.e., each
1550 * endpoint is attempting, at about the same time, to establish an
1551 * association with the other endpoint.
1552 *
1553 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1554 * endpoint MUST respond with an INIT ACK using the same parameters it
1555 * sent in its original INIT chunk (including its Verification Tag,
1556 * unchanged). These original parameters are combined with those from the
1557 * newly received INIT chunk. The endpoint shall also generate a State
1558 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1559 * INIT to calculate the State Cookie.
1560 *
1561 * After that, the endpoint MUST NOT change its state, the T1-init
1562 * timer shall be left running and the corresponding TCB MUST NOT be
1563 * destroyed. The normal procedures for handling State Cookies when
1564 * a TCB exists will resolve the duplicate INITs to a single association.
1565 *
1566 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1567 * its Tie-Tags with the Tag information of itself and its peer (see
1568 * section 5.2.2 for a description of the Tie-Tags).
1569 *
1570 * Verification Tag: Not explicit, but an INIT can not have a valid
1571 * verification tag, so we skip the check.
1572 *
1573 * Inputs
1574 * (endpoint, asoc, chunk)
1575 *
1576 * Outputs
1577 * (asoc, reply_msg, msg_up, timers, counters)
1578 *
1579 * The return value is the disposition of the chunk.
1580 */
1581sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1582 const struct sctp_association *asoc,
1583 const sctp_subtype_t type,
1584 void *arg,
1585 sctp_cmd_seq_t *commands)
1586{
1587 /* Call helper to do the real work for both simulataneous and
1588 * duplicate INIT chunk handling.
1589 */
1590 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1591}
1592
1593/*
1594 * Handle duplicated INIT messages. These are usually delayed
1595 * restransmissions.
1596 *
1597 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1598 * COOKIE-ECHOED and COOKIE-WAIT
1599 *
1600 * Unless otherwise stated, upon reception of an unexpected INIT for
1601 * this association, the endpoint shall generate an INIT ACK with a
1602 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1603 * current Verification Tag and peer's Verification Tag into a reserved
1604 * place within the state cookie. We shall refer to these locations as
1605 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1606 * containing this INIT ACK MUST carry a Verification Tag value equal to
1607 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1608 * MUST contain a new Initiation Tag (randomly generated see Section
1609 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1610 * existing parameters of the association (e.g. number of outbound
1611 * streams) into the INIT ACK and cookie.
1612 *
1613 * After sending out the INIT ACK, the endpoint shall take no further
1614 * actions, i.e., the existing association, including its current state,
1615 * and the corresponding TCB MUST NOT be changed.
1616 *
1617 * Note: Only when a TCB exists and the association is not in a COOKIE-
1618 * WAIT state are the Tie-Tags populated. For a normal association INIT
1619 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1620 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1621 * State Cookie are populated as specified in section 5.2.1.
1622 *
1623 * Verification Tag: Not specified, but an INIT has no way of knowing
1624 * what the verification tag could be, so we ignore it.
1625 *
1626 * Inputs
1627 * (endpoint, asoc, chunk)
1628 *
1629 * Outputs
1630 * (asoc, reply_msg, msg_up, timers, counters)
1631 *
1632 * The return value is the disposition of the chunk.
1633 */
1634sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1635 const struct sctp_association *asoc,
1636 const sctp_subtype_t type,
1637 void *arg,
1638 sctp_cmd_seq_t *commands)
1639{
1640 /* Call helper to do the real work for both simulataneous and
1641 * duplicate INIT chunk handling.
1642 */
1643 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1644}
1645
1646
610ab73a
VY
1647/*
1648 * Unexpected INIT-ACK handler.
1649 *
1650 * Section 5.2.3
1651 * If an INIT ACK received by an endpoint in any state other than the
1652 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1653 * An unexpected INIT ACK usually indicates the processing of an old or
1654 * duplicated INIT chunk.
1655*/
1656sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1657 const struct sctp_association *asoc,
1658 const sctp_subtype_t type,
1659 void *arg, sctp_cmd_seq_t *commands)
1660{
1661 /* Per the above section, we'll discard the chunk if we have an
1662 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1663 */
d808ad9a 1664 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
610ab73a
VY
1665 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1666 else
1667 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1668}
1da177e4
LT
1669
1670/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1671 *
1672 * Section 5.2.4
1673 * A) In this case, the peer may have restarted.
1674 */
1675static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1676 const struct sctp_association *asoc,
1677 struct sctp_chunk *chunk,
1678 sctp_cmd_seq_t *commands,
1679 struct sctp_association *new_asoc)
1680{
1681 sctp_init_chunk_t *peer_init;
1682 struct sctp_ulpevent *ev;
1683 struct sctp_chunk *repl;
1684 struct sctp_chunk *err;
1685 sctp_disposition_t disposition;
1686
1687 /* new_asoc is a brand-new association, so these are not yet
1688 * side effects--it is safe to run them here.
1689 */
1690 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1691
1692 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1693 sctp_source(chunk), peer_init,
1694 GFP_ATOMIC))
1695 goto nomem;
1696
1697 /* Make sure no new addresses are being added during the
1698 * restart. Though this is a pretty complicated attack
1699 * since you'd have to get inside the cookie.
1700 */
1701 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1702 return SCTP_DISPOSITION_CONSUME;
1703 }
1704
1705 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1706 * the peer has restarted (Action A), it MUST NOT setup a new
1707 * association but instead resend the SHUTDOWN ACK and send an ERROR
1708 * chunk with a "Cookie Received while Shutting Down" error cause to
1709 * its peer.
1710 */
1711 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1712 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1713 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1714 chunk, commands);
1715 if (SCTP_DISPOSITION_NOMEM == disposition)
1716 goto nomem;
1717
1718 err = sctp_make_op_error(asoc, chunk,
1719 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1720 NULL, 0);
1721 if (err)
1722 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1723 SCTP_CHUNK(err));
1724
1725 return SCTP_DISPOSITION_CONSUME;
1726 }
1727
1728 /* For now, fail any unsent/unacked data. Consider the optional
1729 * choice of resending of this data.
1730 */
1731 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1732
1da177e4
LT
1733 repl = sctp_make_cookie_ack(new_asoc, chunk);
1734 if (!repl)
1735 goto nomem;
1736
1da177e4
LT
1737 /* Report association restart to upper layer. */
1738 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1739 new_asoc->c.sinit_num_ostreams,
1740 new_asoc->c.sinit_max_instreams,
a5a35e76 1741 NULL, GFP_ATOMIC);
1da177e4
LT
1742 if (!ev)
1743 goto nomem_ev;
1744
df7deeb5
VY
1745 /* Update the content of current association. */
1746 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1da177e4
LT
1748 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1749 return SCTP_DISPOSITION_CONSUME;
1750
1751nomem_ev:
1752 sctp_chunk_free(repl);
1753nomem:
1754 return SCTP_DISPOSITION_NOMEM;
1755}
1756
1757/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1758 *
1759 * Section 5.2.4
1760 * B) In this case, both sides may be attempting to start an association
1761 * at about the same time but the peer endpoint started its INIT
1762 * after responding to the local endpoint's INIT
1763 */
1764/* This case represents an initialization collision. */
1765static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1766 const struct sctp_association *asoc,
1767 struct sctp_chunk *chunk,
1768 sctp_cmd_seq_t *commands,
1769 struct sctp_association *new_asoc)
1770{
1771 sctp_init_chunk_t *peer_init;
1da177e4
LT
1772 struct sctp_chunk *repl;
1773
1774 /* new_asoc is a brand-new association, so these are not yet
1775 * side effects--it is safe to run them here.
1776 */
1777 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1778 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1779 sctp_source(chunk), peer_init,
1780 GFP_ATOMIC))
1781 goto nomem;
1782
1783 /* Update the content of current association. */
1784 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1786 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1787 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1788 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1789
1790 repl = sctp_make_cookie_ack(new_asoc, chunk);
1791 if (!repl)
1792 goto nomem;
1793
1794 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1da177e4
LT
1795
1796 /* RFC 2960 5.1 Normal Establishment of an Association
1797 *
1798 * D) IMPLEMENTATION NOTE: An implementation may choose to
1799 * send the Communication Up notification to the SCTP user
1800 * upon reception of a valid COOKIE ECHO chunk.
07d93967
VY
1801 *
1802 * Sadly, this needs to be implemented as a side-effect, because
1803 * we are not guaranteed to have set the association id of the real
1804 * association and so these notifications need to be delayed until
1805 * the association id is allocated.
1da177e4 1806 */
1da177e4 1807
07d93967 1808 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1da177e4
LT
1809
1810 /* Sockets API Draft Section 5.3.1.6
0f3fffd8 1811 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1da177e4 1812 * delivers this notification to inform the application that of the
0f3fffd8 1813 * peers requested adaptation layer.
07d93967
VY
1814 *
1815 * This also needs to be done as a side effect for the same reason as
1816 * above.
1da177e4 1817 */
07d93967
VY
1818 if (asoc->peer.adaptation_ind)
1819 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1da177e4
LT
1820
1821 return SCTP_DISPOSITION_CONSUME;
1822
1da177e4
LT
1823nomem:
1824 return SCTP_DISPOSITION_NOMEM;
1825}
1826
1827/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1828 *
1829 * Section 5.2.4
1830 * C) In this case, the local endpoint's cookie has arrived late.
1831 * Before it arrived, the local endpoint sent an INIT and received an
1832 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1833 * but a new tag of its own.
1834 */
1835/* This case represents an initialization collision. */
1836static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1837 const struct sctp_association *asoc,
1838 struct sctp_chunk *chunk,
1839 sctp_cmd_seq_t *commands,
1840 struct sctp_association *new_asoc)
1841{
1842 /* The cookie should be silently discarded.
1843 * The endpoint SHOULD NOT change states and should leave
1844 * any timers running.
1845 */
1846 return SCTP_DISPOSITION_DISCARD;
1847}
1848
1849/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1850 *
1851 * Section 5.2.4
1852 *
1853 * D) When both local and remote tags match the endpoint should always
1854 * enter the ESTABLISHED state, if it has not already done so.
1855 */
1856/* This case represents an initialization collision. */
1857static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1858 const struct sctp_association *asoc,
1859 struct sctp_chunk *chunk,
1860 sctp_cmd_seq_t *commands,
1861 struct sctp_association *new_asoc)
1862{
df7deeb5 1863 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1da177e4
LT
1864 struct sctp_chunk *repl;
1865
1866 /* Clarification from Implementor's Guide:
1867 * D) When both local and remote tags match the endpoint should
d808ad9a
YH
1868 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1869 * It should stop any cookie timer that may be running and send
1870 * a COOKIE ACK.
1da177e4
LT
1871 */
1872
1873 /* Don't accidentally move back into established state. */
1874 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1875 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1876 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1877 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1878 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1879 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1880 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1881 SCTP_NULL());
1882
1883 /* RFC 2960 5.1 Normal Establishment of an Association
1884 *
1885 * D) IMPLEMENTATION NOTE: An implementation may choose
1886 * to send the Communication Up notification to the
1887 * SCTP user upon reception of a valid COOKIE
1888 * ECHO chunk.
1889 */
df7deeb5 1890 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1da177e4 1891 SCTP_COMM_UP, 0,
df7deeb5
VY
1892 asoc->c.sinit_num_ostreams,
1893 asoc->c.sinit_max_instreams,
9cbcbf4e 1894 NULL, GFP_ATOMIC);
1da177e4
LT
1895 if (!ev)
1896 goto nomem;
1da177e4
LT
1897
1898 /* Sockets API Draft Section 5.3.1.6
0f3fffd8 1899 * When a peer sends a Adaptation Layer Indication parameter,
1da177e4 1900 * SCTP delivers this notification to inform the application
0f3fffd8 1901 * that of the peers requested adaptation layer.
1da177e4 1902 */
0f3fffd8
ISJ
1903 if (asoc->peer.adaptation_ind) {
1904 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1da177e4 1905 GFP_ATOMIC);
df7deeb5 1906 if (!ai_ev)
1da177e4
LT
1907 goto nomem;
1908
1da177e4
LT
1909 }
1910 }
1da177e4
LT
1911
1912 repl = sctp_make_cookie_ack(new_asoc, chunk);
1913 if (!repl)
1914 goto nomem;
1915
2e3216cd
VY
1916 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1917
df7deeb5
VY
1918 if (ev)
1919 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1920 SCTP_ULPEVENT(ev));
1921 if (ai_ev)
1922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1923 SCTP_ULPEVENT(ai_ev));
1924
1da177e4
LT
1925 return SCTP_DISPOSITION_CONSUME;
1926
1927nomem:
df7deeb5
VY
1928 if (ai_ev)
1929 sctp_ulpevent_free(ai_ev);
1da177e4
LT
1930 if (ev)
1931 sctp_ulpevent_free(ev);
1932 return SCTP_DISPOSITION_NOMEM;
1933}
1934
1935/*
1936 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1937 * chunk was retransmitted and then delayed in the network.
1938 *
1939 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1940 *
1941 * Verification Tag: None. Do cookie validation.
1942 *
1943 * Inputs
1944 * (endpoint, asoc, chunk)
1945 *
1946 * Outputs
1947 * (asoc, reply_msg, msg_up, timers, counters)
1948 *
1949 * The return value is the disposition of the chunk.
1950 */
1951sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1952 const struct sctp_association *asoc,
1953 const sctp_subtype_t type,
1954 void *arg,
1955 sctp_cmd_seq_t *commands)
1956{
1957 sctp_disposition_t retval;
1958 struct sctp_chunk *chunk = arg;
1959 struct sctp_association *new_asoc;
1960 int error = 0;
1961 char action;
1962 struct sctp_chunk *err_chk_p;
1963
1964 /* Make sure that the chunk has a valid length from the protocol
1965 * perspective. In this case check to make sure we have at least
1966 * enough for the chunk header. Cookie length verification is
1967 * done later.
1968 */
1969 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1970 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1971 commands);
1972
1973 /* "Decode" the chunk. We have no optional parameters so we
1974 * are in good shape.
1975 */
d808ad9a 1976 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
62b08083
SS
1977 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1978 sizeof(sctp_chunkhdr_t)))
1979 goto nomem;
1da177e4
LT
1980
1981 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1982 * of a duplicate COOKIE ECHO match the Verification Tags of the
1983 * current association, consider the State Cookie valid even if
1984 * the lifespan is exceeded.
1985 */
1986 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1987 &err_chk_p);
1988
1989 /* FIXME:
1990 * If the re-build failed, what is the proper error path
1991 * from here?
1992 *
1993 * [We should abort the association. --piggy]
1994 */
1995 if (!new_asoc) {
1996 /* FIXME: Several errors are possible. A bad cookie should
1997 * be silently discarded, but think about logging it too.
1998 */
1999 switch (error) {
2000 case -SCTP_IERROR_NOMEM:
2001 goto nomem;
2002
2003 case -SCTP_IERROR_STALE_COOKIE:
2004 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2005 err_chk_p);
2006 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2007 case -SCTP_IERROR_BAD_SIG:
2008 default:
2009 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3ff50b79 2010 }
1da177e4
LT
2011 }
2012
2013 /* Compare the tie_tag in cookie with the verification tag of
2014 * current association.
2015 */
2016 action = sctp_tietags_compare(new_asoc, asoc);
2017
2018 switch (action) {
2019 case 'A': /* Association restart. */
2020 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2021 new_asoc);
2022 break;
2023
2024 case 'B': /* Collision case B. */
2025 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2026 new_asoc);
2027 break;
2028
2029 case 'C': /* Collision case C. */
2030 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2031 new_asoc);
2032 break;
2033
2034 case 'D': /* Collision case D. */
2035 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2036 new_asoc);
2037 break;
2038
2039 default: /* Discard packet for all others. */
2040 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2041 break;
3ff50b79 2042 }
1da177e4
LT
2043
2044 /* Delete the tempory new association. */
2045 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2046 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2047
2048 return retval;
2049
2050nomem:
2051 return SCTP_DISPOSITION_NOMEM;
2052}
2053
2054/*
2055 * Process an ABORT. (SHUTDOWN-PENDING state)
2056 *
2057 * See sctp_sf_do_9_1_abort().
2058 */
2059sctp_disposition_t sctp_sf_shutdown_pending_abort(
2060 const struct sctp_endpoint *ep,
2061 const struct sctp_association *asoc,
2062 const sctp_subtype_t type,
2063 void *arg,
2064 sctp_cmd_seq_t *commands)
2065{
2066 struct sctp_chunk *chunk = arg;
2067
2068 if (!sctp_vtag_verify_either(chunk, asoc))
2069 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2070
2071 /* Make sure that the ABORT chunk has a valid length.
2072 * Since this is an ABORT chunk, we have to discard it
2073 * because of the following text:
2074 * RFC 2960, Section 3.3.7
2075 * If an endpoint receives an ABORT with a format error or for an
2076 * association that doesn't exist, it MUST silently discard it.
2077 * Becasue the length is "invalid", we can't really discard just
2078 * as we do not know its true length. So, to be safe, discard the
2079 * packet.
2080 */
2081 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2082 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2083
75205f47
VY
2084 /* ADD-IP: Special case for ABORT chunks
2085 * F4) One special consideration is that ABORT Chunks arriving
2086 * destined to the IP address being deleted MUST be
2087 * ignored (see Section 5.3.1 for further details).
2088 */
2089 if (SCTP_ADDR_DEL ==
2090 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2091 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2092
75205f47 2093 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1da177e4
LT
2094}
2095
2096/*
2097 * Process an ABORT. (SHUTDOWN-SENT state)
2098 *
2099 * See sctp_sf_do_9_1_abort().
2100 */
2101sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2102 const struct sctp_association *asoc,
2103 const sctp_subtype_t type,
2104 void *arg,
2105 sctp_cmd_seq_t *commands)
2106{
2107 struct sctp_chunk *chunk = arg;
2108
2109 if (!sctp_vtag_verify_either(chunk, asoc))
2110 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2111
2112 /* Make sure that the ABORT chunk has a valid length.
2113 * Since this is an ABORT chunk, we have to discard it
2114 * because of the following text:
2115 * RFC 2960, Section 3.3.7
2116 * If an endpoint receives an ABORT with a format error or for an
2117 * association that doesn't exist, it MUST silently discard it.
2118 * Becasue the length is "invalid", we can't really discard just
2119 * as we do not know its true length. So, to be safe, discard the
2120 * packet.
2121 */
2122 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2123 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2124
75205f47
VY
2125 /* ADD-IP: Special case for ABORT chunks
2126 * F4) One special consideration is that ABORT Chunks arriving
2127 * destined to the IP address being deleted MUST be
2128 * ignored (see Section 5.3.1 for further details).
2129 */
2130 if (SCTP_ADDR_DEL ==
2131 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2132 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2133
1da177e4
LT
2134 /* Stop the T2-shutdown timer. */
2135 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2136 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2137
2138 /* Stop the T5-shutdown guard timer. */
2139 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2140 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2141
75205f47 2142 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1da177e4
LT
2143}
2144
2145/*
2146 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2147 *
2148 * See sctp_sf_do_9_1_abort().
2149 */
2150sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2151 const struct sctp_endpoint *ep,
2152 const struct sctp_association *asoc,
2153 const sctp_subtype_t type,
2154 void *arg,
2155 sctp_cmd_seq_t *commands)
2156{
2157 /* The same T2 timer, so we should be able to use
2158 * common function with the SHUTDOWN-SENT state.
2159 */
2160 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2161}
2162
2163/*
2164 * Handle an Error received in COOKIE_ECHOED state.
2165 *
2166 * Only handle the error type of stale COOKIE Error, the other errors will
2167 * be ignored.
2168 *
2169 * Inputs
2170 * (endpoint, asoc, chunk)
2171 *
2172 * Outputs
2173 * (asoc, reply_msg, msg_up, timers, counters)
2174 *
2175 * The return value is the disposition of the chunk.
2176 */
2177sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2178 const struct sctp_association *asoc,
2179 const sctp_subtype_t type,
2180 void *arg,
2181 sctp_cmd_seq_t *commands)
2182{
2183 struct sctp_chunk *chunk = arg;
2184 sctp_errhdr_t *err;
2185
2186 if (!sctp_vtag_verify(chunk, asoc))
2187 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2188
2189 /* Make sure that the ERROR chunk has a valid length.
2190 * The parameter walking depends on this as well.
2191 */
2192 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2193 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2194 commands);
2195
2196 /* Process the error here */
2197 /* FUTURE FIXME: When PR-SCTP related and other optional
2198 * parms are emitted, this will have to change to handle multiple
2199 * errors.
2200 */
2201 sctp_walk_errors(err, chunk->chunk_hdr) {
2202 if (SCTP_ERROR_STALE_COOKIE == err->cause)
d808ad9a 2203 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
1da177e4
LT
2204 arg, commands);
2205 }
2206
2207 /* It is possible to have malformed error causes, and that
2208 * will cause us to end the walk early. However, since
2209 * we are discarding the packet, there should be no adverse
2210 * affects.
2211 */
2212 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2213}
2214
2215/*
2216 * Handle a Stale COOKIE Error
2217 *
2218 * Section: 5.2.6 Handle Stale COOKIE Error
2219 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2220 * one of the following three alternatives.
2221 * ...
2222 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2223 * Preservative parameter requesting an extension to the lifetime of
2224 * the State Cookie. When calculating the time extension, an
2225 * implementation SHOULD use the RTT information measured based on the
2226 * previous COOKIE ECHO / ERROR exchange, and should add no more
2227 * than 1 second beyond the measured RTT, due to long State Cookie
2228 * lifetimes making the endpoint more subject to a replay attack.
2229 *
2230 * Verification Tag: Not explicit, but safe to ignore.
2231 *
2232 * Inputs
2233 * (endpoint, asoc, chunk)
2234 *
2235 * Outputs
2236 * (asoc, reply_msg, msg_up, timers, counters)
2237 *
2238 * The return value is the disposition of the chunk.
2239 */
2240static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2241 const struct sctp_association *asoc,
2242 const sctp_subtype_t type,
2243 void *arg,
2244 sctp_cmd_seq_t *commands)
2245{
2246 struct sctp_chunk *chunk = arg;
2247 time_t stale;
2248 sctp_cookie_preserve_param_t bht;
2249 sctp_errhdr_t *err;
2250 struct sctp_chunk *reply;
2251 struct sctp_bind_addr *bp;
3f7a87d2 2252 int attempts = asoc->init_err_counter + 1;
1da177e4 2253
81845c21 2254 if (attempts > asoc->max_init_attempts) {
8de8c873
SS
2255 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2256 SCTP_ERROR(ETIMEDOUT));
1da177e4 2257 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
dc251b2b 2258 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
1da177e4
LT
2259 return SCTP_DISPOSITION_DELETE_TCB;
2260 }
2261
2262 err = (sctp_errhdr_t *)(chunk->skb->data);
2263
2264 /* When calculating the time extension, an implementation
2265 * SHOULD use the RTT information measured based on the
2266 * previous COOKIE ECHO / ERROR exchange, and should add no
2267 * more than 1 second beyond the measured RTT, due to long
2268 * State Cookie lifetimes making the endpoint more subject to
2269 * a replay attack.
2270 * Measure of Staleness's unit is usec. (1/1000000 sec)
2271 * Suggested Cookie Life-span Increment's unit is msec.
2272 * (1/1000 sec)
2273 * In general, if you use the suggested cookie life, the value
2274 * found in the field of measure of staleness should be doubled
2275 * to give ample time to retransmit the new cookie and thus
2276 * yield a higher probability of success on the reattempt.
2277 */
34bcca28 2278 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
1da177e4
LT
2279 stale = (stale * 2) / 1000;
2280
2281 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2282 bht.param_hdr.length = htons(sizeof(bht));
2283 bht.lifespan_increment = htonl(stale);
2284
2285 /* Build that new INIT chunk. */
2286 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2287 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2288 if (!reply)
2289 goto nomem;
2290
2291 sctp_addto_chunk(reply, sizeof(bht), &bht);
2292
2293 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2294 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2295
2296 /* Stop pending T3-rtx and heartbeat timers */
2297 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2298 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2299
2300 /* Delete non-primary peer ip addresses since we are transitioning
2301 * back to the COOKIE-WAIT state
2302 */
2303 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2304
d808ad9a
YH
2305 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2306 * resend
1da177e4 2307 */
b6157d8e 2308 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
1da177e4
LT
2309 SCTP_TRANSPORT(asoc->peer.primary_path));
2310
2311 /* Cast away the const modifier, as we want to just
2312 * rerun it through as a sideffect.
2313 */
3f7a87d2 2314 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
1da177e4
LT
2315
2316 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2317 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2318 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2319 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2320 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2321 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2322
2323 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2324
2325 return SCTP_DISPOSITION_CONSUME;
2326
2327nomem:
2328 return SCTP_DISPOSITION_NOMEM;
2329}
2330
2331/*
2332 * Process an ABORT.
2333 *
2334 * Section: 9.1
2335 * After checking the Verification Tag, the receiving endpoint shall
2336 * remove the association from its record, and shall report the
2337 * termination to its upper layer.
2338 *
2339 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2340 * B) Rules for packet carrying ABORT:
2341 *
2342 * - The endpoint shall always fill in the Verification Tag field of the
2343 * outbound packet with the destination endpoint's tag value if it
2344 * is known.
2345 *
2346 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2347 * MUST follow the procedure described in Section 8.4.
2348 *
2349 * - The receiver MUST accept the packet if the Verification Tag
2350 * matches either its own tag, OR the tag of its peer. Otherwise, the
2351 * receiver MUST silently discard the packet and take no further
2352 * action.
2353 *
2354 * Inputs
2355 * (endpoint, asoc, chunk)
2356 *
2357 * Outputs
2358 * (asoc, reply_msg, msg_up, timers, counters)
2359 *
2360 * The return value is the disposition of the chunk.
2361 */
2362sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2363 const struct sctp_association *asoc,
2364 const sctp_subtype_t type,
2365 void *arg,
2366 sctp_cmd_seq_t *commands)
2367{
2368 struct sctp_chunk *chunk = arg;
1da177e4
LT
2369
2370 if (!sctp_vtag_verify_either(chunk, asoc))
2371 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2372
2373 /* Make sure that the ABORT chunk has a valid length.
2374 * Since this is an ABORT chunk, we have to discard it
2375 * because of the following text:
2376 * RFC 2960, Section 3.3.7
2377 * If an endpoint receives an ABORT with a format error or for an
2378 * association that doesn't exist, it MUST silently discard it.
2379 * Becasue the length is "invalid", we can't really discard just
2380 * as we do not know its true length. So, to be safe, discard the
2381 * packet.
2382 */
2383 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2384 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2385
75205f47
VY
2386 /* ADD-IP: Special case for ABORT chunks
2387 * F4) One special consideration is that ABORT Chunks arriving
2388 * destined to the IP address being deleted MUST be
2389 * ignored (see Section 5.3.1 for further details).
2390 */
2391 if (SCTP_ADDR_DEL ==
2392 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2393 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2394
2395 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2396}
2397
2398static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2399 const struct sctp_association *asoc,
2400 const sctp_subtype_t type,
2401 void *arg,
2402 sctp_cmd_seq_t *commands)
2403{
2404 struct sctp_chunk *chunk = arg;
2405 unsigned len;
2406 __be16 error = SCTP_ERROR_NO_ERROR;
2407
1da177e4
LT
2408 /* See if we have an error cause code in the chunk. */
2409 len = ntohs(chunk->chunk_hdr->length);
2410 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2411 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2412
8de8c873 2413 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
d808ad9a 2414 /* ASSOC_FAILED will DELETE_TCB. */
5be291fe 2415 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
1da177e4
LT
2416 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2417 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2418
2419 return SCTP_DISPOSITION_ABORT;
2420}
2421
2422/*
2423 * Process an ABORT. (COOKIE-WAIT state)
2424 *
2425 * See sctp_sf_do_9_1_abort() above.
2426 */
2427sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2428 const struct sctp_association *asoc,
2429 const sctp_subtype_t type,
2430 void *arg,
2431 sctp_cmd_seq_t *commands)
2432{
2433 struct sctp_chunk *chunk = arg;
2434 unsigned len;
f94c0198 2435 __be16 error = SCTP_ERROR_NO_ERROR;
1da177e4
LT
2436
2437 if (!sctp_vtag_verify_either(chunk, asoc))
2438 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2439
2440 /* Make sure that the ABORT chunk has a valid length.
2441 * Since this is an ABORT chunk, we have to discard it
2442 * because of the following text:
2443 * RFC 2960, Section 3.3.7
2444 * If an endpoint receives an ABORT with a format error or for an
2445 * association that doesn't exist, it MUST silently discard it.
2446 * Becasue the length is "invalid", we can't really discard just
2447 * as we do not know its true length. So, to be safe, discard the
2448 * packet.
2449 */
2450 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2451 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2452
2453 /* See if we have an error cause code in the chunk. */
2454 len = ntohs(chunk->chunk_hdr->length);
2455 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2456 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2457
8de8c873
SS
2458 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2459 chunk->transport);
1da177e4
LT
2460}
2461
2462/*
2463 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2464 */
2465sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2466 const struct sctp_association *asoc,
2467 const sctp_subtype_t type,
2468 void *arg,
2469 sctp_cmd_seq_t *commands)
2470{
8de8c873
SS
2471 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2472 ENOPROTOOPT, asoc,
3f7a87d2 2473 (struct sctp_transport *)arg);
1da177e4
LT
2474}
2475
2476/*
2477 * Process an ABORT. (COOKIE-ECHOED state)
2478 */
2479sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2480 const struct sctp_association *asoc,
2481 const sctp_subtype_t type,
2482 void *arg,
2483 sctp_cmd_seq_t *commands)
2484{
2485 /* There is a single T1 timer, so we should be able to use
2486 * common function with the COOKIE-WAIT state.
2487 */
2488 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2489}
2490
2491/*
2492 * Stop T1 timer and abort association with "INIT failed".
2493 *
2494 * This is common code called by several sctp_sf_*_abort() functions above.
2495 */
52c1da39 2496static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
f94c0198 2497 __be16 error, int sk_err,
3f7a87d2
FF
2498 const struct sctp_association *asoc,
2499 struct sctp_transport *transport)
1da177e4 2500{
3f7a87d2 2501 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
1da177e4
LT
2502 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2503 SCTP_STATE(SCTP_STATE_CLOSED));
2504 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2505 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2506 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
8de8c873 2507 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
1da177e4
LT
2508 /* CMD_INIT_FAILED will DELETE_TCB. */
2509 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
dc251b2b 2510 SCTP_PERR(error));
3f7a87d2 2511 return SCTP_DISPOSITION_ABORT;
1da177e4
LT
2512}
2513
2514/*
2515 * sctp_sf_do_9_2_shut
2516 *
2517 * Section: 9.2
2518 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2519 * - enter the SHUTDOWN-RECEIVED state,
2520 *
2521 * - stop accepting new data from its SCTP user
2522 *
2523 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2524 * that all its outstanding DATA chunks have been received by the
2525 * SHUTDOWN sender.
2526 *
2527 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2528 * send a SHUTDOWN in response to a ULP request. And should discard
2529 * subsequent SHUTDOWN chunks.
2530 *
2531 * If there are still outstanding DATA chunks left, the SHUTDOWN
2532 * receiver shall continue to follow normal data transmission
2533 * procedures defined in Section 6 until all outstanding DATA chunks
2534 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2535 * new data from its SCTP user.
2536 *
2537 * Verification Tag: 8.5 Verification Tag [Normal verification]
2538 *
2539 * Inputs
2540 * (endpoint, asoc, chunk)
2541 *
2542 * Outputs
2543 * (asoc, reply_msg, msg_up, timers, counters)
2544 *
2545 * The return value is the disposition of the chunk.
2546 */
2547sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2548 const struct sctp_association *asoc,
2549 const sctp_subtype_t type,
2550 void *arg,
2551 sctp_cmd_seq_t *commands)
2552{
2553 struct sctp_chunk *chunk = arg;
2554 sctp_shutdownhdr_t *sdh;
2555 sctp_disposition_t disposition;
2556 struct sctp_ulpevent *ev;
df10eec4 2557 __u32 ctsn;
1da177e4
LT
2558
2559 if (!sctp_vtag_verify(chunk, asoc))
2560 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2561
2562 /* Make sure that the SHUTDOWN chunk has a valid length. */
2563 if (!sctp_chunk_length_valid(chunk,
2564 sizeof(struct sctp_shutdown_chunk_t)))
2565 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2566 commands);
2567
2568 /* Convert the elaborate header. */
2569 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2570 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2571 chunk->subh.shutdown_hdr = sdh;
df10eec4
WY
2572 ctsn = ntohl(sdh->cum_tsn_ack);
2573
a2f36eec
WY
2574 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2575 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2576 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2577 return SCTP_DISPOSITION_DISCARD;
2578 }
2579
df10eec4
WY
2580 /* If Cumulative TSN Ack beyond the max tsn currently
2581 * send, terminating the association and respond to the
2582 * sender with an ABORT.
2583 */
2584 if (!TSN_lt(ctsn, asoc->next_tsn))
2585 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
1da177e4 2586
eb0e0076
SS
2587 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2588 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2589 * inform the application that it should cease sending data.
2590 */
2591 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2592 if (!ev) {
2593 disposition = SCTP_DISPOSITION_NOMEM;
d808ad9a 2594 goto out;
eb0e0076
SS
2595 }
2596 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2597
1da177e4
LT
2598 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2599 * - enter the SHUTDOWN-RECEIVED state,
2600 * - stop accepting new data from its SCTP user
2601 *
2602 * [This is implicit in the new state.]
2603 */
2604 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2605 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2606 disposition = SCTP_DISPOSITION_CONSUME;
2607
2608 if (sctp_outq_is_empty(&asoc->outqueue)) {
2609 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2610 arg, commands);
2611 }
2612
2613 if (SCTP_DISPOSITION_NOMEM == disposition)
2614 goto out;
2615
2616 /* - verify, by checking the Cumulative TSN Ack field of the
2617 * chunk, that all its outstanding DATA chunks have been
2618 * received by the SHUTDOWN sender.
2619 */
2620 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2178eda8 2621 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
1da177e4 2622
1da177e4
LT
2623out:
2624 return disposition;
2625}
2626
2e3f92da
WY
2627/*
2628 * sctp_sf_do_9_2_shut_ctsn
2629 *
2630 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2631 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2632 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2633 * MUST be processed.
2634 */
2635sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2636 const struct sctp_association *asoc,
2637 const sctp_subtype_t type,
2638 void *arg,
2639 sctp_cmd_seq_t *commands)
2640{
2641 struct sctp_chunk *chunk = arg;
2642 sctp_shutdownhdr_t *sdh;
a2f36eec 2643 __u32 ctsn;
2e3f92da
WY
2644
2645 if (!sctp_vtag_verify(chunk, asoc))
2646 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2647
2648 /* Make sure that the SHUTDOWN chunk has a valid length. */
2649 if (!sctp_chunk_length_valid(chunk,
2650 sizeof(struct sctp_shutdown_chunk_t)))
2651 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2652 commands);
2653
2654 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
a2f36eec
WY
2655 ctsn = ntohl(sdh->cum_tsn_ack);
2656
2657 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2658 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2659 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2660 return SCTP_DISPOSITION_DISCARD;
2661 }
2e3f92da
WY
2662
2663 /* If Cumulative TSN Ack beyond the max tsn currently
2664 * send, terminating the association and respond to the
2665 * sender with an ABORT.
2666 */
a2f36eec 2667 if (!TSN_lt(ctsn, asoc->next_tsn))
2e3f92da
WY
2668 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2669
2670 /* verify, by checking the Cumulative TSN Ack field of the
2671 * chunk, that all its outstanding DATA chunks have been
2672 * received by the SHUTDOWN sender.
2673 */
2674 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2675 SCTP_BE32(sdh->cum_tsn_ack));
2676
2677 return SCTP_DISPOSITION_CONSUME;
2678}
2679
1da177e4
LT
2680/* RFC 2960 9.2
2681 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2682 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2683 * transport addresses (either in the IP addresses or in the INIT chunk)
2684 * that belong to this association, it should discard the INIT chunk and
2685 * retransmit the SHUTDOWN ACK chunk.
2686 */
2687sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2688 const struct sctp_association *asoc,
2689 const sctp_subtype_t type,
2690 void *arg,
2691 sctp_cmd_seq_t *commands)
2692{
2693 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2694 struct sctp_chunk *reply;
2695
ece25dfa
VY
2696 /* Make sure that the chunk has a valid length */
2697 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2698 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2699 commands);
2700
1da177e4
LT
2701 /* Since we are not going to really process this INIT, there
2702 * is no point in verifying chunk boundries. Just generate
2703 * the SHUTDOWN ACK.
2704 */
2705 reply = sctp_make_shutdown_ack(asoc, chunk);
2706 if (NULL == reply)
2707 goto nomem;
2708
2709 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2710 * the T2-SHUTDOWN timer.
2711 */
2712 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2713
2714 /* and restart the T2-shutdown timer. */
2715 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2716 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2717
2718 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2719
2720 return SCTP_DISPOSITION_CONSUME;
2721nomem:
2722 return SCTP_DISPOSITION_NOMEM;
2723}
2724
2725/*
2726 * sctp_sf_do_ecn_cwr
2727 *
2728 * Section: Appendix A: Explicit Congestion Notification
2729 *
2730 * CWR:
2731 *
2732 * RFC 2481 details a specific bit for a sender to send in the header of
2733 * its next outbound TCP segment to indicate to its peer that it has
2734 * reduced its congestion window. This is termed the CWR bit. For
2735 * SCTP the same indication is made by including the CWR chunk.
2736 * This chunk contains one data element, i.e. the TSN number that
2737 * was sent in the ECNE chunk. This element represents the lowest
2738 * TSN number in the datagram that was originally marked with the
2739 * CE bit.
2740 *
2741 * Verification Tag: 8.5 Verification Tag [Normal verification]
2742 * Inputs
2743 * (endpoint, asoc, chunk)
2744 *
2745 * Outputs
2746 * (asoc, reply_msg, msg_up, timers, counters)
2747 *
2748 * The return value is the disposition of the chunk.
2749 */
2750sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2751 const struct sctp_association *asoc,
2752 const sctp_subtype_t type,
2753 void *arg,
2754 sctp_cmd_seq_t *commands)
2755{
2756 sctp_cwrhdr_t *cwr;
2757 struct sctp_chunk *chunk = arg;
34bcca28 2758 u32 lowest_tsn;
1da177e4
LT
2759
2760 if (!sctp_vtag_verify(chunk, asoc))
2761 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2762
2763 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2764 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2765 commands);
d808ad9a 2766
1da177e4
LT
2767 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2768 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2769
34bcca28 2770 lowest_tsn = ntohl(cwr->lowest_tsn);
1da177e4
LT
2771
2772 /* Does this CWR ack the last sent congestion notification? */
34bcca28 2773 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
1da177e4
LT
2774 /* Stop sending ECNE. */
2775 sctp_add_cmd_sf(commands,
2776 SCTP_CMD_ECN_CWR,
34bcca28 2777 SCTP_U32(lowest_tsn));
1da177e4
LT
2778 }
2779 return SCTP_DISPOSITION_CONSUME;
2780}
2781
2782/*
2783 * sctp_sf_do_ecne
2784 *
2785 * Section: Appendix A: Explicit Congestion Notification
2786 *
2787 * ECN-Echo
2788 *
2789 * RFC 2481 details a specific bit for a receiver to send back in its
2790 * TCP acknowledgements to notify the sender of the Congestion
2791 * Experienced (CE) bit having arrived from the network. For SCTP this
2792 * same indication is made by including the ECNE chunk. This chunk
2793 * contains one data element, i.e. the lowest TSN associated with the IP
2794 * datagram marked with the CE bit.....
2795 *
2796 * Verification Tag: 8.5 Verification Tag [Normal verification]
2797 * Inputs
2798 * (endpoint, asoc, chunk)
2799 *
2800 * Outputs
2801 * (asoc, reply_msg, msg_up, timers, counters)
2802 *
2803 * The return value is the disposition of the chunk.
2804 */
2805sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2806 const struct sctp_association *asoc,
2807 const sctp_subtype_t type,
2808 void *arg,
2809 sctp_cmd_seq_t *commands)
2810{
2811 sctp_ecnehdr_t *ecne;
2812 struct sctp_chunk *chunk = arg;
2813
2814 if (!sctp_vtag_verify(chunk, asoc))
2815 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2816
2817 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2818 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2819 commands);
2820
2821 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2822 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2823
2824 /* If this is a newer ECNE than the last CWR packet we sent out */
2825 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2826 SCTP_U32(ntohl(ecne->lowest_tsn)));
2827
2828 return SCTP_DISPOSITION_CONSUME;
2829}
2830
2831/*
2832 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2833 *
2834 * The SCTP endpoint MUST always acknowledge the reception of each valid
2835 * DATA chunk.
2836 *
2837 * The guidelines on delayed acknowledgement algorithm specified in
2838 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2839 * acknowledgement SHOULD be generated for at least every second packet
2840 * (not every second DATA chunk) received, and SHOULD be generated within
2841 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2842 * situations it may be beneficial for an SCTP transmitter to be more
2843 * conservative than the algorithms detailed in this document allow.
2844 * However, an SCTP transmitter MUST NOT be more aggressive than the
2845 * following algorithms allow.
2846 *
2847 * A SCTP receiver MUST NOT generate more than one SACK for every
2848 * incoming packet, other than to update the offered window as the
2849 * receiving application consumes new data.
2850 *
2851 * Verification Tag: 8.5 Verification Tag [Normal verification]
2852 *
2853 * Inputs
2854 * (endpoint, asoc, chunk)
2855 *
2856 * Outputs
2857 * (asoc, reply_msg, msg_up, timers, counters)
2858 *
2859 * The return value is the disposition of the chunk.
2860 */
2861sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2862 const struct sctp_association *asoc,
2863 const sctp_subtype_t type,
2864 void *arg,
2865 sctp_cmd_seq_t *commands)
2866{
2867 struct sctp_chunk *chunk = arg;
2868 int error;
2869
2870 if (!sctp_vtag_verify(chunk, asoc)) {
2871 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2872 SCTP_NULL());
2873 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
d808ad9a 2874 }
1da177e4
LT
2875
2876 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2877 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2878 commands);
2879
2880 error = sctp_eat_data(asoc, chunk, commands );
2881 switch (error) {
2882 case SCTP_IERROR_NO_ERROR:
2883 break;
2884 case SCTP_IERROR_HIGH_TSN:
2885 case SCTP_IERROR_BAD_STREAM:
ac0b0462 2886 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
1da177e4
LT
2887 goto discard_noforce;
2888 case SCTP_IERROR_DUP_TSN:
2889 case SCTP_IERROR_IGNORE_TSN:
ac0b0462 2890 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
1da177e4
LT
2891 goto discard_force;
2892 case SCTP_IERROR_NO_DATA:
2893 goto consume;
2894 default:
2895 BUG();
2896 }
2897
2898 if (asoc->autoclose) {
2899 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2900 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2901 }
2902
2903 /* If this is the last chunk in a packet, we need to count it
2904 * toward sack generation. Note that we need to SACK every
2905 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2906 * THEM. We elect to NOT generate SACK's if the chunk fails
2907 * the verification tag test.
2908 *
2909 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2910 *
2911 * The SCTP endpoint MUST always acknowledge the reception of
2912 * each valid DATA chunk.
2913 *
2914 * The guidelines on delayed acknowledgement algorithm
2915 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2916 * Specifically, an acknowledgement SHOULD be generated for at
2917 * least every second packet (not every second DATA chunk)
2918 * received, and SHOULD be generated within 200 ms of the
2919 * arrival of any unacknowledged DATA chunk. In some
2920 * situations it may be beneficial for an SCTP transmitter to
2921 * be more conservative than the algorithms detailed in this
2922 * document allow. However, an SCTP transmitter MUST NOT be
2923 * more aggressive than the following algorithms allow.
2924 */
52ccb8e9 2925 if (chunk->end_of_packet)
1da177e4
LT
2926 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2927
1da177e4
LT
2928 return SCTP_DISPOSITION_CONSUME;
2929
2930discard_force:
2931 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2932 *
2933 * When a packet arrives with duplicate DATA chunk(s) and with
2934 * no new DATA chunk(s), the endpoint MUST immediately send a
2935 * SACK with no delay. If a packet arrives with duplicate
2936 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2937 * MAY immediately send a SACK. Normally receipt of duplicate
2938 * DATA chunks will occur when the original SACK chunk was lost
2939 * and the peer's RTO has expired. The duplicate TSN number(s)
2940 * SHOULD be reported in the SACK as duplicate.
2941 */
2942 /* In our case, we split the MAY SACK advice up whether or not
2943 * the last chunk is a duplicate.'
2944 */
2945 if (chunk->end_of_packet)
2946 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2947 return SCTP_DISPOSITION_DISCARD;
2948
2949discard_noforce:
52ccb8e9 2950 if (chunk->end_of_packet)
1da177e4
LT
2951 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2952
1da177e4
LT
2953 return SCTP_DISPOSITION_DISCARD;
2954consume:
2955 return SCTP_DISPOSITION_CONSUME;
d808ad9a 2956
1da177e4
LT
2957}
2958
2959/*
2960 * sctp_sf_eat_data_fast_4_4
2961 *
2962 * Section: 4 (4)
2963 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2964 * DATA chunks without delay.
2965 *
2966 * Verification Tag: 8.5 Verification Tag [Normal verification]
2967 * Inputs
2968 * (endpoint, asoc, chunk)
2969 *
2970 * Outputs
2971 * (asoc, reply_msg, msg_up, timers, counters)
2972 *
2973 * The return value is the disposition of the chunk.
2974 */
2975sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2976 const struct sctp_association *asoc,
2977 const sctp_subtype_t type,
2978 void *arg,
2979 sctp_cmd_seq_t *commands)
2980{
2981 struct sctp_chunk *chunk = arg;
2982 int error;
2983
2984 if (!sctp_vtag_verify(chunk, asoc)) {
2985 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2986 SCTP_NULL());
2987 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2988 }
2989
2990 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2991 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2992 commands);
2993
2994 error = sctp_eat_data(asoc, chunk, commands );
2995 switch (error) {
2996 case SCTP_IERROR_NO_ERROR:
2997 case SCTP_IERROR_HIGH_TSN:
2998 case SCTP_IERROR_DUP_TSN:
2999 case SCTP_IERROR_IGNORE_TSN:
3000 case SCTP_IERROR_BAD_STREAM:
3001 break;
3002 case SCTP_IERROR_NO_DATA:
3003 goto consume;
3004 default:
3005 BUG();
3006 }
3007
3008 /* Go a head and force a SACK, since we are shutting down. */
3009
3010 /* Implementor's Guide.
3011 *
3012 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3013 * respond to each received packet containing one or more DATA chunk(s)
3014 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3015 */
3016 if (chunk->end_of_packet) {
3017 /* We must delay the chunk creation since the cumulative
3018 * TSN has not been updated yet.
3019 */
3020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3021 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3022 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3023 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3024 }
3025
3026consume:
3027 return SCTP_DISPOSITION_CONSUME;
3028}
3029
3030/*
3031 * Section: 6.2 Processing a Received SACK
3032 * D) Any time a SACK arrives, the endpoint performs the following:
3033 *
3034 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3035 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3036 * increasing, a SACK whose Cumulative TSN Ack is less than the
3037 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3038 *
3039 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3040 * of bytes still outstanding after processing the Cumulative TSN Ack
3041 * and the Gap Ack Blocks.
3042 *
3043 * iii) If the SACK is missing a TSN that was previously
3044 * acknowledged via a Gap Ack Block (e.g., the data receiver
3045 * reneged on the data), then mark the corresponding DATA chunk
3046 * as available for retransmit: Mark it as missing for fast
3047 * retransmit as described in Section 7.2.4 and if no retransmit
3048 * timer is running for the destination address to which the DATA
3049 * chunk was originally transmitted, then T3-rtx is started for
3050 * that destination address.
3051 *
3052 * Verification Tag: 8.5 Verification Tag [Normal verification]
3053 *
3054 * Inputs
3055 * (endpoint, asoc, chunk)
3056 *
3057 * Outputs
3058 * (asoc, reply_msg, msg_up, timers, counters)
3059 *
3060 * The return value is the disposition of the chunk.
3061 */
3062sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3063 const struct sctp_association *asoc,
3064 const sctp_subtype_t type,
3065 void *arg,
3066 sctp_cmd_seq_t *commands)
3067{
3068 struct sctp_chunk *chunk = arg;
3069 sctp_sackhdr_t *sackh;
3070 __u32 ctsn;
3071
3072 if (!sctp_vtag_verify(chunk, asoc))
3073 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3074
3075 /* Make sure that the SACK chunk has a valid length. */
3076 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3077 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3078 commands);
3079
3080 /* Pull the SACK chunk from the data buffer */
3081 sackh = sctp_sm_pull_sack(chunk);
3082 /* Was this a bogus SACK? */
3083 if (!sackh)
3084 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3085 chunk->subh.sack_hdr = sackh;
3086 ctsn = ntohl(sackh->cum_tsn_ack);
3087
3088 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3089 * Ack Point, then drop the SACK. Since Cumulative TSN
3090 * Ack is monotonically increasing, a SACK whose
3091 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3092 * Point indicates an out-of-order SACK.
3093 */
3094 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3095 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3096 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3097 return SCTP_DISPOSITION_DISCARD;
3098 }
3099
aecedeab
WY
3100 /* If Cumulative TSN Ack beyond the max tsn currently
3101 * send, terminating the association and respond to the
3102 * sender with an ABORT.
3103 */
3104 if (!TSN_lt(ctsn, asoc->next_tsn))
3105 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3106
1da177e4
LT
3107 /* Return this SACK for further processing. */
3108 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3109
3110 /* Note: We do the rest of the work on the PROCESS_SACK
3111 * sideeffect.
3112 */
3113 return SCTP_DISPOSITION_CONSUME;
3114}
3115
3116/*
3117 * Generate an ABORT in response to a packet.
3118 *
047a2428 3119 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
1da177e4 3120 *
047a2428
JF
3121 * 8) The receiver should respond to the sender of the OOTB packet with
3122 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3123 * MUST fill in the Verification Tag field of the outbound packet
3124 * with the value found in the Verification Tag field of the OOTB
3125 * packet and set the T-bit in the Chunk Flags to indicate that the
3126 * Verification Tag is reflected. After sending this ABORT, the
3127 * receiver of the OOTB packet shall discard the OOTB packet and take
3128 * no further action.
1da177e4
LT
3129 *
3130 * Verification Tag:
3131 *
3132 * The return value is the disposition of the chunk.
3133*/
ece25dfa 3134static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
1da177e4
LT
3135 const struct sctp_association *asoc,
3136 const sctp_subtype_t type,
3137 void *arg,
3138 sctp_cmd_seq_t *commands)
3139{
3140 struct sctp_packet *packet = NULL;
3141 struct sctp_chunk *chunk = arg;
3142 struct sctp_chunk *abort;
3143
3144 packet = sctp_ootb_pkt_new(asoc, chunk);
3145
3146 if (packet) {
3147 /* Make an ABORT. The T bit will be set if the asoc
3148 * is NULL.
3149 */
d808ad9a 3150 abort = sctp_make_abort(asoc, chunk, 0);
1da177e4
LT
3151 if (!abort) {
3152 sctp_ootb_pkt_free(packet);
3153 return SCTP_DISPOSITION_NOMEM;
3154 }
3155
047a2428
JF
3156 /* Reflect vtag if T-Bit is set */
3157 if (sctp_test_T_bit(abort))
3158 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3159
1da177e4
LT
3160 /* Set the skb to the belonging sock for accounting. */
3161 abort->skb->sk = ep->base.sk;
3162
3163 sctp_packet_append_chunk(packet, abort);
3164
3165 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3166 SCTP_PACKET(packet));
3167
3168 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3169
d3f25968 3170 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1da177e4
LT
3171 return SCTP_DISPOSITION_CONSUME;
3172 }
3173
3174 return SCTP_DISPOSITION_NOMEM;
3175}
3176
3177/*
3178 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3179 * event as ULP notification for each cause included in the chunk.
3180 *
3181 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3182 *
3183 * The return value is the disposition of the chunk.
3184*/
3185sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3186 const struct sctp_association *asoc,
3187 const sctp_subtype_t type,
3188 void *arg,
3189 sctp_cmd_seq_t *commands)
3190{
3191 struct sctp_chunk *chunk = arg;
1da177e4
LT
3192
3193 if (!sctp_vtag_verify(chunk, asoc))
3194 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3195
3196 /* Make sure that the ERROR chunk has a valid length. */
3197 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3198 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3199 commands);
3200
3df26787
WY
3201 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3202 SCTP_CHUNK(chunk));
1da177e4 3203
1da177e4 3204 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
3205}
3206
3207/*
3208 * Process an inbound SHUTDOWN ACK.
3209 *
3210 * From Section 9.2:
3211 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3212 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3213 * peer, and remove all record of the association.
3214 *
3215 * The return value is the disposition.
3216 */
3217sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3218 const struct sctp_association *asoc,
3219 const sctp_subtype_t type,
3220 void *arg,
3221 sctp_cmd_seq_t *commands)
3222{
3223 struct sctp_chunk *chunk = arg;
3224 struct sctp_chunk *reply;
3225 struct sctp_ulpevent *ev;
3226
3227 if (!sctp_vtag_verify(chunk, asoc))
3228 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3229
3230 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3231 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3232 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3233 commands);
1da177e4
LT
3234 /* 10.2 H) SHUTDOWN COMPLETE notification
3235 *
3236 * When SCTP completes the shutdown procedures (section 9.2) this
3237 * notification is passed to the upper layer.
3238 */
3239 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
a5a35e76 3240 0, 0, 0, NULL, GFP_ATOMIC);
1da177e4
LT
3241 if (!ev)
3242 goto nomem;
3243
df7deeb5
VY
3244 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3245 reply = sctp_make_shutdown_complete(asoc, chunk);
3246 if (!reply)
3247 goto nomem_chunk;
3248
3249 /* Do all the commands now (after allocation), so that we
3250 * have consistent state if memory allocation failes
3251 */
1da177e4
LT
3252 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3253
3254 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3255 * stop the T2-shutdown timer,
3256 */
3257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3258 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3259
3260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3261 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3262
1da177e4
LT
3263 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3264 SCTP_STATE(SCTP_STATE_CLOSED));
3265 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3266 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3267 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3268
3269 /* ...and remove all record of the association. */
3270 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3271 return SCTP_DISPOSITION_DELETE_TCB;
3272
df7deeb5
VY
3273nomem_chunk:
3274 sctp_ulpevent_free(ev);
1da177e4
LT
3275nomem:
3276 return SCTP_DISPOSITION_NOMEM;
3277}
3278
3279/*
047a2428
JF
3280 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3281 *
1da177e4
LT
3282 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3283 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3284 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3285 * packet must fill in the Verification Tag field of the outbound
3286 * packet with the Verification Tag received in the SHUTDOWN ACK and
047a2428
JF
3287 * set the T-bit in the Chunk Flags to indicate that the Verification
3288 * Tag is reflected.
1da177e4
LT
3289 *
3290 * 8) The receiver should respond to the sender of the OOTB packet with
3291 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3292 * MUST fill in the Verification Tag field of the outbound packet
3293 * with the value found in the Verification Tag field of the OOTB
047a2428
JF
3294 * packet and set the T-bit in the Chunk Flags to indicate that the
3295 * Verification Tag is reflected. After sending this ABORT, the
3296 * receiver of the OOTB packet shall discard the OOTB packet and take
3297 * no further action.
1da177e4
LT
3298 */
3299sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3300 const struct sctp_association *asoc,
3301 const sctp_subtype_t type,
3302 void *arg,
3303 sctp_cmd_seq_t *commands)
3304{
3305 struct sctp_chunk *chunk = arg;
3306 struct sk_buff *skb = chunk->skb;
3307 sctp_chunkhdr_t *ch;
3308 __u8 *ch_end;
3309 int ootb_shut_ack = 0;
3310
3311 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3312
3313 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3314 do {
ece25dfa 3315 /* Report violation if the chunk is less then minimal */
1da177e4 3316 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
ece25dfa
VY
3317 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3318 commands);
1da177e4 3319
ece25dfa
VY
3320 /* Now that we know we at least have a chunk header,
3321 * do things that are type appropriate.
3322 */
1da177e4
LT
3323 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3324 ootb_shut_ack = 1;
3325
3326 /* RFC 2960, Section 3.3.7
3327 * Moreover, under any circumstances, an endpoint that
3328 * receives an ABORT MUST NOT respond to that ABORT by
3329 * sending an ABORT of its own.
3330 */
3331 if (SCTP_CID_ABORT == ch->type)
3332 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
d808ad9a 3333
ece25dfa
VY
3334 /* Report violation if chunk len overflows */
3335 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3336 if (ch_end > skb_tail_pointer(skb))
3337 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3338 commands);
3339
1da177e4 3340 ch = (sctp_chunkhdr_t *) ch_end;
27a884dc 3341 } while (ch_end < skb_tail_pointer(skb));
1da177e4
LT
3342
3343 if (ootb_shut_ack)
d3f25968 3344 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
1da177e4 3345 else
d3f25968 3346 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1da177e4
LT
3347}
3348
3349/*
3350 * Handle an "Out of the blue" SHUTDOWN ACK.
3351 *
047a2428
JF
3352 * Section: 8.4 5, sctpimpguide 2.41.
3353 *
1da177e4 3354 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
047a2428
JF
3355 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3356 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3357 * packet must fill in the Verification Tag field of the outbound
3358 * packet with the Verification Tag received in the SHUTDOWN ACK and
3359 * set the T-bit in the Chunk Flags to indicate that the Verification
3360 * Tag is reflected.
1da177e4
LT
3361 *
3362 * Inputs
3363 * (endpoint, asoc, type, arg, commands)
3364 *
3365 * Outputs
3366 * (sctp_disposition_t)
3367 *
3368 * The return value is the disposition of the chunk.
3369 */
3370static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3371 const struct sctp_association *asoc,
3372 const sctp_subtype_t type,
3373 void *arg,
3374 sctp_cmd_seq_t *commands)
3375{
3376 struct sctp_packet *packet = NULL;
3377 struct sctp_chunk *chunk = arg;
3378 struct sctp_chunk *shut;
3379
3380 packet = sctp_ootb_pkt_new(asoc, chunk);
3381
3382 if (packet) {
3383 /* Make an SHUTDOWN_COMPLETE.
d808ad9a
YH
3384 * The T bit will be set if the asoc is NULL.
3385 */
1da177e4
LT
3386 shut = sctp_make_shutdown_complete(asoc, chunk);
3387 if (!shut) {
3388 sctp_ootb_pkt_free(packet);
3389 return SCTP_DISPOSITION_NOMEM;
3390 }
3391
047a2428
JF
3392 /* Reflect vtag if T-Bit is set */
3393 if (sctp_test_T_bit(shut))
3394 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3395
1da177e4
LT
3396 /* Set the skb to the belonging sock for accounting. */
3397 shut->skb->sk = ep->base.sk;
3398
3399 sctp_packet_append_chunk(packet, shut);
3400
3401 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3402 SCTP_PACKET(packet));
3403
3404 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3405
3406 /* If the chunk length is invalid, we don't want to process
3407 * the reset of the packet.
3408 */
3409 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3410 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3411
d3f25968
VY
3412 /* We need to discard the rest of the packet to prevent
3413 * potential bomming attacks from additional bundled chunks.
3414 * This is documented in SCTP Threats ID.
3415 */
3416 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1da177e4
LT
3417 }
3418
3419 return SCTP_DISPOSITION_NOMEM;
3420}
3421
3422/*
3423 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3424 *
3425 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3426 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3427 * procedures in section 8.4 SHOULD be followed, in other words it
3428 * should be treated as an Out Of The Blue packet.
3429 * [This means that we do NOT check the Verification Tag on these
3430 * chunks. --piggy ]
3431 *
3432 */
3433sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3434 const struct sctp_association *asoc,
3435 const sctp_subtype_t type,
3436 void *arg,
3437 sctp_cmd_seq_t *commands)
3438{
ece25dfa
VY
3439 struct sctp_chunk *chunk = arg;
3440
3441 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3442 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3443 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3444 commands);
3445
1da177e4
LT
3446 /* Although we do have an association in this case, it corresponds
3447 * to a restarted association. So the packet is treated as an OOTB
3448 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3449 * called with a NULL association.
3450 */
8190f89d
WY
3451 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3452
1da177e4
LT
3453 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3454}
3455
3456/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3457sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3458 const struct sctp_association *asoc,
3459 const sctp_subtype_t type, void *arg,
3460 sctp_cmd_seq_t *commands)
3461{
3462 struct sctp_chunk *chunk = arg;
3463 struct sctp_chunk *asconf_ack = NULL;
6f4c618d 3464 struct sctp_paramhdr *err_param = NULL;
1da177e4 3465 sctp_addiphdr_t *hdr;
6f4c618d 3466 union sctp_addr_param *addr_param;
1da177e4 3467 __u32 serial;
6f4c618d 3468 int length;
1da177e4
LT
3469
3470 if (!sctp_vtag_verify(chunk, asoc)) {
3471 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3472 SCTP_NULL());
3473 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3474 }
3475
6afd2e83
VY
3476 /* ADD-IP: Section 4.1.1
3477 * This chunk MUST be sent in an authenticated way by using
3478 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3479 * is received unauthenticated it MUST be silently discarded as
3480 * described in [I-D.ietf-tsvwg-sctp-auth].
3481 */
3482 if (!sctp_addip_noauth && !chunk->auth)
3483 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3484
1da177e4
LT
3485 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3486 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3487 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3488 commands);
3489
3490 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3491 serial = ntohl(hdr->serial);
3492
6f4c618d
WY
3493 addr_param = (union sctp_addr_param *)hdr->params;
3494 length = ntohs(addr_param->p.length);
3495 if (length < sizeof(sctp_paramhdr_t))
ba016670 3496 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
6f4c618d
WY
3497 (void *)addr_param, commands);
3498
3499 /* Verify the ASCONF chunk before processing it. */
3500 if (!sctp_verify_asconf(asoc,
a08de64d
VY
3501 (sctp_paramhdr_t *)((void *)addr_param + length),
3502 (void *)chunk->chunk_end,
3503 &err_param))
ba016670
WY
3504 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3505 (void *)err_param, commands);
6f4c618d 3506
a08de64d 3507 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
1da177e4 3508 * the endpoint stored in a new association variable
d808ad9a 3509 * 'Peer-Serial-Number'.
1da177e4
LT
3510 */
3511 if (serial == asoc->peer.addip_serial + 1) {
a08de64d
VY
3512 /* If this is the first instance of ASCONF in the packet,
3513 * we can clean our old ASCONF-ACKs.
3514 */
3515 if (!chunk->has_asconf)
3516 sctp_assoc_clean_asconf_ack_cache(asoc);
3517
3518 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3519 * expected, process the ASCONF as described below and after
3520 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3521 * the response packet and cache a copy of it (in the event it
3522 * later needs to be retransmitted).
3523 *
3524 * Essentially, do V1-V5.
1da177e4
LT
3525 */
3526 asconf_ack = sctp_process_asconf((struct sctp_association *)
3527 asoc, chunk);
3528 if (!asconf_ack)
3529 return SCTP_DISPOSITION_NOMEM;
a08de64d
VY
3530 } else if (serial < asoc->peer.addip_serial + 1) {
3531 /* ADDIP 5.2 E2)
3532 * If the value found in the Sequence Number is less than the
3533 * ('Peer- Sequence-Number' + 1), simply skip to the next
3534 * ASCONF, and include in the outbound response packet
3535 * any previously cached ASCONF-ACK response that was
3536 * sent and saved that matches the Sequence Number of the
3537 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3538 * Chunk exists. This will occur when an older ASCONF
3539 * arrives out of order. In such a case, the receiver
3540 * should skip the ASCONF Chunk and not include ASCONF-ACK
3541 * Chunk for that chunk.
1da177e4 3542 */
a08de64d
VY
3543 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3544 if (!asconf_ack)
1da177e4
LT
3545 return SCTP_DISPOSITION_DISCARD;
3546 } else {
a08de64d 3547 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
1da177e4 3548 * it must be either a stale packet or from an attacker.
d808ad9a 3549 */
1da177e4
LT
3550 return SCTP_DISPOSITION_DISCARD;
3551 }
3552
a08de64d
VY
3553 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3554 * containing the ASCONF-ACK Chunks MUST be the source address of
3555 * the SCTP packet that held the ASCONF Chunks.
3556 *
3557 * To do this properly, we'll set the destination address of the chunk
3558 * and at the transmit time, will try look up the transport to use.
3559 * Since ASCONFs may be bundled, the correct transport may not be
3560 * created untill we process the entire packet, thus this workaround.
1da177e4 3561 */
a08de64d 3562 asconf_ack->dest = chunk->source;
1da177e4 3563 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
d808ad9a 3564
1da177e4
LT
3565 return SCTP_DISPOSITION_CONSUME;
3566}
3567
3568/*
3569 * ADDIP Section 4.3 General rules for address manipulation
3570 * When building TLV parameters for the ASCONF Chunk that will add or
3571 * delete IP addresses the D0 to D13 rules should be applied:
3572 */
3573sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3574 const struct sctp_association *asoc,
d808ad9a 3575 const sctp_subtype_t type, void *arg,
1da177e4
LT
3576 sctp_cmd_seq_t *commands)
3577{
3578 struct sctp_chunk *asconf_ack = arg;
3579 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3580 struct sctp_chunk *abort;
6f4c618d 3581 struct sctp_paramhdr *err_param = NULL;
1da177e4
LT
3582 sctp_addiphdr_t *addip_hdr;
3583 __u32 sent_serial, rcvd_serial;
3584
3585 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3586 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3587 SCTP_NULL());
3588 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3589 }
3590
6afd2e83
VY
3591 /* ADD-IP, Section 4.1.2:
3592 * This chunk MUST be sent in an authenticated way by using
3593 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3594 * is received unauthenticated it MUST be silently discarded as
3595 * described in [I-D.ietf-tsvwg-sctp-auth].
3596 */
3597 if (!sctp_addip_noauth && !asconf_ack->auth)
3598 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3599
1da177e4
LT
3600 /* Make sure that the ADDIP chunk has a valid length. */
3601 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3602 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3603 commands);
3604
3605 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3606 rcvd_serial = ntohl(addip_hdr->serial);
3607
6f4c618d
WY
3608 /* Verify the ASCONF-ACK chunk before processing it. */
3609 if (!sctp_verify_asconf(asoc,
3610 (sctp_paramhdr_t *)addip_hdr->params,
3611 (void *)asconf_ack->chunk_end,
3612 &err_param))
ba016670
WY
3613 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3614 (void *)err_param, commands);
6f4c618d 3615
1da177e4
LT
3616 if (last_asconf) {
3617 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3618 sent_serial = ntohl(addip_hdr->serial);
3619 } else {
3620 sent_serial = asoc->addip_serial - 1;
3621 }
3622
3623 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3624 * equal to the next serial number to be used but no ASCONF chunk is
3625 * outstanding the endpoint MUST ABORT the association. Note that a
3626 * sequence number is greater than if it is no more than 2^^31-1
3627 * larger than the current sequence number (using serial arithmetic).
3628 */
3629 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3630 !(asoc->addip_last_asconf)) {
3631 abort = sctp_make_abort(asoc, asconf_ack,
3632 sizeof(sctp_errhdr_t));
3633 if (abort) {
00f1c2df 3634 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
1da177e4
LT
3635 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3636 SCTP_CHUNK(abort));
3637 }
3638 /* We are going to ABORT, so we might as well stop
3639 * processing the rest of the chunks in the packet.
3640 */
3641 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3642 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3643 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
8de8c873 3644 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
d808ad9a 3645 SCTP_ERROR(ECONNABORTED));
1da177e4 3646 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 3647 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
1da177e4
LT
3648 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3649 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3650 return SCTP_DISPOSITION_ABORT;
3651 }
3652
3653 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3654 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3655 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3656
3657 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3658 asconf_ack))
3659 return SCTP_DISPOSITION_CONSUME;
3660
3661 abort = sctp_make_abort(asoc, asconf_ack,
3662 sizeof(sctp_errhdr_t));
3663 if (abort) {
00f1c2df 3664 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
1da177e4
LT
3665 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3666 SCTP_CHUNK(abort));
3667 }
3668 /* We are going to ABORT, so we might as well stop
3669 * processing the rest of the chunks in the packet.
3670 */
3671 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
8de8c873 3672 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
d808ad9a 3673 SCTP_ERROR(ECONNABORTED));
1da177e4 3674 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 3675 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
1da177e4
LT
3676 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3677 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3678 return SCTP_DISPOSITION_ABORT;
3679 }
3680
3681 return SCTP_DISPOSITION_DISCARD;
3682}
3683
3684/*
3685 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3686 *
3687 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3688 * its cumulative TSN point to the value carried in the FORWARD TSN
3689 * chunk, and then MUST further advance its cumulative TSN point locally
3690 * if possible.
3691 * After the above processing, the data receiver MUST stop reporting any
3692 * missing TSNs earlier than or equal to the new cumulative TSN point.
3693 *
3694 * Verification Tag: 8.5 Verification Tag [Normal verification]
3695 *
3696 * The return value is the disposition of the chunk.
3697 */
3698sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3699 const struct sctp_association *asoc,
3700 const sctp_subtype_t type,
3701 void *arg,
3702 sctp_cmd_seq_t *commands)
3703{
3704 struct sctp_chunk *chunk = arg;
3705 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
9fcb95a1 3706 struct sctp_fwdtsn_skip *skip;
1da177e4
LT
3707 __u16 len;
3708 __u32 tsn;
3709
3710 if (!sctp_vtag_verify(chunk, asoc)) {
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3712 SCTP_NULL());
3713 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3714 }
3715
3716 /* Make sure that the FORWARD_TSN chunk has valid length. */
3717 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3718 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3719 commands);
3720
3721 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3722 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3723 len = ntohs(chunk->chunk_hdr->length);
3724 len -= sizeof(struct sctp_chunkhdr);
3725 skb_pull(chunk->skb, len);
3726
3727 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
0dc47877 3728 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
1da177e4
LT
3729
3730 /* The TSN is too high--silently discard the chunk and count on it
3731 * getting retransmitted later.
3732 */
3733 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3734 goto discard_noforce;
3735
9fcb95a1
WY
3736 /* Silently discard the chunk if stream-id is not valid */
3737 sctp_walk_fwdtsn(skip, chunk) {
3738 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3739 goto discard_noforce;
3740 }
3741
1da177e4
LT
3742 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3743 if (len > sizeof(struct sctp_fwdtsn_hdr))
d808ad9a 3744 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
1da177e4 3745 SCTP_CHUNK(chunk));
d808ad9a 3746
1da177e4
LT
3747 /* Count this as receiving DATA. */
3748 if (asoc->autoclose) {
3749 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3750 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3751 }
d808ad9a 3752
1da177e4 3753 /* FIXME: For now send a SACK, but DATA processing may
d808ad9a 3754 * send another.
1da177e4
LT
3755 */
3756 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
1da177e4
LT
3757
3758 return SCTP_DISPOSITION_CONSUME;
3759
3760discard_noforce:
3761 return SCTP_DISPOSITION_DISCARD;
3762}
3763
3764sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3765 const struct sctp_endpoint *ep,
3766 const struct sctp_association *asoc,
3767 const sctp_subtype_t type,
3768 void *arg,
3769 sctp_cmd_seq_t *commands)
3770{
3771 struct sctp_chunk *chunk = arg;
3772 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
9fcb95a1 3773 struct sctp_fwdtsn_skip *skip;
1da177e4
LT
3774 __u16 len;
3775 __u32 tsn;
3776
3777 if (!sctp_vtag_verify(chunk, asoc)) {
3778 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3779 SCTP_NULL());
3780 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3781 }
3782
3783 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3784 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3785 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3786 commands);
3787
3788 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3789 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3790 len = ntohs(chunk->chunk_hdr->length);
3791 len -= sizeof(struct sctp_chunkhdr);
3792 skb_pull(chunk->skb, len);
3793
3794 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
0dc47877 3795 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
1da177e4
LT
3796
3797 /* The TSN is too high--silently discard the chunk and count on it
3798 * getting retransmitted later.
3799 */
3800 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3801 goto gen_shutdown;
3802
9fcb95a1
WY
3803 /* Silently discard the chunk if stream-id is not valid */
3804 sctp_walk_fwdtsn(skip, chunk) {
3805 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3806 goto gen_shutdown;
3807 }
3808
1da177e4
LT
3809 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3810 if (len > sizeof(struct sctp_fwdtsn_hdr))
d808ad9a 3811 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
1da177e4 3812 SCTP_CHUNK(chunk));
d808ad9a 3813
1da177e4
LT
3814 /* Go a head and force a SACK, since we are shutting down. */
3815gen_shutdown:
3816 /* Implementor's Guide.
3817 *
3818 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3819 * respond to each received packet containing one or more DATA chunk(s)
3820 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3821 */
3822 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3823 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3825 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3826
d808ad9a 3827 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
3828}
3829
bbd0d598
VY
3830/*
3831 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3832 *
3833 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3834 * Identifier field. If this algorithm was not specified by the
3835 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3836 * during association setup, the AUTH chunk and all chunks after it MUST
3837 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3838 * defined in Section 4.1.
3839 *
3840 * If an endpoint with no shared key receives a Shared Key Identifier
3841 * other than 0, it MUST silently discard all authenticated chunks. If
3842 * the endpoint has at least one endpoint pair shared key for the peer,
3843 * it MUST use the key specified by the Shared Key Identifier if a
3844 * key has been configured for that Shared Key Identifier. If no
3845 * endpoint pair shared key has been configured for that Shared Key
3846 * Identifier, all authenticated chunks MUST be silently discarded.
3847 *
3848 * Verification Tag: 8.5 Verification Tag [Normal verification]
3849 *
3850 * The return value is the disposition of the chunk.
3851 */
3852static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3853 const struct sctp_association *asoc,
3854 const sctp_subtype_t type,
3855 struct sctp_chunk *chunk)
3856{
3857 struct sctp_authhdr *auth_hdr;
3858 struct sctp_hmac *hmac;
3859 unsigned int sig_len;
3860 __u16 key_id;
3861 __u8 *save_digest;
3862 __u8 *digest;
3863
3864 /* Pull in the auth header, so we can do some more verification */
3865 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3866 chunk->subh.auth_hdr = auth_hdr;
3867 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3868
3869 /* Make sure that we suport the HMAC algorithm from the auth
3870 * chunk.
3871 */
3872 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3873 return SCTP_IERROR_AUTH_BAD_HMAC;
3874
3875 /* Make sure that the provided shared key identifier has been
3876 * configured
3877 */
3878 key_id = ntohs(auth_hdr->shkey_id);
3879 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3880 return SCTP_IERROR_AUTH_BAD_KEYID;
3881
3882
3883 /* Make sure that the length of the signature matches what
3884 * we expect.
3885 */
3886 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3887 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3888 if (sig_len != hmac->hmac_len)
3889 return SCTP_IERROR_PROTO_VIOLATION;
3890
3891 /* Now that we've done validation checks, we can compute and
3892 * verify the hmac. The steps involved are:
3893 * 1. Save the digest from the chunk.
3894 * 2. Zero out the digest in the chunk.
3895 * 3. Compute the new digest
3896 * 4. Compare saved and new digests.
3897 */
3898 digest = auth_hdr->hmac;
3899 skb_pull(chunk->skb, sig_len);
3900
3901 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3902 if (!save_digest)
3903 goto nomem;
3904
3905 memset(digest, 0, sig_len);
3906
3907 sctp_auth_calculate_hmac(asoc, chunk->skb,
3908 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3909 GFP_ATOMIC);
3910
3911 /* Discard the packet if the digests do not match */
3912 if (memcmp(save_digest, digest, sig_len)) {
3913 kfree(save_digest);
3914 return SCTP_IERROR_BAD_SIG;
3915 }
3916
3917 kfree(save_digest);
3918 chunk->auth = 1;
3919
3920 return SCTP_IERROR_NO_ERROR;
3921nomem:
3922 return SCTP_IERROR_NOMEM;
3923}
3924
3925sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3926 const struct sctp_association *asoc,
3927 const sctp_subtype_t type,
3928 void *arg,
3929 sctp_cmd_seq_t *commands)
3930{
3931 struct sctp_authhdr *auth_hdr;
3932 struct sctp_chunk *chunk = arg;
3933 struct sctp_chunk *err_chunk;
3934 sctp_ierror_t error;
3935
d2f19fa1
WY
3936 /* Make sure that the peer has AUTH capable */
3937 if (!asoc->peer.auth_capable)
3938 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3939
bbd0d598
VY
3940 if (!sctp_vtag_verify(chunk, asoc)) {
3941 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3942 SCTP_NULL());
3943 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3944 }
3945
3946 /* Make sure that the AUTH chunk has valid length. */
3947 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3948 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3949 commands);
3950
3951 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3952 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3953 switch (error) {
3954 case SCTP_IERROR_AUTH_BAD_HMAC:
3955 /* Generate the ERROR chunk and discard the rest
3956 * of the packet
3957 */
3958 err_chunk = sctp_make_op_error(asoc, chunk,
3959 SCTP_ERROR_UNSUP_HMAC,
3960 &auth_hdr->hmac_id,
3961 sizeof(__u16));
3962 if (err_chunk) {
3963 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3964 SCTP_CHUNK(err_chunk));
3965 }
3966 /* Fall Through */
3967 case SCTP_IERROR_AUTH_BAD_KEYID:
3968 case SCTP_IERROR_BAD_SIG:
3969 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3970 break;
3971 case SCTP_IERROR_PROTO_VIOLATION:
3972 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3973 commands);
3974 break;
3975 case SCTP_IERROR_NOMEM:
3976 return SCTP_DISPOSITION_NOMEM;
3977 default:
3978 break;
3979 }
3980
65b07e5d
VY
3981 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3982 struct sctp_ulpevent *ev;
3983
3984 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3985 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3986
3987 if (!ev)
3988 return -ENOMEM;
3989
3990 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3991 SCTP_ULPEVENT(ev));
3992 }
3993
bbd0d598
VY
3994 return SCTP_DISPOSITION_CONSUME;
3995}
3996
1da177e4
LT
3997/*
3998 * Process an unknown chunk.
3999 *
4000 * Section: 3.2. Also, 2.1 in the implementor's guide.
4001 *
4002 * Chunk Types are encoded such that the highest-order two bits specify
4003 * the action that must be taken if the processing endpoint does not
4004 * recognize the Chunk Type.
4005 *
4006 * 00 - Stop processing this SCTP packet and discard it, do not process
4007 * any further chunks within it.
4008 *
4009 * 01 - Stop processing this SCTP packet and discard it, do not process
4010 * any further chunks within it, and report the unrecognized
4011 * chunk in an 'Unrecognized Chunk Type'.
4012 *
4013 * 10 - Skip this chunk and continue processing.
4014 *
4015 * 11 - Skip this chunk and continue processing, but report in an ERROR
4016 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4017 *
4018 * The return value is the disposition of the chunk.
4019 */
4020sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4021 const struct sctp_association *asoc,
4022 const sctp_subtype_t type,
4023 void *arg,
4024 sctp_cmd_seq_t *commands)
4025{
4026 struct sctp_chunk *unk_chunk = arg;
4027 struct sctp_chunk *err_chunk;
4028 sctp_chunkhdr_t *hdr;
4029
4030 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4031
4032 if (!sctp_vtag_verify(unk_chunk, asoc))
4033 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4034
4035 /* Make sure that the chunk has a valid length.
4036 * Since we don't know the chunk type, we use a general
4037 * chunkhdr structure to make a comparison.
4038 */
4039 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4040 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4041 commands);
4042
4043 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4044 case SCTP_CID_ACTION_DISCARD:
4045 /* Discard the packet. */
4046 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4047 break;
4048 case SCTP_CID_ACTION_DISCARD_ERR:
1da177e4
LT
4049 /* Generate an ERROR chunk as response. */
4050 hdr = unk_chunk->chunk_hdr;
4051 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4052 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4053 WORD_ROUND(ntohs(hdr->length)));
4054 if (err_chunk) {
4055 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4056 SCTP_CHUNK(err_chunk));
4057 }
2e3216cd
VY
4058
4059 /* Discard the packet. */
4060 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1da177e4
LT
4061 return SCTP_DISPOSITION_CONSUME;
4062 break;
4063 case SCTP_CID_ACTION_SKIP:
4064 /* Skip the chunk. */
4065 return SCTP_DISPOSITION_DISCARD;
4066 break;
4067 case SCTP_CID_ACTION_SKIP_ERR:
4068 /* Generate an ERROR chunk as response. */
4069 hdr = unk_chunk->chunk_hdr;
4070 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4071 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4072 WORD_ROUND(ntohs(hdr->length)));
4073 if (err_chunk) {
4074 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4075 SCTP_CHUNK(err_chunk));
4076 }
4077 /* Skip the chunk. */
4078 return SCTP_DISPOSITION_CONSUME;
4079 break;
4080 default:
4081 break;
4082 }
4083
4084 return SCTP_DISPOSITION_DISCARD;
4085}
4086
4087/*
4088 * Discard the chunk.
4089 *
4090 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4091 * [Too numerous to mention...]
4092 * Verification Tag: No verification needed.
4093 * Inputs
4094 * (endpoint, asoc, chunk)
4095 *
4096 * Outputs
4097 * (asoc, reply_msg, msg_up, timers, counters)
4098 *
4099 * The return value is the disposition of the chunk.
4100 */
4101sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4102 const struct sctp_association *asoc,
4103 const sctp_subtype_t type,
4104 void *arg,
4105 sctp_cmd_seq_t *commands)
4106{
ece25dfa
VY
4107 struct sctp_chunk *chunk = arg;
4108
4109 /* Make sure that the chunk has a valid length.
4110 * Since we don't know the chunk type, we use a general
4111 * chunkhdr structure to make a comparison.
4112 */
4113 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4114 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4115 commands);
4116
1da177e4
LT
4117 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4118 return SCTP_DISPOSITION_DISCARD;
4119}
4120
4121/*
4122 * Discard the whole packet.
4123 *
4124 * Section: 8.4 2)
4125 *
4126 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4127 * silently discard the OOTB packet and take no further action.
1da177e4
LT
4128 *
4129 * Verification Tag: No verification necessary
4130 *
4131 * Inputs
4132 * (endpoint, asoc, chunk)
4133 *
4134 * Outputs
4135 * (asoc, reply_msg, msg_up, timers, counters)
4136 *
4137 * The return value is the disposition of the chunk.
4138 */
4139sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4140 const struct sctp_association *asoc,
4141 const sctp_subtype_t type,
4142 void *arg,
4143 sctp_cmd_seq_t *commands)
4144{
ac0b0462 4145 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
1da177e4
LT
4146 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4147
4148 return SCTP_DISPOSITION_CONSUME;
4149}
4150
4151
4152/*
4153 * The other end is violating protocol.
4154 *
4155 * Section: Not specified
4156 * Verification Tag: Not specified
4157 * Inputs
4158 * (endpoint, asoc, chunk)
4159 *
4160 * Outputs
4161 * (asoc, reply_msg, msg_up, timers, counters)
4162 *
4163 * We simply tag the chunk as a violation. The state machine will log
4164 * the violation and continue.
4165 */
4166sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4167 const struct sctp_association *asoc,
4168 const sctp_subtype_t type,
4169 void *arg,
4170 sctp_cmd_seq_t *commands)
4171{
ece25dfa
VY
4172 struct sctp_chunk *chunk = arg;
4173
4174 /* Make sure that the chunk has a valid length. */
4175 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4176 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4177 commands);
4178
1da177e4
LT
4179 return SCTP_DISPOSITION_VIOLATION;
4180}
4181
1da177e4 4182/*
aecedeab 4183 * Common function to handle a protocol violation.
1da177e4 4184 */
aecedeab 4185static sctp_disposition_t sctp_sf_abort_violation(
ece25dfa 4186 const struct sctp_endpoint *ep,
1da177e4 4187 const struct sctp_association *asoc,
1da177e4 4188 void *arg,
aecedeab
WY
4189 sctp_cmd_seq_t *commands,
4190 const __u8 *payload,
4191 const size_t paylen)
1da177e4 4192{
ece25dfa 4193 struct sctp_packet *packet = NULL;
1da177e4
LT
4194 struct sctp_chunk *chunk = arg;
4195 struct sctp_chunk *abort = NULL;
1da177e4 4196
bbd0d598
VY
4197 /* SCTP-AUTH, Section 6.3:
4198 * It should be noted that if the receiver wants to tear
4199 * down an association in an authenticated way only, the
4200 * handling of malformed packets should not result in
4201 * tearing down the association.
4202 *
4203 * This means that if we only want to abort associations
4204 * in an authenticated way (i.e AUTH+ABORT), then we
ac461a03 4205 * can't destroy this association just becuase the packet
bbd0d598
VY
4206 * was malformed.
4207 */
4208 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4209 goto discard;
4210
9abed245
JJ
4211 /* Make the abort chunk. */
4212 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4213 if (!abort)
4214 goto nomem;
4215
ece25dfa 4216 if (asoc) {
f4ad85ca
GJ
4217 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4218 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4219 !asoc->peer.i.init_tag) {
4220 sctp_initack_chunk_t *initack;
4221
4222 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4223 if (!sctp_chunk_length_valid(chunk,
4224 sizeof(sctp_initack_chunk_t)))
4225 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4226 else {
4227 unsigned int inittag;
4228
4229 inittag = ntohl(initack->init_hdr.init_tag);
4230 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4231 SCTP_U32(inittag));
4232 }
4233 }
4234
ece25dfa
VY
4235 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4236 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1da177e4 4237
ece25dfa
VY
4238 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4239 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4240 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4241 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4242 SCTP_ERROR(ECONNREFUSED));
4243 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4244 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4245 } else {
4246 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4247 SCTP_ERROR(ECONNABORTED));
4248 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4249 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4250 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4251 }
1da177e4 4252 } else {
ece25dfa
VY
4253 packet = sctp_ootb_pkt_new(asoc, chunk);
4254
4255 if (!packet)
4256 goto nomem_pkt;
4257
4258 if (sctp_test_T_bit(abort))
4259 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4260
4261 abort->skb->sk = ep->base.sk;
4262
4263 sctp_packet_append_chunk(packet, abort);
4264
4265 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4266 SCTP_PACKET(packet));
4267
4268 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1da177e4
LT
4269 }
4270
1da177e4 4271 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
d808ad9a 4272
56eb82bb
VY
4273discard:
4274 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
1da177e4
LT
4275 return SCTP_DISPOSITION_ABORT;
4276
ece25dfa
VY
4277nomem_pkt:
4278 sctp_chunk_free(abort);
1da177e4
LT
4279nomem:
4280 return SCTP_DISPOSITION_NOMEM;
4281}
4282
aecedeab
WY
4283/*
4284 * Handle a protocol violation when the chunk length is invalid.
025dfdaf 4285 * "Invalid" length is identified as smaller than the minimal length a
aecedeab 4286 * given chunk can be. For example, a SACK chunk has invalid length
025dfdaf 4287 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
aecedeab
WY
4288 *
4289 * We inform the other end by sending an ABORT with a Protocol Violation
4290 * error code.
4291 *
4292 * Section: Not specified
4293 * Verification Tag: Nothing to do
4294 * Inputs
4295 * (endpoint, asoc, chunk)
4296 *
4297 * Outputs
4298 * (reply_msg, msg_up, counters)
4299 *
4300 * Generate an ABORT chunk and terminate the association.
4301 */
4302static sctp_disposition_t sctp_sf_violation_chunklen(
4303 const struct sctp_endpoint *ep,
4304 const struct sctp_association *asoc,
4305 const sctp_subtype_t type,
4306 void *arg,
4307 sctp_cmd_seq_t *commands)
4308{
2444844c 4309 static const char err_str[]="The following chunk had invalid length:";
aecedeab 4310
ece25dfa 4311 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
aecedeab
WY
4312 sizeof(err_str));
4313}
4314
6f4c618d
WY
4315/*
4316 * Handle a protocol violation when the parameter length is invalid.
025dfdaf 4317 * "Invalid" length is identified as smaller than the minimal length a
6f4c618d
WY
4318 * given parameter can be.
4319 */
4320static sctp_disposition_t sctp_sf_violation_paramlen(
4321 const struct sctp_endpoint *ep,
4322 const struct sctp_association *asoc,
4323 const sctp_subtype_t type,
ba016670
WY
4324 void *arg, void *ext,
4325 sctp_cmd_seq_t *commands)
4326{
4327 struct sctp_chunk *chunk = arg;
4328 struct sctp_paramhdr *param = ext;
4329 struct sctp_chunk *abort = NULL;
6f4c618d 4330
ba016670
WY
4331 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4332 goto discard;
4333
4334 /* Make the abort chunk. */
4335 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4336 if (!abort)
4337 goto nomem;
4338
4339 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4340 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4341
4342 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4343 SCTP_ERROR(ECONNABORTED));
4344 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4345 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4346 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
56eb82bb 4347 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
ba016670
WY
4348
4349discard:
4350 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
ba016670
WY
4351 return SCTP_DISPOSITION_ABORT;
4352nomem:
4353 return SCTP_DISPOSITION_NOMEM;
6f4c618d
WY
4354}
4355
aecedeab
WY
4356/* Handle a protocol violation when the peer trying to advance the
4357 * cumulative tsn ack to a point beyond the max tsn currently sent.
4358 *
4359 * We inform the other end by sending an ABORT with a Protocol Violation
4360 * error code.
4361 */
4362static sctp_disposition_t sctp_sf_violation_ctsn(
4363 const struct sctp_endpoint *ep,
4364 const struct sctp_association *asoc,
4365 const sctp_subtype_t type,
4366 void *arg,
4367 sctp_cmd_seq_t *commands)
4368{
2444844c 4369 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
aecedeab 4370
ece25dfa 4371 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
aecedeab
WY
4372 sizeof(err_str));
4373}
4374
ece25dfa
VY
4375/* Handle protocol violation of an invalid chunk bundling. For example,
4376 * when we have an association and we recieve bundled INIT-ACK, or
4377 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4378 * statement from the specs. Additinally, there might be an attacker
4379 * on the path and we may not want to continue this communication.
4380 */
4381static sctp_disposition_t sctp_sf_violation_chunk(
4382 const struct sctp_endpoint *ep,
4383 const struct sctp_association *asoc,
4384 const sctp_subtype_t type,
4385 void *arg,
4386 sctp_cmd_seq_t *commands)
4387{
2444844c 4388 static const char err_str[]="The following chunk violates protocol:";
ece25dfa
VY
4389
4390 if (!asoc)
4391 return sctp_sf_violation(ep, asoc, type, arg, commands);
4392
4393 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4394 sizeof(err_str));
4395}
1da177e4
LT
4396/***************************************************************************
4397 * These are the state functions for handling primitive (Section 10) events.
4398 ***************************************************************************/
4399/*
4400 * sctp_sf_do_prm_asoc
4401 *
4402 * Section: 10.1 ULP-to-SCTP
4403 * B) Associate
4404 *
4405 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4406 * outbound stream count)
4407 * -> association id [,destination transport addr list] [,outbound stream
4408 * count]
4409 *
4410 * This primitive allows the upper layer to initiate an association to a
4411 * specific peer endpoint.
4412 *
4413 * The peer endpoint shall be specified by one of the transport addresses
4414 * which defines the endpoint (see Section 1.4). If the local SCTP
4415 * instance has not been initialized, the ASSOCIATE is considered an
4416 * error.
4417 * [This is not relevant for the kernel implementation since we do all
4418 * initialization at boot time. It we hadn't initialized we wouldn't
4419 * get anywhere near this code.]
4420 *
4421 * An association id, which is a local handle to the SCTP association,
4422 * will be returned on successful establishment of the association. If
4423 * SCTP is not able to open an SCTP association with the peer endpoint,
4424 * an error is returned.
4425 * [In the kernel implementation, the struct sctp_association needs to
4426 * be created BEFORE causing this primitive to run.]
4427 *
4428 * Other association parameters may be returned, including the
4429 * complete destination transport addresses of the peer as well as the
4430 * outbound stream count of the local endpoint. One of the transport
4431 * address from the returned destination addresses will be selected by
4432 * the local endpoint as default primary path for sending SCTP packets
4433 * to this peer. The returned "destination transport addr list" can
4434 * be used by the ULP to change the default primary path or to force
4435 * sending a packet to a specific transport address. [All of this
4436 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4437 * function.]
4438 *
4439 * Mandatory attributes:
4440 *
4441 * o local SCTP instance name - obtained from the INITIALIZE operation.
4442 * [This is the argument asoc.]
4443 * o destination transport addr - specified as one of the transport
4444 * addresses of the peer endpoint with which the association is to be
4445 * established.
4446 * [This is asoc->peer.active_path.]
4447 * o outbound stream count - the number of outbound streams the ULP
4448 * would like to open towards this peer endpoint.
4449 * [BUG: This is not currently implemented.]
4450 * Optional attributes:
4451 *
4452 * None.
4453 *
4454 * The return value is a disposition.
4455 */
4456sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4457 const struct sctp_association *asoc,
4458 const sctp_subtype_t type,
4459 void *arg,
4460 sctp_cmd_seq_t *commands)
4461{
4462 struct sctp_chunk *repl;
ab38fb04 4463 struct sctp_association* my_asoc;
1da177e4
LT
4464
4465 /* The comment below says that we enter COOKIE-WAIT AFTER
4466 * sending the INIT, but that doesn't actually work in our
4467 * implementation...
4468 */
4469 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4470 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4471
4472 /* RFC 2960 5.1 Normal Establishment of an Association
4473 *
4474 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4475 * must provide its Verification Tag (Tag_A) in the Initiate
4476 * Tag field. Tag_A SHOULD be a random number in the range of
4477 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4478 */
4479
4480 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4481 if (!repl)
4482 goto nomem;
4483
4484 /* Cast away the const modifier, as we want to just
4485 * rerun it through as a sideffect.
4486 */
ab38fb04
VY
4487 my_asoc = (struct sctp_association *)asoc;
4488 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
1da177e4 4489
3f7a87d2
FF
4490 /* Choose transport for INIT. */
4491 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4492 SCTP_CHUNK(repl));
4493
1da177e4
LT
4494 /* After sending the INIT, "A" starts the T1-init timer and
4495 * enters the COOKIE-WAIT state.
4496 */
4497 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4498 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4499 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4500 return SCTP_DISPOSITION_CONSUME;
4501
4502nomem:
4503 return SCTP_DISPOSITION_NOMEM;
4504}
4505
4506/*
4507 * Process the SEND primitive.
4508 *
4509 * Section: 10.1 ULP-to-SCTP
4510 * E) Send
4511 *
4512 * Format: SEND(association id, buffer address, byte count [,context]
4513 * [,stream id] [,life time] [,destination transport address]
4514 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4515 * -> result
4516 *
4517 * This is the main method to send user data via SCTP.
4518 *
4519 * Mandatory attributes:
4520 *
4521 * o association id - local handle to the SCTP association
4522 *
4523 * o buffer address - the location where the user message to be
4524 * transmitted is stored;
4525 *
4526 * o byte count - The size of the user data in number of bytes;
4527 *
4528 * Optional attributes:
4529 *
4530 * o context - an optional 32 bit integer that will be carried in the
4531 * sending failure notification to the ULP if the transportation of
4532 * this User Message fails.
4533 *
4534 * o stream id - to indicate which stream to send the data on. If not
4535 * specified, stream 0 will be used.
4536 *
4537 * o life time - specifies the life time of the user data. The user data
4538 * will not be sent by SCTP after the life time expires. This
4539 * parameter can be used to avoid efforts to transmit stale
4540 * user messages. SCTP notifies the ULP if the data cannot be
4541 * initiated to transport (i.e. sent to the destination via SCTP's
4542 * send primitive) within the life time variable. However, the
4543 * user data will be transmitted if SCTP has attempted to transmit a
4544 * chunk before the life time expired.
4545 *
4546 * o destination transport address - specified as one of the destination
4547 * transport addresses of the peer endpoint to which this packet
4548 * should be sent. Whenever possible, SCTP should use this destination
4549 * transport address for sending the packets, instead of the current
4550 * primary path.
4551 *
4552 * o unorder flag - this flag, if present, indicates that the user
4553 * would like the data delivered in an unordered fashion to the peer
4554 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4555 * message).
4556 *
4557 * o no-bundle flag - instructs SCTP not to bundle this user data with
4558 * other outbound DATA chunks. SCTP MAY still bundle even when
4559 * this flag is present, when faced with network congestion.
4560 *
4561 * o payload protocol-id - A 32 bit unsigned integer that is to be
4562 * passed to the peer indicating the type of payload protocol data
4563 * being transmitted. This value is passed as opaque data by SCTP.
4564 *
4565 * The return value is the disposition.
4566 */
4567sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4568 const struct sctp_association *asoc,
4569 const sctp_subtype_t type,
4570 void *arg,
4571 sctp_cmd_seq_t *commands)
4572{
9c5c62be 4573 struct sctp_datamsg *msg = arg;
1da177e4 4574
9c5c62be 4575 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
1da177e4
LT
4576 return SCTP_DISPOSITION_CONSUME;
4577}
4578
4579/*
4580 * Process the SHUTDOWN primitive.
4581 *
4582 * Section: 10.1:
4583 * C) Shutdown
4584 *
4585 * Format: SHUTDOWN(association id)
4586 * -> result
4587 *
4588 * Gracefully closes an association. Any locally queued user data
4589 * will be delivered to the peer. The association will be terminated only
4590 * after the peer acknowledges all the SCTP packets sent. A success code
4591 * will be returned on successful termination of the association. If
4592 * attempting to terminate the association results in a failure, an error
4593 * code shall be returned.
4594 *
4595 * Mandatory attributes:
4596 *
4597 * o association id - local handle to the SCTP association
4598 *
4599 * Optional attributes:
4600 *
4601 * None.
4602 *
4603 * The return value is the disposition.
4604 */
4605sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4606 const struct sctp_endpoint *ep,
4607 const struct sctp_association *asoc,
4608 const sctp_subtype_t type,
4609 void *arg,
4610 sctp_cmd_seq_t *commands)
4611{
4612 int disposition;
4613
4614 /* From 9.2 Shutdown of an Association
4615 * Upon receipt of the SHUTDOWN primitive from its upper
4616 * layer, the endpoint enters SHUTDOWN-PENDING state and
4617 * remains there until all outstanding data has been
4618 * acknowledged by its peer. The endpoint accepts no new data
4619 * from its upper layer, but retransmits data to the far end
4620 * if necessary to fill gaps.
4621 */
4622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4623 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4624
1da177e4
LT
4625 disposition = SCTP_DISPOSITION_CONSUME;
4626 if (sctp_outq_is_empty(&asoc->outqueue)) {
4627 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4628 arg, commands);
4629 }
4630 return disposition;
4631}
4632
4633/*
4634 * Process the ABORT primitive.
4635 *
4636 * Section: 10.1:
4637 * C) Abort
4638 *
4639 * Format: Abort(association id [, cause code])
4640 * -> result
4641 *
4642 * Ungracefully closes an association. Any locally queued user data
4643 * will be discarded and an ABORT chunk is sent to the peer. A success code
4644 * will be returned on successful abortion of the association. If
4645 * attempting to abort the association results in a failure, an error
4646 * code shall be returned.
4647 *
4648 * Mandatory attributes:
4649 *
4650 * o association id - local handle to the SCTP association
4651 *
4652 * Optional attributes:
4653 *
4654 * o cause code - reason of the abort to be passed to the peer
4655 *
4656 * None.
4657 *
4658 * The return value is the disposition.
4659 */
4660sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4661 const struct sctp_endpoint *ep,
4662 const struct sctp_association *asoc,
4663 const sctp_subtype_t type,
4664 void *arg,
4665 sctp_cmd_seq_t *commands)
4666{
4667 /* From 9.1 Abort of an Association
4668 * Upon receipt of the ABORT primitive from its upper
4669 * layer, the endpoint enters CLOSED state and
4670 * discard all outstanding data has been
4671 * acknowledged by its peer. The endpoint accepts no new data
4672 * from its upper layer, but retransmits data to the far end
4673 * if necessary to fill gaps.
4674 */
c164a9ba 4675 struct sctp_chunk *abort = arg;
1da177e4
LT
4676 sctp_disposition_t retval;
4677
4678 retval = SCTP_DISPOSITION_CONSUME;
4679
c164a9ba 4680 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
1da177e4
LT
4681
4682 /* Even if we can't send the ABORT due to low memory delete the
4683 * TCB. This is a departure from our typical NOMEM handling.
4684 */
4685
8de8c873
SS
4686 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4687 SCTP_ERROR(ECONNABORTED));
1da177e4
LT
4688 /* Delete the established association. */
4689 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 4690 SCTP_PERR(SCTP_ERROR_USER_ABORT));
1da177e4
LT
4691
4692 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4693 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4694
4695 return retval;
4696}
4697
4698/* We tried an illegal operation on an association which is closed. */
4699sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4700 const struct sctp_association *asoc,
4701 const sctp_subtype_t type,
4702 void *arg,
4703 sctp_cmd_seq_t *commands)
4704{
4705 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4706 return SCTP_DISPOSITION_CONSUME;
4707}
4708
4709/* We tried an illegal operation on an association which is shutting
4710 * down.
4711 */
4712sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4713 const struct sctp_association *asoc,
4714 const sctp_subtype_t type,
4715 void *arg,
4716 sctp_cmd_seq_t *commands)
4717{
4718 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4719 SCTP_ERROR(-ESHUTDOWN));
4720 return SCTP_DISPOSITION_CONSUME;
4721}
4722
4723/*
4724 * sctp_cookie_wait_prm_shutdown
4725 *
4726 * Section: 4 Note: 2
4727 * Verification Tag:
4728 * Inputs
4729 * (endpoint, asoc)
4730 *
4731 * The RFC does not explicitly address this issue, but is the route through the
4732 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4733 *
4734 * Outputs
4735 * (timers)
4736 */
4737sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4738 const struct sctp_endpoint *ep,
4739 const struct sctp_association *asoc,
4740 const sctp_subtype_t type,
4741 void *arg,
4742 sctp_cmd_seq_t *commands)
4743{
4744 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4745 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4746
4747 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4748 SCTP_STATE(SCTP_STATE_CLOSED));
4749
4750 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4751
4752 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4753
4754 return SCTP_DISPOSITION_DELETE_TCB;
4755}
4756
4757/*
4758 * sctp_cookie_echoed_prm_shutdown
4759 *
4760 * Section: 4 Note: 2
4761 * Verification Tag:
4762 * Inputs
4763 * (endpoint, asoc)
4764 *
4765 * The RFC does not explcitly address this issue, but is the route through the
4766 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4767 *
4768 * Outputs
4769 * (timers)
4770 */
4771sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4772 const struct sctp_endpoint *ep,
4773 const struct sctp_association *asoc,
4774 const sctp_subtype_t type,
4775 void *arg, sctp_cmd_seq_t *commands)
4776{
4777 /* There is a single T1 timer, so we should be able to use
4778 * common function with the COOKIE-WAIT state.
4779 */
4780 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4781}
4782
4783/*
4784 * sctp_sf_cookie_wait_prm_abort
4785 *
4786 * Section: 4 Note: 2
4787 * Verification Tag:
4788 * Inputs
4789 * (endpoint, asoc)
4790 *
4791 * The RFC does not explicitly address this issue, but is the route through the
4792 * state table when someone issues an abort while in COOKIE_WAIT state.
4793 *
4794 * Outputs
4795 * (timers)
4796 */
4797sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4798 const struct sctp_endpoint *ep,
4799 const struct sctp_association *asoc,
4800 const sctp_subtype_t type,
4801 void *arg,
4802 sctp_cmd_seq_t *commands)
4803{
c164a9ba 4804 struct sctp_chunk *abort = arg;
1da177e4
LT
4805 sctp_disposition_t retval;
4806
4807 /* Stop T1-init timer */
4808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4809 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4810 retval = SCTP_DISPOSITION_CONSUME;
4811
c164a9ba 4812 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
1da177e4
LT
4813
4814 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4815 SCTP_STATE(SCTP_STATE_CLOSED));
4816
4817 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4818
4819 /* Even if we can't send the ABORT due to low memory delete the
4820 * TCB. This is a departure from our typical NOMEM handling.
4821 */
4822
8de8c873
SS
4823 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4824 SCTP_ERROR(ECONNREFUSED));
1da177e4
LT
4825 /* Delete the established association. */
4826 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
dc251b2b 4827 SCTP_PERR(SCTP_ERROR_USER_ABORT));
1da177e4
LT
4828
4829 return retval;
4830}
4831
4832/*
4833 * sctp_sf_cookie_echoed_prm_abort
4834 *
4835 * Section: 4 Note: 3
4836 * Verification Tag:
4837 * Inputs
4838 * (endpoint, asoc)
4839 *
4840 * The RFC does not explcitly address this issue, but is the route through the
4841 * state table when someone issues an abort while in COOKIE_ECHOED state.
4842 *
4843 * Outputs
4844 * (timers)
4845 */
4846sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4847 const struct sctp_endpoint *ep,
4848 const struct sctp_association *asoc,
4849 const sctp_subtype_t type,
4850 void *arg,
4851 sctp_cmd_seq_t *commands)
4852{
4853 /* There is a single T1 timer, so we should be able to use
4854 * common function with the COOKIE-WAIT state.
4855 */
4856 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4857}
4858
4859/*
4860 * sctp_sf_shutdown_pending_prm_abort
4861 *
4862 * Inputs
4863 * (endpoint, asoc)
4864 *
4865 * The RFC does not explicitly address this issue, but is the route through the
4866 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4867 *
4868 * Outputs
4869 * (timers)
4870 */
4871sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4872 const struct sctp_endpoint *ep,
4873 const struct sctp_association *asoc,
4874 const sctp_subtype_t type,
4875 void *arg,
4876 sctp_cmd_seq_t *commands)
4877{
4878 /* Stop the T5-shutdown guard timer. */
4879 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4880 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4881
4882 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4883}
4884
4885/*
4886 * sctp_sf_shutdown_sent_prm_abort
4887 *
4888 * Inputs
4889 * (endpoint, asoc)
4890 *
4891 * The RFC does not explicitly address this issue, but is the route through the
4892 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4893 *
4894 * Outputs
4895 * (timers)
4896 */
4897sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4898 const struct sctp_endpoint *ep,
4899 const struct sctp_association *asoc,
4900 const sctp_subtype_t type,
4901 void *arg,
4902 sctp_cmd_seq_t *commands)
4903{
4904 /* Stop the T2-shutdown timer. */
4905 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4906 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4907
4908 /* Stop the T5-shutdown guard timer. */
4909 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4910 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4911
4912 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4913}
4914
4915/*
4916 * sctp_sf_cookie_echoed_prm_abort
4917 *
4918 * Inputs
4919 * (endpoint, asoc)
4920 *
4921 * The RFC does not explcitly address this issue, but is the route through the
4922 * state table when someone issues an abort while in COOKIE_ECHOED state.
4923 *
4924 * Outputs
4925 * (timers)
4926 */
4927sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4928 const struct sctp_endpoint *ep,
4929 const struct sctp_association *asoc,
4930 const sctp_subtype_t type,
4931 void *arg,
4932 sctp_cmd_seq_t *commands)
4933{
4934 /* The same T2 timer, so we should be able to use
4935 * common function with the SHUTDOWN-SENT state.
4936 */
4937 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4938}
4939
4940/*
4941 * Process the REQUESTHEARTBEAT primitive
4942 *
4943 * 10.1 ULP-to-SCTP
4944 * J) Request Heartbeat
4945 *
4946 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4947 *
4948 * -> result
4949 *
4950 * Instructs the local endpoint to perform a HeartBeat on the specified
4951 * destination transport address of the given association. The returned
4952 * result should indicate whether the transmission of the HEARTBEAT
4953 * chunk to the destination address is successful.
4954 *
4955 * Mandatory attributes:
4956 *
4957 * o association id - local handle to the SCTP association
4958 *
4959 * o destination transport address - the transport address of the
4960 * association on which a heartbeat should be issued.
4961 */
4962sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4963 const struct sctp_endpoint *ep,
4964 const struct sctp_association *asoc,
4965 const sctp_subtype_t type,
4966 void *arg,
4967 sctp_cmd_seq_t *commands)
4968{
fb78525a
VY
4969 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4970 (struct sctp_transport *)arg, commands))
4971 return SCTP_DISPOSITION_NOMEM;
4972
4973 /*
4974 * RFC 2960 (bis), section 8.3
4975 *
4976 * D) Request an on-demand HEARTBEAT on a specific destination
4977 * transport address of a given association.
4978 *
4979 * The endpoint should increment the respective error counter of
4980 * the destination transport address each time a HEARTBEAT is sent
4981 * to that address and not acknowledged within one RTO.
4982 *
4983 */
7e99013a 4984 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
fb78525a
VY
4985 SCTP_TRANSPORT(arg));
4986 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
4987}
4988
4989/*
4990 * ADDIP Section 4.1 ASCONF Chunk Procedures
4991 * When an endpoint has an ASCONF signaled change to be sent to the
4992 * remote endpoint it should do A1 to A9
4993 */
4994sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4995 const struct sctp_association *asoc,
4996 const sctp_subtype_t type,
4997 void *arg,
4998 sctp_cmd_seq_t *commands)
4999{
5000 struct sctp_chunk *chunk = arg;
5001
5002 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5003 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5004 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5005 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5006 return SCTP_DISPOSITION_CONSUME;
5007}
5008
5009/*
5010 * Ignore the primitive event
5011 *
5012 * The return value is the disposition of the primitive.
5013 */
5014sctp_disposition_t sctp_sf_ignore_primitive(
5015 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)
5020{
5021 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5022 return SCTP_DISPOSITION_DISCARD;
5023}
5024
5025/***************************************************************************
5026 * These are the state functions for the OTHER events.
5027 ***************************************************************************/
5028
5029/*
5030 * Start the shutdown negotiation.
5031 *
5032 * From Section 9.2:
5033 * Once all its outstanding data has been acknowledged, the endpoint
5034 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5035 * TSN Ack field the last sequential TSN it has received from the peer.
5036 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5037 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5038 * with the updated last sequential TSN received from its peer.
5039 *
5040 * The return value is the disposition.
5041 */
5042sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5043 const struct sctp_endpoint *ep,
5044 const struct sctp_association *asoc,
5045 const sctp_subtype_t type,
5046 void *arg,
5047 sctp_cmd_seq_t *commands)
5048{
5049 struct sctp_chunk *reply;
5050
5051 /* Once all its outstanding data has been acknowledged, the
5052 * endpoint shall send a SHUTDOWN chunk to its peer including
5053 * in the Cumulative TSN Ack field the last sequential TSN it
5054 * has received from the peer.
5055 */
5056 reply = sctp_make_shutdown(asoc, NULL);
5057 if (!reply)
5058 goto nomem;
5059
5060 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5061 * T2-shutdown timer.
5062 */
5063 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5064
5065 /* It shall then start the T2-shutdown timer */
5066 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5067 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5068
536428a9
WY
5069 /* RFC 4960 Section 9.2
5070 * The sender of the SHUTDOWN MAY also start an overall guard timer
5071 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5072 */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5075
1da177e4
LT
5076 if (asoc->autoclose)
5077 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5078 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5079
5080 /* and enter the SHUTDOWN-SENT state. */
5081 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5082 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5083
5084 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5085 *
5086 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
d808ad9a 5087 * or SHUTDOWN-ACK.
1da177e4
LT
5088 */
5089 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5090
5091 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5092
5093 return SCTP_DISPOSITION_CONSUME;
5094
5095nomem:
5096 return SCTP_DISPOSITION_NOMEM;
5097}
5098
5099/*
5100 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5101 *
5102 * From Section 9.2:
5103 *
5104 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5105 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5106 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5107 * endpoint must re-send the SHUTDOWN ACK.
5108 *
5109 * The return value is the disposition.
5110 */
5111sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5112 const struct sctp_endpoint *ep,
5113 const struct sctp_association *asoc,
5114 const sctp_subtype_t type,
5115 void *arg,
5116 sctp_cmd_seq_t *commands)
5117{
5118 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5119 struct sctp_chunk *reply;
5120
5121 /* There are 2 ways of getting here:
5122 * 1) called in response to a SHUTDOWN chunk
5123 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5124 *
5125 * For the case (2), the arg parameter is set to NULL. We need
5126 * to check that we have a chunk before accessing it's fields.
5127 */
5128 if (chunk) {
5129 if (!sctp_vtag_verify(chunk, asoc))
5130 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5131
5132 /* Make sure that the SHUTDOWN chunk has a valid length. */
5133 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5134 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5135 commands);
5136 }
5137
5138 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5139 * shall send a SHUTDOWN ACK ...
5140 */
5141 reply = sctp_make_shutdown_ack(asoc, chunk);
5142 if (!reply)
5143 goto nomem;
5144
5145 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5146 * the T2-shutdown timer.
5147 */
5148 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5149
5150 /* and start/restart a T2-shutdown timer of its own, */
5151 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5152 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5153
5154 if (asoc->autoclose)
5155 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5156 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5157
5158 /* Enter the SHUTDOWN-ACK-SENT state. */
5159 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5160 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5161
5162 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5163 *
5164 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
d808ad9a 5165 * or SHUTDOWN-ACK.
1da177e4
LT
5166 */
5167 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5168
5169 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5170
5171 return SCTP_DISPOSITION_CONSUME;
5172
5173nomem:
5174 return SCTP_DISPOSITION_NOMEM;
5175}
5176
5177/*
5178 * Ignore the event defined as other
5179 *
5180 * The return value is the disposition of the event.
5181 */
5182sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5183 const struct sctp_association *asoc,
5184 const sctp_subtype_t type,
5185 void *arg,
5186 sctp_cmd_seq_t *commands)
5187{
5188 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5189 return SCTP_DISPOSITION_DISCARD;
5190}
5191
5192/************************************************************
5193 * These are the state functions for handling timeout events.
5194 ************************************************************/
5195
5196/*
5197 * RTX Timeout
5198 *
5199 * Section: 6.3.3 Handle T3-rtx Expiration
5200 *
5201 * Whenever the retransmission timer T3-rtx expires for a destination
5202 * address, do the following:
5203 * [See below]
5204 *
5205 * The return value is the disposition of the chunk.
5206 */
5207sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5208 const struct sctp_association *asoc,
5209 const sctp_subtype_t type,
5210 void *arg,
5211 sctp_cmd_seq_t *commands)
5212{
5213 struct sctp_transport *transport = arg;
5214
ac0b0462
SS
5215 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5216
1da177e4 5217 if (asoc->overall_error_count >= asoc->max_retrans) {
8de8c873
SS
5218 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5219 SCTP_ERROR(ETIMEDOUT));
1da177e4
LT
5220 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5221 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 5222 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4
LT
5223 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5224 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5225 return SCTP_DISPOSITION_DELETE_TCB;
5226 }
5227
5228 /* E1) For the destination address for which the timer
5229 * expires, adjust its ssthresh with rules defined in Section
5230 * 7.2.3 and set the cwnd <- MTU.
5231 */
5232
5233 /* E2) For the destination address for which the timer
5234 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5235 * maximum value discussed in rule C7 above (RTO.max) may be
5236 * used to provide an upper bound to this doubling operation.
5237 */
5238
5239 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5240 * outstanding DATA chunks for the address for which the
5241 * T3-rtx has expired will fit into a single packet, subject
5242 * to the MTU constraint for the path corresponding to the
5243 * destination transport address to which the retransmission
5244 * is being sent (this may be different from the address for
5245 * which the timer expires [see Section 6.4]). Call this
5246 * value K. Bundle and retransmit those K DATA chunks in a
5247 * single packet to the destination endpoint.
5248 *
5249 * Note: Any DATA chunks that were sent to the address for
5250 * which the T3-rtx timer expired but did not fit in one MTU
5251 * (rule E3 above), should be marked for retransmission and
5252 * sent as soon as cwnd allows (normally when a SACK arrives).
5253 */
5254
1da177e4
LT
5255 /* Do some failure management (Section 8.2). */
5256 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5257
1845a579
VY
5258 /* NB: Rules E4 and F1 are implicit in R1. */
5259 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5260
1da177e4
LT
5261 return SCTP_DISPOSITION_CONSUME;
5262}
5263
5264/*
5265 * Generate delayed SACK on timeout
5266 *
5267 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5268 *
5269 * The guidelines on delayed acknowledgement algorithm specified in
5270 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5271 * acknowledgement SHOULD be generated for at least every second packet
5272 * (not every second DATA chunk) received, and SHOULD be generated
5273 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5274 * some situations it may be beneficial for an SCTP transmitter to be
5275 * more conservative than the algorithms detailed in this document
5276 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5277 * the following algorithms allow.
5278 */
5279sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5280 const struct sctp_association *asoc,
5281 const sctp_subtype_t type,
5282 void *arg,
5283 sctp_cmd_seq_t *commands)
5284{
ac0b0462 5285 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
1da177e4
LT
5286 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5287 return SCTP_DISPOSITION_CONSUME;
5288}
5289
5290/*
3f7a87d2 5291 * sctp_sf_t1_init_timer_expire
1da177e4
LT
5292 *
5293 * Section: 4 Note: 2
5294 * Verification Tag:
5295 * Inputs
5296 * (endpoint, asoc)
5297 *
5298 * RFC 2960 Section 4 Notes
5299 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5300 * and re-start the T1-init timer without changing state. This MUST
5301 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5302 * endpoint MUST abort the initialization process and report the
5303 * error to SCTP user.
5304 *
3f7a87d2
FF
5305 * Outputs
5306 * (timers, events)
5307 *
5308 */
5309sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5310 const struct sctp_association *asoc,
5311 const sctp_subtype_t type,
5312 void *arg,
5313 sctp_cmd_seq_t *commands)
5314{
5315 struct sctp_chunk *repl = NULL;
5316 struct sctp_bind_addr *bp;
5317 int attempts = asoc->init_err_counter + 1;
5318
5319 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
ac0b0462 5320 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
3f7a87d2 5321
81845c21 5322 if (attempts <= asoc->max_init_attempts) {
3f7a87d2
FF
5323 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5324 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5325 if (!repl)
5326 return SCTP_DISPOSITION_NOMEM;
5327
5328 /* Choose transport for INIT. */
5329 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5330 SCTP_CHUNK(repl));
5331
5332 /* Issue a sideeffect to do the needed accounting. */
5333 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5334 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5335
5336 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5337 } else {
5338 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5339 " max_init_attempts: %d\n",
5340 attempts, asoc->max_init_attempts);
8de8c873
SS
5341 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5342 SCTP_ERROR(ETIMEDOUT));
3f7a87d2 5343 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
dc251b2b 5344 SCTP_PERR(SCTP_ERROR_NO_ERROR));
3f7a87d2
FF
5345 return SCTP_DISPOSITION_DELETE_TCB;
5346 }
5347
5348 return SCTP_DISPOSITION_CONSUME;
5349}
5350
5351/*
5352 * sctp_sf_t1_cookie_timer_expire
5353 *
5354 * Section: 4 Note: 2
5355 * Verification Tag:
5356 * Inputs
5357 * (endpoint, asoc)
5358 *
5359 * RFC 2960 Section 4 Notes
5360 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
1da177e4
LT
5361 * COOKIE ECHO and re-start the T1-cookie timer without changing
5362 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5363 * After that, the endpoint MUST abort the initialization process and
5364 * report the error to SCTP user.
5365 *
5366 * Outputs
5367 * (timers, events)
5368 *
5369 */
3f7a87d2 5370sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
1da177e4
LT
5371 const struct sctp_association *asoc,
5372 const sctp_subtype_t type,
5373 void *arg,
5374 sctp_cmd_seq_t *commands)
5375{
3f7a87d2
FF
5376 struct sctp_chunk *repl = NULL;
5377 int attempts = asoc->init_err_counter + 1;
1da177e4 5378
3f7a87d2 5379 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
ac0b0462 5380 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
1da177e4 5381
81845c21 5382 if (attempts <= asoc->max_init_attempts) {
3f7a87d2 5383 repl = sctp_make_cookie_echo(asoc, NULL);
1da177e4 5384 if (!repl)
3f7a87d2 5385 return SCTP_DISPOSITION_NOMEM;
1da177e4 5386
96cd0d3d
VY
5387 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5388 SCTP_CHUNK(repl));
1da177e4 5389 /* Issue a sideeffect to do the needed accounting. */
3f7a87d2
FF
5390 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5391 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5392
1da177e4
LT
5393 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5394 } else {
8de8c873
SS
5395 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5396 SCTP_ERROR(ETIMEDOUT));
1da177e4 5397 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
dc251b2b 5398 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4
LT
5399 return SCTP_DISPOSITION_DELETE_TCB;
5400 }
5401
5402 return SCTP_DISPOSITION_CONSUME;
1da177e4
LT
5403}
5404
5405/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5406 * with the updated last sequential TSN received from its peer.
5407 *
5408 * An endpoint should limit the number of retransmissions of the
5409 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5410 * If this threshold is exceeded the endpoint should destroy the TCB and
5411 * MUST report the peer endpoint unreachable to the upper layer (and
5412 * thus the association enters the CLOSED state). The reception of any
5413 * packet from its peer (i.e. as the peer sends all of its queued DATA
5414 * chunks) should clear the endpoint's retransmission count and restart
5415 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5416 * all of its queued DATA chunks that have not yet been sent.
5417 */
5418sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5419 const struct sctp_association *asoc,
5420 const sctp_subtype_t type,
5421 void *arg,
5422 sctp_cmd_seq_t *commands)
5423{
5424 struct sctp_chunk *reply = NULL;
5425
5426 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
ac0b0462
SS
5427 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5428
58fbbed4
NH
5429 ((struct sctp_association *)asoc)->shutdown_retries++;
5430
1da177e4 5431 if (asoc->overall_error_count >= asoc->max_retrans) {
8de8c873
SS
5432 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5433 SCTP_ERROR(ETIMEDOUT));
1da177e4
LT
5434 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5435 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 5436 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4
LT
5437 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5438 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5439 return SCTP_DISPOSITION_DELETE_TCB;
5440 }
5441
5442 switch (asoc->state) {
5443 case SCTP_STATE_SHUTDOWN_SENT:
5444 reply = sctp_make_shutdown(asoc, NULL);
5445 break;
5446
5447 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5448 reply = sctp_make_shutdown_ack(asoc, NULL);
5449 break;
5450
5451 default:
5452 BUG();
5453 break;
3ff50b79 5454 }
1da177e4
LT
5455
5456 if (!reply)
5457 goto nomem;
5458
6345b199
WY
5459 /* Do some failure management (Section 8.2).
5460 * If we remove the transport an SHUTDOWN was last sent to, don't
5461 * do failure management.
5462 */
5463 if (asoc->shutdown_last_sent_to)
5464 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5465 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
1da177e4
LT
5466
5467 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5468 * the T2-shutdown timer.
5469 */
5470 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5471
5472 /* Restart the T2-shutdown timer. */
5473 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5474 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5475 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5476 return SCTP_DISPOSITION_CONSUME;
5477
5478nomem:
5479 return SCTP_DISPOSITION_NOMEM;
5480}
5481
5482/*
5483 * ADDIP Section 4.1 ASCONF CHunk Procedures
5484 * If the T4 RTO timer expires the endpoint should do B1 to B5
5485 */
5486sctp_disposition_t sctp_sf_t4_timer_expire(
5487 const struct sctp_endpoint *ep,
5488 const struct sctp_association *asoc,
5489 const sctp_subtype_t type,
5490 void *arg,
5491 sctp_cmd_seq_t *commands)
5492{
5493 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5494 struct sctp_transport *transport = chunk->transport;
5495
ac0b0462
SS
5496 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5497
1da177e4
LT
5498 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5499 * detection on the appropriate destination address as defined in
5500 * RFC2960 [5] section 8.1 and 8.2.
5501 */
10a43cea
WY
5502 if (transport)
5503 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5504 SCTP_TRANSPORT(transport));
1da177e4
LT
5505
5506 /* Reconfig T4 timer and transport. */
5507 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5508
5509 /* ADDIP 4.1 B2) Increment the association error counters and perform
5510 * endpoint failure detection on the association as defined in
5511 * RFC2960 [5] section 8.1 and 8.2.
5512 * association error counter is incremented in SCTP_CMD_STRIKE.
5513 */
5514 if (asoc->overall_error_count >= asoc->max_retrans) {
5515 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5516 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
8de8c873
SS
5517 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5518 SCTP_ERROR(ETIMEDOUT));
1da177e4 5519 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 5520 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4 5521 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3d5a019d 5522 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
1da177e4
LT
5523 return SCTP_DISPOSITION_ABORT;
5524 }
5525
5526 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5527 * the ASCONF chunk was sent by doubling the RTO timer value.
5528 * This is done in SCTP_CMD_STRIKE.
5529 */
5530
5531 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5532 * choose an alternate destination address (please refer to RFC2960
5533 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
d808ad9a 5534 * chunk, it MUST be the same (including its serial number) as the last
1da177e4
LT
5535 * ASCONF sent.
5536 */
5537 sctp_chunk_hold(asoc->addip_last_asconf);
5538 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5539 SCTP_CHUNK(asoc->addip_last_asconf));
5540
5541 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5542 * destination is selected, then the RTO used will be that of the new
5543 * destination address.
5544 */
5545 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5546 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5547
5548 return SCTP_DISPOSITION_CONSUME;
5549}
5550
5551/* sctpimpguide-05 Section 2.12.2
5552 * The sender of the SHUTDOWN MAY also start an overall guard timer
5553 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5554 * At the expiration of this timer the sender SHOULD abort the association
5555 * by sending an ABORT chunk.
5556 */
5557sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5558 const struct sctp_association *asoc,
5559 const sctp_subtype_t type,
5560 void *arg,
5561 sctp_cmd_seq_t *commands)
5562{
5563 struct sctp_chunk *reply = NULL;
5564
5565 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
ac0b0462 5566 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
1da177e4
LT
5567
5568 reply = sctp_make_abort(asoc, NULL, 0);
5569 if (!reply)
5570 goto nomem;
5571
5572 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
8de8c873
SS
5573 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5574 SCTP_ERROR(ETIMEDOUT));
1da177e4 5575 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 5576 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1da177e4 5577
a1080a8b
VY
5578 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5579 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5580
1da177e4
LT
5581 return SCTP_DISPOSITION_DELETE_TCB;
5582nomem:
5583 return SCTP_DISPOSITION_NOMEM;
5584}
5585
5586/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5587 * the association is automatically closed by starting the shutdown process.
5588 * The work that needs to be done is same as when SHUTDOWN is initiated by
5589 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5590 */
5591sctp_disposition_t sctp_sf_autoclose_timer_expire(
5592 const struct sctp_endpoint *ep,
5593 const struct sctp_association *asoc,
5594 const sctp_subtype_t type,
5595 void *arg,
5596 sctp_cmd_seq_t *commands)
5597{
5598 int disposition;
5599
ac0b0462
SS
5600 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5601
1da177e4
LT
5602 /* From 9.2 Shutdown of an Association
5603 * Upon receipt of the SHUTDOWN primitive from its upper
5604 * layer, the endpoint enters SHUTDOWN-PENDING state and
5605 * remains there until all outstanding data has been
5606 * acknowledged by its peer. The endpoint accepts no new data
5607 * from its upper layer, but retransmits data to the far end
5608 * if necessary to fill gaps.
5609 */
5610 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5611 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5612
1da177e4
LT
5613 disposition = SCTP_DISPOSITION_CONSUME;
5614 if (sctp_outq_is_empty(&asoc->outqueue)) {
5615 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5616 arg, commands);
5617 }
5618 return disposition;
5619}
5620
5621/*****************************************************************************
5622 * These are sa state functions which could apply to all types of events.
5623 ****************************************************************************/
5624
5625/*
5626 * This table entry is not implemented.
5627 *
5628 * Inputs
5629 * (endpoint, asoc, chunk)
5630 *
5631 * The return value is the disposition of the chunk.
5632 */
5633sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5634 const struct sctp_association *asoc,
5635 const sctp_subtype_t type,
5636 void *arg,
5637 sctp_cmd_seq_t *commands)
5638{
5639 return SCTP_DISPOSITION_NOT_IMPL;
5640}
5641
5642/*
5643 * This table entry represents a bug.
5644 *
5645 * Inputs
5646 * (endpoint, asoc, chunk)
5647 *
5648 * The return value is the disposition of the chunk.
5649 */
5650sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5651 const struct sctp_association *asoc,
5652 const sctp_subtype_t type,
5653 void *arg,
5654 sctp_cmd_seq_t *commands)
5655{
5656 return SCTP_DISPOSITION_BUG;
5657}
5658
5659/*
5660 * This table entry represents the firing of a timer in the wrong state.
5661 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5662 * when the association is in the wrong state. This event should
5663 * be ignored, so as to prevent any rearming of the timer.
5664 *
5665 * Inputs
5666 * (endpoint, asoc, chunk)
5667 *
5668 * The return value is the disposition of the chunk.
5669 */
5670sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5671 const struct sctp_association *asoc,
5672 const sctp_subtype_t type,
5673 void *arg,
5674 sctp_cmd_seq_t *commands)
5675{
5676 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5677 return SCTP_DISPOSITION_CONSUME;
5678}
5679
5680/********************************************************************
5681 * 2nd Level Abstractions
5682 ********************************************************************/
5683
5684/* Pull the SACK chunk based on the SACK header. */
5685static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5686{
5687 struct sctp_sackhdr *sack;
5688 unsigned int len;
5689 __u16 num_blocks;
5690 __u16 num_dup_tsns;
5691
5692 /* Protect ourselves from reading too far into
5693 * the skb from a bogus sender.
5694 */
5695 sack = (struct sctp_sackhdr *) chunk->skb->data;
5696
5697 num_blocks = ntohs(sack->num_gap_ack_blocks);
5698 num_dup_tsns = ntohs(sack->num_dup_tsns);
5699 len = sizeof(struct sctp_sackhdr);
5700 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5701 if (len > chunk->skb->len)
5702 return NULL;
5703
5704 skb_pull(chunk->skb, len);
5705
5706 return sack;
5707}
5708
5709/* Create an ABORT packet to be sent as a response, with the specified
5710 * error causes.
5711 */
5712static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5713 const struct sctp_association *asoc,
5714 struct sctp_chunk *chunk,
5715 const void *payload,
5716 size_t paylen)
5717{
5718 struct sctp_packet *packet;
5719 struct sctp_chunk *abort;
5720
5721 packet = sctp_ootb_pkt_new(asoc, chunk);
5722
5723 if (packet) {
5724 /* Make an ABORT.
5725 * The T bit will be set if the asoc is NULL.
5726 */
5727 abort = sctp_make_abort(asoc, chunk, paylen);
5728 if (!abort) {
5729 sctp_ootb_pkt_free(packet);
5730 return NULL;
5731 }
047a2428
JF
5732
5733 /* Reflect vtag if T-Bit is set */
5734 if (sctp_test_T_bit(abort))
5735 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5736
1da177e4
LT
5737 /* Add specified error causes, i.e., payload, to the
5738 * end of the chunk.
5739 */
5740 sctp_addto_chunk(abort, paylen, payload);
5741
5742 /* Set the skb to the belonging sock for accounting. */
5743 abort->skb->sk = ep->base.sk;
5744
5745 sctp_packet_append_chunk(packet, abort);
5746
5747 }
5748
5749 return packet;
5750}
5751
5752/* Allocate a packet for responding in the OOTB conditions. */
5753static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5754 const struct sctp_chunk *chunk)
5755{
5756 struct sctp_packet *packet;
5757 struct sctp_transport *transport;
5758 __u16 sport;
5759 __u16 dport;
5760 __u32 vtag;
5761
5762 /* Get the source and destination port from the inbound packet. */
5763 sport = ntohs(chunk->sctp_hdr->dest);
5764 dport = ntohs(chunk->sctp_hdr->source);
5765
5766 /* The V-tag is going to be the same as the inbound packet if no
5767 * association exists, otherwise, use the peer's vtag.
5768 */
5769 if (asoc) {
02c4e12c
WY
5770 /* Special case the INIT-ACK as there is no peer's vtag
5771 * yet.
5772 */
5773 switch(chunk->chunk_hdr->type) {
5774 case SCTP_CID_INIT_ACK:
5775 {
5776 sctp_initack_chunk_t *initack;
5777
5778 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5779 vtag = ntohl(initack->init_hdr.init_tag);
5780 break;
5781 }
5782 default:
5783 vtag = asoc->peer.i.init_tag;
5784 break;
5785 }
1da177e4
LT
5786 } else {
5787 /* Special case the INIT and stale COOKIE_ECHO as there is no
5788 * vtag yet.
5789 */
5790 switch(chunk->chunk_hdr->type) {
5791 case SCTP_CID_INIT:
5792 {
5793 sctp_init_chunk_t *init;
5794
5795 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5796 vtag = ntohl(init->init_hdr.init_tag);
5797 break;
5798 }
d808ad9a 5799 default:
1da177e4
LT
5800 vtag = ntohl(chunk->sctp_hdr->vtag);
5801 break;
5802 }
5803 }
5804
5805 /* Make a transport for the bucket, Eliza... */
6a1e5f33 5806 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
1da177e4
LT
5807 if (!transport)
5808 goto nomem;
5809
5810 /* Cache a route for the transport with the chunk's destination as
5811 * the source address.
5812 */
16b0a030 5813 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
1da177e4
LT
5814 sctp_sk(sctp_get_ctl_sock()));
5815
5816 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5817 packet = sctp_packet_config(packet, vtag, 0);
5818
5819 return packet;
5820
5821nomem:
5822 return NULL;
5823}
5824
5825/* Free the packet allocated earlier for responding in the OOTB condition. */
5826void sctp_ootb_pkt_free(struct sctp_packet *packet)
5827{
5828 sctp_transport_free(packet->transport);
5829}
5830
5831/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5832static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5833 const struct sctp_association *asoc,
5834 const struct sctp_chunk *chunk,
5835 sctp_cmd_seq_t *commands,
5836 struct sctp_chunk *err_chunk)
5837{
5838 struct sctp_packet *packet;
5839
5840 if (err_chunk) {
5841 packet = sctp_ootb_pkt_new(asoc, chunk);
5842 if (packet) {
5843 struct sctp_signed_cookie *cookie;
5844
5845 /* Override the OOTB vtag from the cookie. */
5846 cookie = chunk->subh.cookie_hdr;
5847 packet->vtag = cookie->c.peer_vtag;
d808ad9a 5848
1da177e4
LT
5849 /* Set the skb to the belonging sock for accounting. */
5850 err_chunk->skb->sk = ep->base.sk;
5851 sctp_packet_append_chunk(packet, err_chunk);
5852 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5853 SCTP_PACKET(packet));
5854 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5855 } else
5856 sctp_chunk_free (err_chunk);
5857 }
5858}
5859
5860
5861/* Process a data chunk */
5862static int sctp_eat_data(const struct sctp_association *asoc,
5863 struct sctp_chunk *chunk,
5864 sctp_cmd_seq_t *commands)
5865{
5866 sctp_datahdr_t *data_hdr;
5867 struct sctp_chunk *err;
5868 size_t datalen;
5869 sctp_verb_t deliver;
5870 int tmp;
5871 __u32 tsn;
7c3ceb4f 5872 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
049b3ff5 5873 struct sock *sk = asoc->base.sk;
1da177e4
LT
5874
5875 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5876 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5877
5878 tsn = ntohl(data_hdr->tsn);
5879 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5880
5881 /* ASSERT: Now skb->data is really the user data. */
5882
5883 /* Process ECN based congestion.
5884 *
5885 * Since the chunk structure is reused for all chunks within
5886 * a packet, we use ecn_ce_done to track if we've already
5887 * done CE processing for this packet.
5888 *
5889 * We need to do ECN processing even if we plan to discard the
5890 * chunk later.
5891 */
5892
5893 if (!chunk->ecn_ce_done) {
5894 struct sctp_af *af;
5895 chunk->ecn_ce_done = 1;
5896
5897 af = sctp_get_af_specific(
eddc9ec5 5898 ipver2af(ip_hdr(chunk->skb)->version));
1da177e4
LT
5899
5900 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5901 /* Do real work as sideffect. */
5902 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5903 SCTP_U32(tsn));
5904 }
5905 }
5906
5907 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5908 if (tmp < 0) {
5909 /* The TSN is too high--silently discard the chunk and
5910 * count on it getting retransmitted later.
5911 */
5912 return SCTP_IERROR_HIGH_TSN;
5913 } else if (tmp > 0) {
5914 /* This is a duplicate. Record it. */
5915 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5916 return SCTP_IERROR_DUP_TSN;
5917 }
5918
5919 /* This is a new TSN. */
5920
5921 /* Discard if there is no room in the receive window.
5922 * Actually, allow a little bit of overflow (up to a MTU).
5923 */
5924 datalen = ntohs(chunk->chunk_hdr->length);
5925 datalen -= sizeof(sctp_data_chunk_t);
5926
5927 deliver = SCTP_CMD_CHUNK_ULP;
5928
5929 /* Think about partial delivery. */
5930 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5931
5932 /* Even if we don't accept this chunk there is
5933 * memory pressure.
5934 */
5935 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5936 }
5937
d808ad9a 5938 /* Spill over rwnd a little bit. Note: While allowed, this spill over
1da177e4
LT
5939 * seems a bit troublesome in that frag_point varies based on
5940 * PMTU. In cases, such as loopback, this might be a rather
5941 * large spill over.
4d93df0a
NH
5942 */
5943 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5944 (datalen > asoc->rwnd + asoc->frag_point))) {
1da177e4
LT
5945
5946 /* If this is the next TSN, consider reneging to make
5947 * room. Note: Playing nice with a confused sender. A
5948 * malicious sender can still eat up all our buffer
5949 * space and in the future we may want to detect and
5950 * do more drastic reneging.
5951 */
7c3ceb4f
NH
5952 if (sctp_tsnmap_has_gap(map) &&
5953 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
1da177e4
LT
5954 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5955 deliver = SCTP_CMD_RENEGE;
5956 } else {
5957 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5958 "rwnd: %d\n", tsn, datalen,
5959 asoc->rwnd);
5960 return SCTP_IERROR_IGNORE_TSN;
5961 }
5962 }
5963
4d93df0a
NH
5964 /*
5965 * Also try to renege to limit our memory usage in the event that
5966 * we are under memory pressure
3ab224be 5967 * If we can't renege, don't worry about it, the sk_rmem_schedule
4d93df0a
NH
5968 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5969 * memory usage too much
5970 */
5971 if (*sk->sk_prot_creator->memory_pressure) {
5972 if (sctp_tsnmap_has_gap(map) &&
5973 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5974 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5975 deliver = SCTP_CMD_RENEGE;
5976 }
5977 }
5978
1da177e4
LT
5979 /*
5980 * Section 3.3.10.9 No User Data (9)
5981 *
5982 * Cause of error
5983 * ---------------
5984 * No User Data: This error cause is returned to the originator of a
5985 * DATA chunk if a received DATA chunk has no user data.
5986 */
5987 if (unlikely(0 == datalen)) {
5988 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5989 if (err) {
5990 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5991 SCTP_CHUNK(err));
5992 }
5993 /* We are going to ABORT, so we might as well stop
5994 * processing the rest of the chunks in the packet.
5995 */
5996 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
8de8c873
SS
5997 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5998 SCTP_ERROR(ECONNABORTED));
1da177e4 5999 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5be291fe 6000 SCTP_PERR(SCTP_ERROR_NO_DATA));
1da177e4
LT
6001 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6002 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6003 return SCTP_IERROR_NO_DATA;
6004 }
6005
9faa730f
SS
6006 chunk->data_accepted = 1;
6007
1da177e4
LT
6008 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6009 * if we renege and the chunk arrives again.
6010 */
6011 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6012 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6013 else
6014 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6015
6016 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6017 *
6018 * If an endpoint receive a DATA chunk with an invalid stream
6019 * identifier, it shall acknowledge the reception of the DATA chunk
6020 * following the normal procedure, immediately send an ERROR chunk
6021 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6022 * and discard the DATA chunk.
6023 */
6024 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
3888e9ef
VY
6025 /* Mark tsn as received even though we drop it */
6026 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6027
1da177e4
LT
6028 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6029 &data_hdr->stream,
6030 sizeof(data_hdr->stream));
6031 if (err)
6032 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6033 SCTP_CHUNK(err));
6034 return SCTP_IERROR_BAD_STREAM;
6035 }
6036
6037 /* Send the data up to the user. Note: Schedule the
6038 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6039 * chunk needs the updated rwnd.
6040 */
6041 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6042
6043 return SCTP_IERROR_NO_ERROR;
6044}