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