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