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