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