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