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