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