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