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