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