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