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