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