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