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