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