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