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