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