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