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