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