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