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