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