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