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