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