xref: /linux/net/sctp/sm_statefuns.c (revision ddd664bbff63e09e7a7f9acae9c43605d4cf185f)
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 	sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
2602 
2603 	/* Cast away the const modifier, as we want to just
2604 	 * rerun it through as a sideffect.
2605 	 */
2606 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2607 
2608 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2609 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2610 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2611 			SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2612 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2613 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2614 
2615 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2616 
2617 	return SCTP_DISPOSITION_CONSUME;
2618 
2619 nomem:
2620 	return SCTP_DISPOSITION_NOMEM;
2621 }
2622 
2623 /*
2624  * Process an ABORT.
2625  *
2626  * Section: 9.1
2627  * After checking the Verification Tag, the receiving endpoint shall
2628  * remove the association from its record, and shall report the
2629  * termination to its upper layer.
2630  *
2631  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2632  * B) Rules for packet carrying ABORT:
2633  *
2634  *  - The endpoint shall always fill in the Verification Tag field of the
2635  *    outbound packet with the destination endpoint's tag value if it
2636  *    is known.
2637  *
2638  *  - If the ABORT is sent in response to an OOTB packet, the endpoint
2639  *    MUST follow the procedure described in Section 8.4.
2640  *
2641  *  - The receiver MUST accept the packet if the Verification Tag
2642  *    matches either its own tag, OR the tag of its peer. Otherwise, the
2643  *    receiver MUST silently discard the packet and take no further
2644  *    action.
2645  *
2646  * Inputs
2647  * (endpoint, asoc, chunk)
2648  *
2649  * Outputs
2650  * (asoc, reply_msg, msg_up, timers, counters)
2651  *
2652  * The return value is the disposition of the chunk.
2653  */
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)2654 enum sctp_disposition sctp_sf_do_9_1_abort(
2655 					struct net *net,
2656 					const struct sctp_endpoint *ep,
2657 					const struct sctp_association *asoc,
2658 					const union sctp_subtype type,
2659 					void *arg,
2660 					struct sctp_cmd_seq *commands)
2661 {
2662 	struct sctp_chunk *chunk = arg;
2663 
2664 	if (!sctp_vtag_verify_either(chunk, asoc))
2665 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2666 
2667 	/* Make sure that the ABORT chunk has a valid length.
2668 	 * Since this is an ABORT chunk, we have to discard it
2669 	 * because of the following text:
2670 	 * RFC 2960, Section 3.3.7
2671 	 *    If an endpoint receives an ABORT with a format error or for an
2672 	 *    association that doesn't exist, it MUST silently discard it.
2673 	 * Because the length is "invalid", we can't really discard just
2674 	 * as we do not know its true length.  So, to be safe, discard the
2675 	 * packet.
2676 	 */
2677 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2678 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2679 
2680 	/* ADD-IP: Special case for ABORT chunks
2681 	 * F4)  One special consideration is that ABORT Chunks arriving
2682 	 * destined to the IP address being deleted MUST be
2683 	 * ignored (see Section 5.3.1 for further details).
2684 	 */
2685 	if (SCTP_ADDR_DEL ==
2686 		    sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2687 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2688 
2689 	if (!sctp_err_chunk_valid(chunk))
2690 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2691 
2692 	return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2693 }
2694 
__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)2695 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2696 					struct net *net,
2697 					const struct sctp_endpoint *ep,
2698 					const struct sctp_association *asoc,
2699 					const union sctp_subtype type,
2700 					void *arg,
2701 					struct sctp_cmd_seq *commands)
2702 {
2703 	__be16 error = SCTP_ERROR_NO_ERROR;
2704 	struct sctp_chunk *chunk = arg;
2705 	unsigned int len;
2706 
2707 	/* See if we have an error cause code in the chunk.  */
2708 	len = ntohs(chunk->chunk_hdr->length);
2709 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2710 		error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2711 
2712 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2713 	/* ASSOC_FAILED will DELETE_TCB. */
2714 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2715 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2716 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2717 
2718 	return SCTP_DISPOSITION_ABORT;
2719 }
2720 
2721 /*
2722  * Process an ABORT.  (COOKIE-WAIT state)
2723  *
2724  * See sctp_sf_do_9_1_abort() above.
2725  */
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)2726 enum sctp_disposition sctp_sf_cookie_wait_abort(
2727 					struct net *net,
2728 					const struct sctp_endpoint *ep,
2729 					const struct sctp_association *asoc,
2730 					const union sctp_subtype type,
2731 					void *arg,
2732 					struct sctp_cmd_seq *commands)
2733 {
2734 	__be16 error = SCTP_ERROR_NO_ERROR;
2735 	struct sctp_chunk *chunk = arg;
2736 	unsigned int len;
2737 
2738 	if (!sctp_vtag_verify_either(chunk, asoc))
2739 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2740 
2741 	/* Make sure that the ABORT chunk has a valid length.
2742 	 * Since this is an ABORT chunk, we have to discard it
2743 	 * because of the following text:
2744 	 * RFC 2960, Section 3.3.7
2745 	 *    If an endpoint receives an ABORT with a format error or for an
2746 	 *    association that doesn't exist, it MUST silently discard it.
2747 	 * Because the length is "invalid", we can't really discard just
2748 	 * as we do not know its true length.  So, to be safe, discard the
2749 	 * packet.
2750 	 */
2751 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2752 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2753 
2754 	/* See if we have an error cause code in the chunk.  */
2755 	len = ntohs(chunk->chunk_hdr->length);
2756 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2757 		error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2758 
2759 	return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2760 				      chunk->transport);
2761 }
2762 
2763 /*
2764  * Process an incoming ICMP as an ABORT.  (COOKIE-WAIT state)
2765  */
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)2766 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2767 					struct net *net,
2768 					const struct sctp_endpoint *ep,
2769 					const struct sctp_association *asoc,
2770 					const union sctp_subtype type,
2771 					void *arg,
2772 					struct sctp_cmd_seq *commands)
2773 {
2774 	return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2775 				      ENOPROTOOPT, asoc,
2776 				      (struct sctp_transport *)arg);
2777 }
2778 
2779 /*
2780  * Process an ABORT.  (COOKIE-ECHOED state)
2781  */
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)2782 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2783 					struct net *net,
2784 					const struct sctp_endpoint *ep,
2785 					const struct sctp_association *asoc,
2786 					const union sctp_subtype type,
2787 					void *arg,
2788 					struct sctp_cmd_seq *commands)
2789 {
2790 	/* There is a single T1 timer, so we should be able to use
2791 	 * common function with the COOKIE-WAIT state.
2792 	 */
2793 	return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2794 }
2795 
2796 /*
2797  * Stop T1 timer and abort association with "INIT failed".
2798  *
2799  * This is common code called by several sctp_sf_*_abort() functions above.
2800  */
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)2801 static enum sctp_disposition sctp_stop_t1_and_abort(
2802 					struct net *net,
2803 					struct sctp_cmd_seq *commands,
2804 					__be16 error, int sk_err,
2805 					const struct sctp_association *asoc,
2806 					struct sctp_transport *transport)
2807 {
2808 	pr_debug("%s: ABORT received (INIT)\n", __func__);
2809 
2810 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2811 			SCTP_STATE(SCTP_STATE_CLOSED));
2812 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2813 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2814 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2815 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2816 	/* CMD_INIT_FAILED will DELETE_TCB. */
2817 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2818 			SCTP_PERR(error));
2819 
2820 	return SCTP_DISPOSITION_ABORT;
2821 }
2822 
2823 /*
2824  * sctp_sf_do_9_2_shut
2825  *
2826  * Section: 9.2
2827  * Upon the reception of the SHUTDOWN, the peer endpoint shall
2828  *  - enter the SHUTDOWN-RECEIVED state,
2829  *
2830  *  - stop accepting new data from its SCTP user
2831  *
2832  *  - verify, by checking the Cumulative TSN Ack field of the chunk,
2833  *    that all its outstanding DATA chunks have been received by the
2834  *    SHUTDOWN sender.
2835  *
2836  * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2837  * send a SHUTDOWN in response to a ULP request. And should discard
2838  * subsequent SHUTDOWN chunks.
2839  *
2840  * If there are still outstanding DATA chunks left, the SHUTDOWN
2841  * receiver shall continue to follow normal data transmission
2842  * procedures defined in Section 6 until all outstanding DATA chunks
2843  * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2844  * new data from its SCTP user.
2845  *
2846  * Verification Tag:  8.5 Verification Tag [Normal verification]
2847  *
2848  * Inputs
2849  * (endpoint, asoc, chunk)
2850  *
2851  * Outputs
2852  * (asoc, reply_msg, msg_up, timers, counters)
2853  *
2854  * The return value is the disposition of the chunk.
2855  */
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)2856 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2857 					struct net *net,
2858 					const struct sctp_endpoint *ep,
2859 					const struct sctp_association *asoc,
2860 					const union sctp_subtype type,
2861 					void *arg,
2862 					struct sctp_cmd_seq *commands)
2863 {
2864 	enum sctp_disposition disposition;
2865 	struct sctp_chunk *chunk = arg;
2866 	struct sctp_shutdownhdr *sdh;
2867 	struct sctp_ulpevent *ev;
2868 	__u32 ctsn;
2869 
2870 	if (!sctp_vtag_verify(chunk, asoc))
2871 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2872 
2873 	/* Make sure that the SHUTDOWN chunk has a valid length. */
2874 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2875 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2876 						  commands);
2877 
2878 	/* Convert the elaborate header.  */
2879 	sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2880 	skb_pull(chunk->skb, sizeof(*sdh));
2881 	chunk->subh.shutdown_hdr = sdh;
2882 	ctsn = ntohl(sdh->cum_tsn_ack);
2883 
2884 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2885 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2886 			 asoc->ctsn_ack_point);
2887 
2888 		return SCTP_DISPOSITION_DISCARD;
2889 	}
2890 
2891 	/* If Cumulative TSN Ack beyond the max tsn currently
2892 	 * send, terminating the association and respond to the
2893 	 * sender with an ABORT.
2894 	 */
2895 	if (!TSN_lt(ctsn, asoc->next_tsn))
2896 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2897 
2898 	/* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2899 	 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2900 	 * inform the application that it should cease sending data.
2901 	 */
2902 	ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2903 	if (!ev) {
2904 		disposition = SCTP_DISPOSITION_NOMEM;
2905 		goto out;
2906 	}
2907 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2908 
2909 	/* Upon the reception of the SHUTDOWN, the peer endpoint shall
2910 	 *  - enter the SHUTDOWN-RECEIVED state,
2911 	 *  - stop accepting new data from its SCTP user
2912 	 *
2913 	 * [This is implicit in the new state.]
2914 	 */
2915 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2916 			SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2917 	disposition = SCTP_DISPOSITION_CONSUME;
2918 
2919 	if (sctp_outq_is_empty(&asoc->outqueue)) {
2920 		disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2921 							  arg, commands);
2922 	}
2923 
2924 	if (SCTP_DISPOSITION_NOMEM == disposition)
2925 		goto out;
2926 
2927 	/*  - verify, by checking the Cumulative TSN Ack field of the
2928 	 *    chunk, that all its outstanding DATA chunks have been
2929 	 *    received by the SHUTDOWN sender.
2930 	 */
2931 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2932 			SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2933 
2934 out:
2935 	return disposition;
2936 }
2937 
2938 /*
2939  * sctp_sf_do_9_2_shut_ctsn
2940  *
2941  * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2942  * it MUST NOT send a SHUTDOWN in response to a ULP request.
2943  * The Cumulative TSN Ack of the received SHUTDOWN chunk
2944  * MUST be processed.
2945  */
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)2946 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2947 					struct net *net,
2948 					const struct sctp_endpoint *ep,
2949 					const struct sctp_association *asoc,
2950 					const union sctp_subtype type,
2951 					void *arg,
2952 					struct sctp_cmd_seq *commands)
2953 {
2954 	struct sctp_chunk *chunk = arg;
2955 	struct sctp_shutdownhdr *sdh;
2956 	__u32 ctsn;
2957 
2958 	if (!sctp_vtag_verify(chunk, asoc))
2959 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2960 
2961 	/* Make sure that the SHUTDOWN chunk has a valid length. */
2962 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2963 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2964 						  commands);
2965 
2966 	sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2967 	ctsn = ntohl(sdh->cum_tsn_ack);
2968 
2969 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2970 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2971 			 asoc->ctsn_ack_point);
2972 
2973 		return SCTP_DISPOSITION_DISCARD;
2974 	}
2975 
2976 	/* If Cumulative TSN Ack beyond the max tsn currently
2977 	 * send, terminating the association and respond to the
2978 	 * sender with an ABORT.
2979 	 */
2980 	if (!TSN_lt(ctsn, asoc->next_tsn))
2981 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2982 
2983 	/* verify, by checking the Cumulative TSN Ack field of the
2984 	 * chunk, that all its outstanding DATA chunks have been
2985 	 * received by the SHUTDOWN sender.
2986 	 */
2987 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2988 			SCTP_BE32(sdh->cum_tsn_ack));
2989 
2990 	return SCTP_DISPOSITION_CONSUME;
2991 }
2992 
2993 /* RFC 2960 9.2
2994  * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2995  * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2996  * transport addresses (either in the IP addresses or in the INIT chunk)
2997  * that belong to this association, it should discard the INIT chunk and
2998  * retransmit the SHUTDOWN ACK chunk.
2999  */
3000 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)3001 __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
3002 			   const struct sctp_association *asoc,
3003 			   const union sctp_subtype type, void *arg,
3004 			   struct sctp_cmd_seq *commands)
3005 {
3006 	struct sctp_chunk *chunk = arg;
3007 	struct sctp_chunk *reply;
3008 
3009 	/* Make sure that the chunk has a valid length */
3010 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3011 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3012 						  commands);
3013 
3014 	/* Since we are not going to really process this INIT, there
3015 	 * is no point in verifying chunk boundaries.  Just generate
3016 	 * the SHUTDOWN ACK.
3017 	 */
3018 	reply = sctp_make_shutdown_ack(asoc, chunk);
3019 	if (NULL == reply)
3020 		goto nomem;
3021 
3022 	/* Set the transport for the SHUTDOWN ACK chunk and the timeout for
3023 	 * the T2-SHUTDOWN timer.
3024 	 */
3025 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
3026 
3027 	/* and restart the T2-shutdown timer. */
3028 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3029 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3030 
3031 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3032 
3033 	return SCTP_DISPOSITION_CONSUME;
3034 nomem:
3035 	return SCTP_DISPOSITION_NOMEM;
3036 }
3037 
3038 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)3039 sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
3040 			 const struct sctp_association *asoc,
3041 			 const union sctp_subtype type, void *arg,
3042 			 struct sctp_cmd_seq *commands)
3043 {
3044 	struct sctp_chunk *chunk = arg;
3045 
3046 	if (!chunk->singleton)
3047 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3048 
3049 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
3050 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3051 
3052 	if (chunk->sctp_hdr->vtag != 0)
3053 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3054 
3055 	return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
3056 }
3057 
3058 /*
3059  * sctp_sf_do_ecn_cwr
3060  *
3061  * Section:  Appendix A: Explicit Congestion Notification
3062  *
3063  * CWR:
3064  *
3065  * RFC 2481 details a specific bit for a sender to send in the header of
3066  * its next outbound TCP segment to indicate to its peer that it has
3067  * reduced its congestion window.  This is termed the CWR bit.  For
3068  * SCTP the same indication is made by including the CWR chunk.
3069  * This chunk contains one data element, i.e. the TSN number that
3070  * was sent in the ECNE chunk.  This element represents the lowest
3071  * TSN number in the datagram that was originally marked with the
3072  * CE bit.
3073  *
3074  * Verification Tag: 8.5 Verification Tag [Normal verification]
3075  * Inputs
3076  * (endpoint, asoc, chunk)
3077  *
3078  * Outputs
3079  * (asoc, reply_msg, msg_up, timers, counters)
3080  *
3081  * The return value is the disposition of the chunk.
3082  */
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)3083 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
3084 					 const struct sctp_endpoint *ep,
3085 					 const struct sctp_association *asoc,
3086 					 const union sctp_subtype type,
3087 					 void *arg,
3088 					 struct sctp_cmd_seq *commands)
3089 {
3090 	struct sctp_chunk *chunk = arg;
3091 	struct sctp_cwrhdr *cwr;
3092 	u32 lowest_tsn;
3093 
3094 	if (!sctp_vtag_verify(chunk, asoc))
3095 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3096 
3097 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3098 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3099 						  commands);
3100 
3101 	cwr = (struct sctp_cwrhdr *)chunk->skb->data;
3102 	skb_pull(chunk->skb, sizeof(*cwr));
3103 
3104 	lowest_tsn = ntohl(cwr->lowest_tsn);
3105 
3106 	/* Does this CWR ack the last sent congestion notification? */
3107 	if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
3108 		/* Stop sending ECNE. */
3109 		sctp_add_cmd_sf(commands,
3110 				SCTP_CMD_ECN_CWR,
3111 				SCTP_U32(lowest_tsn));
3112 	}
3113 	return SCTP_DISPOSITION_CONSUME;
3114 }
3115 
3116 /*
3117  * sctp_sf_do_ecne
3118  *
3119  * Section:  Appendix A: Explicit Congestion Notification
3120  *
3121  * ECN-Echo
3122  *
3123  * RFC 2481 details a specific bit for a receiver to send back in its
3124  * TCP acknowledgements to notify the sender of the Congestion
3125  * Experienced (CE) bit having arrived from the network.  For SCTP this
3126  * same indication is made by including the ECNE chunk.  This chunk
3127  * contains one data element, i.e. the lowest TSN associated with the IP
3128  * datagram marked with the CE bit.....
3129  *
3130  * Verification Tag: 8.5 Verification Tag [Normal verification]
3131  * Inputs
3132  * (endpoint, asoc, chunk)
3133  *
3134  * Outputs
3135  * (asoc, reply_msg, msg_up, timers, counters)
3136  *
3137  * The return value is the disposition of the chunk.
3138  */
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)3139 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3140 				      const struct sctp_endpoint *ep,
3141 				      const struct sctp_association *asoc,
3142 				      const union sctp_subtype type,
3143 				      void *arg, struct sctp_cmd_seq *commands)
3144 {
3145 	struct sctp_chunk *chunk = arg;
3146 	struct sctp_ecnehdr *ecne;
3147 
3148 	if (!sctp_vtag_verify(chunk, asoc))
3149 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3150 
3151 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3152 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3153 						  commands);
3154 
3155 	ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3156 	skb_pull(chunk->skb, sizeof(*ecne));
3157 
3158 	/* If this is a newer ECNE than the last CWR packet we sent out */
3159 	sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3160 			SCTP_U32(ntohl(ecne->lowest_tsn)));
3161 
3162 	return SCTP_DISPOSITION_CONSUME;
3163 }
3164 
3165 /*
3166  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
3167  *
3168  * The SCTP endpoint MUST always acknowledge the reception of each valid
3169  * DATA chunk.
3170  *
3171  * The guidelines on delayed acknowledgement algorithm specified in
3172  * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3173  * acknowledgement SHOULD be generated for at least every second packet
3174  * (not every second DATA chunk) received, and SHOULD be generated within
3175  * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3176  * situations it may be beneficial for an SCTP transmitter to be more
3177  * conservative than the algorithms detailed in this document allow.
3178  * However, an SCTP transmitter MUST NOT be more aggressive than the
3179  * following algorithms allow.
3180  *
3181  * A SCTP receiver MUST NOT generate more than one SACK for every
3182  * incoming packet, other than to update the offered window as the
3183  * receiving application consumes new data.
3184  *
3185  * Verification Tag:  8.5 Verification Tag [Normal verification]
3186  *
3187  * Inputs
3188  * (endpoint, asoc, chunk)
3189  *
3190  * Outputs
3191  * (asoc, reply_msg, msg_up, timers, counters)
3192  *
3193  * The return value is the disposition of the chunk.
3194  */
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)3195 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3196 					   const struct sctp_endpoint *ep,
3197 					   const struct sctp_association *asoc,
3198 					   const union sctp_subtype type,
3199 					   void *arg,
3200 					   struct sctp_cmd_seq *commands)
3201 {
3202 	union sctp_arg force = SCTP_NOFORCE();
3203 	struct sctp_chunk *chunk = arg;
3204 	int error;
3205 
3206 	if (!sctp_vtag_verify(chunk, asoc)) {
3207 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3208 				SCTP_NULL());
3209 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3210 	}
3211 
3212 	if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3213 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3214 						  commands);
3215 
3216 	error = sctp_eat_data(asoc, chunk, commands);
3217 	switch (error) {
3218 	case SCTP_IERROR_NO_ERROR:
3219 		break;
3220 	case SCTP_IERROR_HIGH_TSN:
3221 	case SCTP_IERROR_BAD_STREAM:
3222 		SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3223 		goto discard_noforce;
3224 	case SCTP_IERROR_DUP_TSN:
3225 	case SCTP_IERROR_IGNORE_TSN:
3226 		SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3227 		goto discard_force;
3228 	case SCTP_IERROR_NO_DATA:
3229 		return SCTP_DISPOSITION_ABORT;
3230 	case SCTP_IERROR_PROTO_VIOLATION:
3231 		return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3232 					       (u8 *)chunk->subh.data_hdr,
3233 					       sctp_datahdr_len(&asoc->stream));
3234 	default:
3235 		BUG();
3236 	}
3237 
3238 	if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3239 		force = SCTP_FORCE();
3240 
3241 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3242 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3243 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3244 	}
3245 
3246 	/* If this is the last chunk in a packet, we need to count it
3247 	 * toward sack generation.  Note that we need to SACK every
3248 	 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3249 	 * THEM.  We elect to NOT generate SACK's if the chunk fails
3250 	 * the verification tag test.
3251 	 *
3252 	 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3253 	 *
3254 	 * The SCTP endpoint MUST always acknowledge the reception of
3255 	 * each valid DATA chunk.
3256 	 *
3257 	 * The guidelines on delayed acknowledgement algorithm
3258 	 * specified in  Section 4.2 of [RFC2581] SHOULD be followed.
3259 	 * Specifically, an acknowledgement SHOULD be generated for at
3260 	 * least every second packet (not every second DATA chunk)
3261 	 * received, and SHOULD be generated within 200 ms of the
3262 	 * arrival of any unacknowledged DATA chunk.  In some
3263 	 * situations it may be beneficial for an SCTP transmitter to
3264 	 * be more conservative than the algorithms detailed in this
3265 	 * document allow. However, an SCTP transmitter MUST NOT be
3266 	 * more aggressive than the following algorithms allow.
3267 	 */
3268 	if (chunk->end_of_packet)
3269 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3270 
3271 	return SCTP_DISPOSITION_CONSUME;
3272 
3273 discard_force:
3274 	/* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3275 	 *
3276 	 * When a packet arrives with duplicate DATA chunk(s) and with
3277 	 * no new DATA chunk(s), the endpoint MUST immediately send a
3278 	 * SACK with no delay.  If a packet arrives with duplicate
3279 	 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3280 	 * MAY immediately send a SACK.  Normally receipt of duplicate
3281 	 * DATA chunks will occur when the original SACK chunk was lost
3282 	 * and the peer's RTO has expired.  The duplicate TSN number(s)
3283 	 * SHOULD be reported in the SACK as duplicate.
3284 	 */
3285 	/* In our case, we split the MAY SACK advice up whether or not
3286 	 * the last chunk is a duplicate.'
3287 	 */
3288 	if (chunk->end_of_packet)
3289 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3290 	return SCTP_DISPOSITION_DISCARD;
3291 
3292 discard_noforce:
3293 	if (chunk->end_of_packet)
3294 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3295 
3296 	return SCTP_DISPOSITION_DISCARD;
3297 }
3298 
3299 /*
3300  * sctp_sf_eat_data_fast_4_4
3301  *
3302  * Section: 4 (4)
3303  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3304  *    DATA chunks without delay.
3305  *
3306  * Verification Tag:  8.5 Verification Tag [Normal verification]
3307  * Inputs
3308  * (endpoint, asoc, chunk)
3309  *
3310  * Outputs
3311  * (asoc, reply_msg, msg_up, timers, counters)
3312  *
3313  * The return value is the disposition of the chunk.
3314  */
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)3315 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3316 					struct net *net,
3317 					const struct sctp_endpoint *ep,
3318 					const struct sctp_association *asoc,
3319 					const union sctp_subtype type,
3320 					void *arg,
3321 					struct sctp_cmd_seq *commands)
3322 {
3323 	struct sctp_chunk *chunk = arg;
3324 	int error;
3325 
3326 	if (!sctp_vtag_verify(chunk, asoc)) {
3327 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3328 				SCTP_NULL());
3329 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3330 	}
3331 
3332 	if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3333 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3334 						  commands);
3335 
3336 	error = sctp_eat_data(asoc, chunk, commands);
3337 	switch (error) {
3338 	case SCTP_IERROR_NO_ERROR:
3339 	case SCTP_IERROR_HIGH_TSN:
3340 	case SCTP_IERROR_DUP_TSN:
3341 	case SCTP_IERROR_IGNORE_TSN:
3342 	case SCTP_IERROR_BAD_STREAM:
3343 		break;
3344 	case SCTP_IERROR_NO_DATA:
3345 		return SCTP_DISPOSITION_ABORT;
3346 	case SCTP_IERROR_PROTO_VIOLATION:
3347 		return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3348 					       (u8 *)chunk->subh.data_hdr,
3349 					       sctp_datahdr_len(&asoc->stream));
3350 	default:
3351 		BUG();
3352 	}
3353 
3354 	/* Go a head and force a SACK, since we are shutting down. */
3355 
3356 	/* Implementor's Guide.
3357 	 *
3358 	 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3359 	 * respond to each received packet containing one or more DATA chunk(s)
3360 	 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3361 	 */
3362 	if (chunk->end_of_packet) {
3363 		/* We must delay the chunk creation since the cumulative
3364 		 * TSN has not been updated yet.
3365 		 */
3366 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3367 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3368 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3369 				SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3370 	}
3371 
3372 	return SCTP_DISPOSITION_CONSUME;
3373 }
3374 
3375 /*
3376  * Section: 6.2  Processing a Received SACK
3377  * D) Any time a SACK arrives, the endpoint performs the following:
3378  *
3379  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3380  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
3381  *     increasing, a SACK whose Cumulative TSN Ack is less than the
3382  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
3383  *
3384  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
3385  *     of bytes still outstanding after processing the Cumulative TSN Ack
3386  *     and the Gap Ack Blocks.
3387  *
3388  *     iii) If the SACK is missing a TSN that was previously
3389  *     acknowledged via a Gap Ack Block (e.g., the data receiver
3390  *     reneged on the data), then mark the corresponding DATA chunk
3391  *     as available for retransmit:  Mark it as missing for fast
3392  *     retransmit as described in Section 7.2.4 and if no retransmit
3393  *     timer is running for the destination address to which the DATA
3394  *     chunk was originally transmitted, then T3-rtx is started for
3395  *     that destination address.
3396  *
3397  * Verification Tag:  8.5 Verification Tag [Normal verification]
3398  *
3399  * Inputs
3400  * (endpoint, asoc, chunk)
3401  *
3402  * Outputs
3403  * (asoc, reply_msg, msg_up, timers, counters)
3404  *
3405  * The return value is the disposition of the chunk.
3406  */
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)3407 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3408 					   const struct sctp_endpoint *ep,
3409 					   const struct sctp_association *asoc,
3410 					   const union sctp_subtype type,
3411 					   void *arg,
3412 					   struct sctp_cmd_seq *commands)
3413 {
3414 	struct sctp_chunk *chunk = arg;
3415 	struct sctp_sackhdr *sackh;
3416 	__u32 ctsn;
3417 
3418 	if (!sctp_vtag_verify(chunk, asoc))
3419 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3420 
3421 	/* Make sure that the SACK chunk has a valid length. */
3422 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3423 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3424 						  commands);
3425 
3426 	/* Pull the SACK chunk from the data buffer */
3427 	sackh = sctp_sm_pull_sack(chunk);
3428 	/* Was this a bogus SACK? */
3429 	if (!sackh)
3430 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3431 	chunk->subh.sack_hdr = sackh;
3432 	ctsn = ntohl(sackh->cum_tsn_ack);
3433 
3434 	/* If Cumulative TSN Ack beyond the max tsn currently
3435 	 * send, terminating the association and respond to the
3436 	 * sender with an ABORT.
3437 	 */
3438 	if (TSN_lte(asoc->next_tsn, ctsn))
3439 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3440 
3441 	trace_sctp_probe(ep, asoc, chunk);
3442 
3443 	/* i) If Cumulative TSN Ack is less than the Cumulative TSN
3444 	 *     Ack Point, then drop the SACK.  Since Cumulative TSN
3445 	 *     Ack is monotonically increasing, a SACK whose
3446 	 *     Cumulative TSN Ack is less than the Cumulative TSN Ack
3447 	 *     Point indicates an out-of-order SACK.
3448 	 */
3449 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3450 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3451 			 asoc->ctsn_ack_point);
3452 
3453 		return SCTP_DISPOSITION_DISCARD;
3454 	}
3455 
3456 	/* Return this SACK for further processing.  */
3457 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3458 
3459 	/* Note: We do the rest of the work on the PROCESS_SACK
3460 	 * sideeffect.
3461 	 */
3462 	return SCTP_DISPOSITION_CONSUME;
3463 }
3464 
3465 /*
3466  * Generate an ABORT in response to a packet.
3467  *
3468  * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3469  *
3470  * 8) The receiver should respond to the sender of the OOTB packet with
3471  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3472  *    MUST fill in the Verification Tag field of the outbound packet
3473  *    with the value found in the Verification Tag field of the OOTB
3474  *    packet and set the T-bit in the Chunk Flags to indicate that the
3475  *    Verification Tag is reflected.  After sending this ABORT, the
3476  *    receiver of the OOTB packet shall discard the OOTB packet and take
3477  *    no further action.
3478  *
3479  * Verification Tag:
3480  *
3481  * The return value is the disposition of the chunk.
3482 */
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)3483 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3484 					struct net *net,
3485 					const struct sctp_endpoint *ep,
3486 					const struct sctp_association *asoc,
3487 					const union sctp_subtype type,
3488 					void *arg,
3489 					struct sctp_cmd_seq *commands)
3490 {
3491 	struct sctp_packet *packet = NULL;
3492 	struct sctp_chunk *chunk = arg;
3493 	struct sctp_chunk *abort;
3494 
3495 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
3496 	if (!packet)
3497 		return SCTP_DISPOSITION_NOMEM;
3498 
3499 	/* Make an ABORT. The T bit will be set if the asoc
3500 	 * is NULL.
3501 	 */
3502 	abort = sctp_make_abort(asoc, chunk, 0);
3503 	if (!abort) {
3504 		sctp_ootb_pkt_free(packet);
3505 		return SCTP_DISPOSITION_NOMEM;
3506 	}
3507 
3508 	/* Reflect vtag if T-Bit is set */
3509 	if (sctp_test_T_bit(abort))
3510 		packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3511 
3512 	/* Set the skb to the belonging sock for accounting.  */
3513 	abort->skb->sk = ep->base.sk;
3514 
3515 	sctp_packet_append_chunk(packet, abort);
3516 
3517 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(packet));
3518 
3519 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3520 
3521 	sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3522 	return SCTP_DISPOSITION_CONSUME;
3523 }
3524 
3525 /* Handling of SCTP Packets Containing an INIT Chunk Matching an
3526  * Existing Associations when the UDP encap port is incorrect.
3527  *
3528  * From Section 4 at draft-tuexen-tsvwg-sctp-udp-encaps-cons-03.
3529  */
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)3530 static enum sctp_disposition sctp_sf_new_encap_port(
3531 					struct net *net,
3532 					const struct sctp_endpoint *ep,
3533 					const struct sctp_association *asoc,
3534 					const union sctp_subtype type,
3535 					void *arg,
3536 					struct sctp_cmd_seq *commands)
3537 {
3538 	struct sctp_packet *packet = NULL;
3539 	struct sctp_chunk *chunk = arg;
3540 	struct sctp_chunk *abort;
3541 
3542 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
3543 	if (!packet)
3544 		return SCTP_DISPOSITION_NOMEM;
3545 
3546 	abort = sctp_make_new_encap_port(asoc, chunk);
3547 	if (!abort) {
3548 		sctp_ootb_pkt_free(packet);
3549 		return SCTP_DISPOSITION_NOMEM;
3550 	}
3551 
3552 	abort->skb->sk = ep->base.sk;
3553 
3554 	sctp_packet_append_chunk(packet, abort);
3555 
3556 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3557 			SCTP_PACKET(packet));
3558 
3559 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3560 
3561 	sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3562 	return SCTP_DISPOSITION_CONSUME;
3563 }
3564 
3565 /*
3566  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
3567  * event as ULP notification for each cause included in the chunk.
3568  *
3569  * API 5.3.1.3 - SCTP_REMOTE_ERROR
3570  *
3571  * The return value is the disposition of the chunk.
3572 */
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)3573 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3574 					   const struct sctp_endpoint *ep,
3575 					   const struct sctp_association *asoc,
3576 					   const union sctp_subtype type,
3577 					   void *arg,
3578 					   struct sctp_cmd_seq *commands)
3579 {
3580 	struct sctp_chunk *chunk = arg;
3581 	struct sctp_errhdr *err;
3582 
3583 	if (!sctp_vtag_verify(chunk, asoc))
3584 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3585 
3586 	/* Make sure that the ERROR chunk has a valid length. */
3587 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3588 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3589 						  commands);
3590 	sctp_walk_errors(err, chunk->chunk_hdr);
3591 	if ((void *)err != (void *)chunk->chunk_end)
3592 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3593 						  (void *)err, commands);
3594 
3595 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3596 			SCTP_CHUNK(chunk));
3597 
3598 	return SCTP_DISPOSITION_CONSUME;
3599 }
3600 
3601 /*
3602  * Process an inbound SHUTDOWN ACK.
3603  *
3604  * From Section 9.2:
3605  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3606  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3607  * peer, and remove all record of the association.
3608  *
3609  * The return value is the disposition.
3610  */
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)3611 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3612 					   const struct sctp_endpoint *ep,
3613 					   const struct sctp_association *asoc,
3614 					   const union sctp_subtype type,
3615 					   void *arg,
3616 					   struct sctp_cmd_seq *commands)
3617 {
3618 	struct sctp_chunk *chunk = arg;
3619 	struct sctp_chunk *reply;
3620 	struct sctp_ulpevent *ev;
3621 
3622 	if (!sctp_vtag_verify(chunk, asoc))
3623 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3624 
3625 	/* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3626 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3627 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3628 						  commands);
3629 	/* 10.2 H) SHUTDOWN COMPLETE notification
3630 	 *
3631 	 * When SCTP completes the shutdown procedures (section 9.2) this
3632 	 * notification is passed to the upper layer.
3633 	 */
3634 	ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3635 					     0, 0, 0, NULL, GFP_ATOMIC);
3636 	if (!ev)
3637 		goto nomem;
3638 
3639 	/* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3640 	reply = sctp_make_shutdown_complete(asoc, chunk);
3641 	if (!reply)
3642 		goto nomem_chunk;
3643 
3644 	/* Do all the commands now (after allocation), so that we
3645 	 * have consistent state if memory allocation fails
3646 	 */
3647 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3648 
3649 	/* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3650 	 * stop the T2-shutdown timer,
3651 	 */
3652 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3653 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3654 
3655 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3656 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3657 
3658 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3659 			SCTP_STATE(SCTP_STATE_CLOSED));
3660 	SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3661 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3662 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3663 
3664 	/* ...and remove all record of the association. */
3665 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3666 	return SCTP_DISPOSITION_DELETE_TCB;
3667 
3668 nomem_chunk:
3669 	sctp_ulpevent_free(ev);
3670 nomem:
3671 	return SCTP_DISPOSITION_NOMEM;
3672 }
3673 
3674 /*
3675  * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3676  *
3677  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3678  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3679  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3680  *    packet must fill in the Verification Tag field of the outbound
3681  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3682  *    set the T-bit in the Chunk Flags to indicate that the Verification
3683  *    Tag is reflected.
3684  *
3685  * 8) The receiver should respond to the sender of the OOTB packet with
3686  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3687  *    MUST fill in the Verification Tag field of the outbound packet
3688  *    with the value found in the Verification Tag field of the OOTB
3689  *    packet and set the T-bit in the Chunk Flags to indicate that the
3690  *    Verification Tag is reflected.  After sending this ABORT, the
3691  *    receiver of the OOTB packet shall discard the OOTB packet and take
3692  *    no further action.
3693  */
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)3694 enum sctp_disposition sctp_sf_ootb(struct net *net,
3695 				   const struct sctp_endpoint *ep,
3696 				   const struct sctp_association *asoc,
3697 				   const union sctp_subtype type,
3698 				   void *arg, struct sctp_cmd_seq *commands)
3699 {
3700 	struct sctp_chunk *chunk = arg;
3701 	struct sk_buff *skb = chunk->skb;
3702 	struct sctp_chunkhdr *ch;
3703 	struct sctp_errhdr *err;
3704 	int ootb_cookie_ack = 0;
3705 	int ootb_shut_ack = 0;
3706 	__u8 *ch_end;
3707 
3708 	SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3709 
3710 	if (asoc && !sctp_vtag_verify(chunk, asoc))
3711 		asoc = NULL;
3712 
3713 	ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3714 	do {
3715 		/* Report violation if the chunk is less then minimal */
3716 		if (ntohs(ch->length) < sizeof(*ch))
3717 			return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3718 						  commands);
3719 
3720 		/* Report violation if chunk len overflows */
3721 		ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3722 		if (ch_end > skb_tail_pointer(skb))
3723 			return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3724 						  commands);
3725 
3726 		/* Now that we know we at least have a chunk header,
3727 		 * do things that are type appropriate.
3728 		 */
3729 		if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3730 			ootb_shut_ack = 1;
3731 
3732 		/* RFC 2960, Section 3.3.7
3733 		 *   Moreover, under any circumstances, an endpoint that
3734 		 *   receives an ABORT  MUST NOT respond to that ABORT by
3735 		 *   sending an ABORT of its own.
3736 		 */
3737 		if (SCTP_CID_ABORT == ch->type)
3738 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3739 
3740 		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3741 		 * or a COOKIE ACK the SCTP Packet should be silently
3742 		 * discarded.
3743 		 */
3744 
3745 		if (SCTP_CID_COOKIE_ACK == ch->type)
3746 			ootb_cookie_ack = 1;
3747 
3748 		if (SCTP_CID_ERROR == ch->type) {
3749 			sctp_walk_errors(err, ch) {
3750 				if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3751 					ootb_cookie_ack = 1;
3752 					break;
3753 				}
3754 			}
3755 		}
3756 
3757 		ch = (struct sctp_chunkhdr *)ch_end;
3758 	} while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
3759 
3760 	if (ootb_shut_ack)
3761 		return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3762 	else if (ootb_cookie_ack)
3763 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3764 	else
3765 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3766 }
3767 
3768 /*
3769  * Handle an "Out of the blue" SHUTDOWN ACK.
3770  *
3771  * Section: 8.4 5, sctpimpguide 2.41.
3772  *
3773  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3774  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3775  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3776  *    packet must fill in the Verification Tag field of the outbound
3777  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3778  *    set the T-bit in the Chunk Flags to indicate that the Verification
3779  *    Tag is reflected.
3780  *
3781  * Inputs
3782  * (endpoint, asoc, type, arg, commands)
3783  *
3784  * Outputs
3785  * (enum sctp_disposition)
3786  *
3787  * The return value is the disposition of the chunk.
3788  */
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)3789 static enum sctp_disposition sctp_sf_shut_8_4_5(
3790 					struct net *net,
3791 					const struct sctp_endpoint *ep,
3792 					const struct sctp_association *asoc,
3793 					const union sctp_subtype type,
3794 					void *arg,
3795 					struct sctp_cmd_seq *commands)
3796 {
3797 	struct sctp_packet *packet = NULL;
3798 	struct sctp_chunk *chunk = arg;
3799 	struct sctp_chunk *shut;
3800 
3801 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
3802 	if (!packet)
3803 		return SCTP_DISPOSITION_NOMEM;
3804 
3805 	/* Make an SHUTDOWN_COMPLETE.
3806 	 * The T bit will be set if the asoc is NULL.
3807 	 */
3808 	shut = sctp_make_shutdown_complete(asoc, chunk);
3809 	if (!shut) {
3810 		sctp_ootb_pkt_free(packet);
3811 		return SCTP_DISPOSITION_NOMEM;
3812 	}
3813 
3814 	/* Reflect vtag if T-Bit is set */
3815 	if (sctp_test_T_bit(shut))
3816 		packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3817 
3818 	/* Set the skb to the belonging sock for accounting.  */
3819 	shut->skb->sk = ep->base.sk;
3820 
3821 	sctp_packet_append_chunk(packet, shut);
3822 
3823 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3824 			SCTP_PACKET(packet));
3825 
3826 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3827 
3828 	/* We need to discard the rest of the packet to prevent
3829 	 * potential boomming attacks from additional bundled chunks.
3830 	 * This is documented in SCTP Threats ID.
3831 	 */
3832 	return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3833 }
3834 
3835 /*
3836  * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3837  *
3838  * Verification Tag:  8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3839  *   If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3840  *   procedures in section 8.4 SHOULD be followed, in other words it
3841  *   should be treated as an Out Of The Blue packet.
3842  *   [This means that we do NOT check the Verification Tag on these
3843  *   chunks. --piggy ]
3844  *
3845  */
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)3846 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3847 					    const struct sctp_endpoint *ep,
3848 					    const struct sctp_association *asoc,
3849 					    const union sctp_subtype type,
3850 					    void *arg,
3851 					    struct sctp_cmd_seq *commands)
3852 {
3853 	struct sctp_chunk *chunk = arg;
3854 
3855 	if (!sctp_vtag_verify(chunk, asoc))
3856 		asoc = NULL;
3857 
3858 	/* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3859 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3860 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3861 						  commands);
3862 
3863 	/* Although we do have an association in this case, it corresponds
3864 	 * to a restarted association. So the packet is treated as an OOTB
3865 	 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3866 	 * called with a NULL association.
3867 	 */
3868 	SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3869 
3870 	return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3871 }
3872 
3873 /* 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)3874 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3875 					const struct sctp_endpoint *ep,
3876 					const struct sctp_association *asoc,
3877 					const union sctp_subtype type,
3878 					void *arg,
3879 					struct sctp_cmd_seq *commands)
3880 {
3881 	struct sctp_paramhdr *err_param = NULL;
3882 	struct sctp_chunk *asconf_ack = NULL;
3883 	struct sctp_chunk *chunk = arg;
3884 	struct sctp_addiphdr *hdr;
3885 	__u32 serial;
3886 
3887 	if (!sctp_vtag_verify(chunk, asoc)) {
3888 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3889 				SCTP_NULL());
3890 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3891 	}
3892 
3893 	/* Make sure that the ASCONF ADDIP chunk has a valid length.  */
3894 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3895 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3896 						  commands);
3897 
3898 	/* ADD-IP: Section 4.1.1
3899 	 * This chunk MUST be sent in an authenticated way by using
3900 	 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3901 	 * is received unauthenticated it MUST be silently discarded as
3902 	 * described in [I-D.ietf-tsvwg-sctp-auth].
3903 	 */
3904 	if (!asoc->peer.asconf_capable ||
3905 	    (!net->sctp.addip_noauth && !chunk->auth))
3906 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3907 
3908 	hdr = (struct sctp_addiphdr *)chunk->skb->data;
3909 	serial = ntohl(hdr->serial);
3910 
3911 	/* Verify the ASCONF chunk before processing it. */
3912 	if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3913 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3914 						  (void *)err_param, commands);
3915 
3916 	/* ADDIP 5.2 E1) Compare the value of the serial number to the value
3917 	 * the endpoint stored in a new association variable
3918 	 * 'Peer-Serial-Number'.
3919 	 */
3920 	if (serial == asoc->peer.addip_serial + 1) {
3921 		/* If this is the first instance of ASCONF in the packet,
3922 		 * we can clean our old ASCONF-ACKs.
3923 		 */
3924 		if (!chunk->has_asconf)
3925 			sctp_assoc_clean_asconf_ack_cache(asoc);
3926 
3927 		/* ADDIP 5.2 E4) When the Sequence Number matches the next one
3928 		 * expected, process the ASCONF as described below and after
3929 		 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3930 		 * the response packet and cache a copy of it (in the event it
3931 		 * later needs to be retransmitted).
3932 		 *
3933 		 * Essentially, do V1-V5.
3934 		 */
3935 		asconf_ack = sctp_process_asconf((struct sctp_association *)
3936 						 asoc, chunk);
3937 		if (!asconf_ack)
3938 			return SCTP_DISPOSITION_NOMEM;
3939 	} else if (serial < asoc->peer.addip_serial + 1) {
3940 		/* ADDIP 5.2 E2)
3941 		 * If the value found in the Sequence Number is less than the
3942 		 * ('Peer- Sequence-Number' + 1), simply skip to the next
3943 		 * ASCONF, and include in the outbound response packet
3944 		 * any previously cached ASCONF-ACK response that was
3945 		 * sent and saved that matches the Sequence Number of the
3946 		 * ASCONF.  Note: It is possible that no cached ASCONF-ACK
3947 		 * Chunk exists.  This will occur when an older ASCONF
3948 		 * arrives out of order.  In such a case, the receiver
3949 		 * should skip the ASCONF Chunk and not include ASCONF-ACK
3950 		 * Chunk for that chunk.
3951 		 */
3952 		asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3953 		if (!asconf_ack)
3954 			return SCTP_DISPOSITION_DISCARD;
3955 
3956 		/* Reset the transport so that we select the correct one
3957 		 * this time around.  This is to make sure that we don't
3958 		 * accidentally use a stale transport that's been removed.
3959 		 */
3960 		asconf_ack->transport = NULL;
3961 	} else {
3962 		/* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3963 		 * it must be either a stale packet or from an attacker.
3964 		 */
3965 		return SCTP_DISPOSITION_DISCARD;
3966 	}
3967 
3968 	/* ADDIP 5.2 E6)  The destination address of the SCTP packet
3969 	 * containing the ASCONF-ACK Chunks MUST be the source address of
3970 	 * the SCTP packet that held the ASCONF Chunks.
3971 	 *
3972 	 * To do this properly, we'll set the destination address of the chunk
3973 	 * and at the transmit time, will try look up the transport to use.
3974 	 * Since ASCONFs may be bundled, the correct transport may not be
3975 	 * created until we process the entire packet, thus this workaround.
3976 	 */
3977 	asconf_ack->dest = chunk->source;
3978 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3979 	if (asoc->new_transport) {
3980 		sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3981 		((struct sctp_association *)asoc)->new_transport = NULL;
3982 	}
3983 
3984 	return SCTP_DISPOSITION_CONSUME;
3985 }
3986 
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)3987 static enum sctp_disposition sctp_send_next_asconf(
3988 					struct net *net,
3989 					const struct sctp_endpoint *ep,
3990 					struct sctp_association *asoc,
3991 					const union sctp_subtype type,
3992 					struct sctp_cmd_seq *commands)
3993 {
3994 	struct sctp_chunk *asconf;
3995 	struct list_head *entry;
3996 
3997 	if (list_empty(&asoc->addip_chunk_list))
3998 		return SCTP_DISPOSITION_CONSUME;
3999 
4000 	entry = asoc->addip_chunk_list.next;
4001 	asconf = list_entry(entry, struct sctp_chunk, list);
4002 
4003 	list_del_init(entry);
4004 	sctp_chunk_hold(asconf);
4005 	asoc->addip_last_asconf = asconf;
4006 
4007 	return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
4008 }
4009 
4010 /*
4011  * ADDIP Section 4.3 General rules for address manipulation
4012  * When building TLV parameters for the ASCONF Chunk that will add or
4013  * delete IP addresses the D0 to D13 rules should be applied:
4014  */
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)4015 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
4016 					    const struct sctp_endpoint *ep,
4017 					    const struct sctp_association *asoc,
4018 					    const union sctp_subtype type,
4019 					    void *arg,
4020 					    struct sctp_cmd_seq *commands)
4021 {
4022 	struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
4023 	struct sctp_paramhdr *err_param = NULL;
4024 	struct sctp_chunk *asconf_ack = arg;
4025 	struct sctp_addiphdr *addip_hdr;
4026 	__u32 sent_serial, rcvd_serial;
4027 	struct sctp_chunk *abort;
4028 
4029 	if (!sctp_vtag_verify(asconf_ack, asoc)) {
4030 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4031 				SCTP_NULL());
4032 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4033 	}
4034 
4035 	/* Make sure that the ADDIP chunk has a valid length.  */
4036 	if (!sctp_chunk_length_valid(asconf_ack,
4037 				     sizeof(struct sctp_addip_chunk)))
4038 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4039 						  commands);
4040 
4041 	/* ADD-IP, Section 4.1.2:
4042 	 * This chunk MUST be sent in an authenticated way by using
4043 	 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
4044 	 * is received unauthenticated it MUST be silently discarded as
4045 	 * described in [I-D.ietf-tsvwg-sctp-auth].
4046 	 */
4047 	if (!asoc->peer.asconf_capable ||
4048 	    (!net->sctp.addip_noauth && !asconf_ack->auth))
4049 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4050 
4051 	addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
4052 	rcvd_serial = ntohl(addip_hdr->serial);
4053 
4054 	/* Verify the ASCONF-ACK chunk before processing it. */
4055 	if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
4056 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4057 			   (void *)err_param, commands);
4058 
4059 	if (last_asconf) {
4060 		addip_hdr = last_asconf->subh.addip_hdr;
4061 		sent_serial = ntohl(addip_hdr->serial);
4062 	} else {
4063 		sent_serial = asoc->addip_serial - 1;
4064 	}
4065 
4066 	/* D0) If an endpoint receives an ASCONF-ACK that is greater than or
4067 	 * equal to the next serial number to be used but no ASCONF chunk is
4068 	 * outstanding the endpoint MUST ABORT the association. Note that a
4069 	 * sequence number is greater than if it is no more than 2^^31-1
4070 	 * larger than the current sequence number (using serial arithmetic).
4071 	 */
4072 	if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
4073 	    !(asoc->addip_last_asconf)) {
4074 		abort = sctp_make_abort(asoc, asconf_ack,
4075 					sizeof(struct sctp_errhdr));
4076 		if (abort) {
4077 			sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
4078 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4079 					SCTP_CHUNK(abort));
4080 		}
4081 		/* We are going to ABORT, so we might as well stop
4082 		 * processing the rest of the chunks in the packet.
4083 		 */
4084 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4085 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4086 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4087 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4088 				SCTP_ERROR(ECONNABORTED));
4089 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4090 				SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4091 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4092 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4093 		return SCTP_DISPOSITION_ABORT;
4094 	}
4095 
4096 	if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
4097 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4098 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4099 
4100 		if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
4101 					     asconf_ack))
4102 			return sctp_send_next_asconf(net, ep,
4103 					(struct sctp_association *)asoc,
4104 							type, commands);
4105 
4106 		abort = sctp_make_abort(asoc, asconf_ack,
4107 					sizeof(struct sctp_errhdr));
4108 		if (abort) {
4109 			sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
4110 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4111 					SCTP_CHUNK(abort));
4112 		}
4113 		/* We are going to ABORT, so we might as well stop
4114 		 * processing the rest of the chunks in the packet.
4115 		 */
4116 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4117 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4118 				SCTP_ERROR(ECONNABORTED));
4119 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4120 				SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4121 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4122 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4123 		return SCTP_DISPOSITION_ABORT;
4124 	}
4125 
4126 	return SCTP_DISPOSITION_DISCARD;
4127 }
4128 
4129 /* 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)4130 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
4131 					const struct sctp_endpoint *ep,
4132 					const struct sctp_association *asoc,
4133 					const union sctp_subtype type,
4134 					void *arg,
4135 					struct sctp_cmd_seq *commands)
4136 {
4137 	struct sctp_paramhdr *err_param = NULL;
4138 	struct sctp_chunk *chunk = arg;
4139 	struct sctp_reconf_chunk *hdr;
4140 	union sctp_params param;
4141 
4142 	if (!sctp_vtag_verify(chunk, asoc)) {
4143 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4144 				SCTP_NULL());
4145 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4146 	}
4147 
4148 	/* Make sure that the RECONF chunk has a valid length.  */
4149 	if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
4150 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4151 						  commands);
4152 
4153 	if (!sctp_verify_reconf(asoc, chunk, &err_param))
4154 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4155 						  (void *)err_param, commands);
4156 
4157 	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
4158 	sctp_walk_params(param, hdr) {
4159 		struct sctp_chunk *reply = NULL;
4160 		struct sctp_ulpevent *ev = NULL;
4161 
4162 		if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4163 			reply = sctp_process_strreset_outreq(
4164 				(struct sctp_association *)asoc, param, &ev);
4165 		else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4166 			reply = sctp_process_strreset_inreq(
4167 				(struct sctp_association *)asoc, param, &ev);
4168 		else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4169 			reply = sctp_process_strreset_tsnreq(
4170 				(struct sctp_association *)asoc, param, &ev);
4171 		else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4172 			reply = sctp_process_strreset_addstrm_out(
4173 				(struct sctp_association *)asoc, param, &ev);
4174 		else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4175 			reply = sctp_process_strreset_addstrm_in(
4176 				(struct sctp_association *)asoc, param, &ev);
4177 		else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4178 			reply = sctp_process_strreset_resp(
4179 				(struct sctp_association *)asoc, param, &ev);
4180 
4181 		if (ev)
4182 			sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4183 					SCTP_ULPEVENT(ev));
4184 
4185 		if (reply)
4186 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4187 					SCTP_CHUNK(reply));
4188 	}
4189 
4190 	return SCTP_DISPOSITION_CONSUME;
4191 }
4192 
4193 /*
4194  * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4195  *
4196  * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4197  * its cumulative TSN point to the value carried in the FORWARD TSN
4198  * chunk, and then MUST further advance its cumulative TSN point locally
4199  * if possible.
4200  * After the above processing, the data receiver MUST stop reporting any
4201  * missing TSNs earlier than or equal to the new cumulative TSN point.
4202  *
4203  * Verification Tag:  8.5 Verification Tag [Normal verification]
4204  *
4205  * The return value is the disposition of the chunk.
4206  */
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)4207 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4208 					  const struct sctp_endpoint *ep,
4209 					  const struct sctp_association *asoc,
4210 					  const union sctp_subtype type,
4211 					  void *arg,
4212 					  struct sctp_cmd_seq *commands)
4213 {
4214 	struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4215 	struct sctp_chunk *chunk = arg;
4216 	__u16 len;
4217 	__u32 tsn;
4218 
4219 	if (!sctp_vtag_verify(chunk, asoc)) {
4220 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4221 				SCTP_NULL());
4222 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4223 	}
4224 
4225 	if (!asoc->peer.prsctp_capable)
4226 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4227 
4228 	/* Make sure that the FORWARD_TSN chunk has valid length.  */
4229 	if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4230 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4231 						  commands);
4232 
4233 	fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4234 	chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4235 	len = ntohs(chunk->chunk_hdr->length);
4236 	len -= sizeof(struct sctp_chunkhdr);
4237 	skb_pull(chunk->skb, len);
4238 
4239 	tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4240 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4241 
4242 	/* The TSN is too high--silently discard the chunk and count on it
4243 	 * getting retransmitted later.
4244 	 */
4245 	if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4246 		goto discard_noforce;
4247 
4248 	if (!asoc->stream.si->validate_ftsn(chunk))
4249 		goto discard_noforce;
4250 
4251 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4252 	if (len > sctp_ftsnhdr_len(&asoc->stream))
4253 		sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4254 				SCTP_CHUNK(chunk));
4255 
4256 	/* Count this as receiving DATA. */
4257 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4258 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4259 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4260 	}
4261 
4262 	/* FIXME: For now send a SACK, but DATA processing may
4263 	 * send another.
4264 	 */
4265 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4266 
4267 	return SCTP_DISPOSITION_CONSUME;
4268 
4269 discard_noforce:
4270 	return SCTP_DISPOSITION_DISCARD;
4271 }
4272 
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)4273 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4274 					struct net *net,
4275 					const struct sctp_endpoint *ep,
4276 					const struct sctp_association *asoc,
4277 					const union sctp_subtype type,
4278 					void *arg,
4279 					struct sctp_cmd_seq *commands)
4280 {
4281 	struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4282 	struct sctp_chunk *chunk = arg;
4283 	__u16 len;
4284 	__u32 tsn;
4285 
4286 	if (!sctp_vtag_verify(chunk, asoc)) {
4287 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4288 				SCTP_NULL());
4289 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4290 	}
4291 
4292 	if (!asoc->peer.prsctp_capable)
4293 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4294 
4295 	/* Make sure that the FORWARD_TSN chunk has a valid length.  */
4296 	if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4297 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4298 						  commands);
4299 
4300 	fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4301 	chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4302 	len = ntohs(chunk->chunk_hdr->length);
4303 	len -= sizeof(struct sctp_chunkhdr);
4304 	skb_pull(chunk->skb, len);
4305 
4306 	tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4307 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4308 
4309 	/* The TSN is too high--silently discard the chunk and count on it
4310 	 * getting retransmitted later.
4311 	 */
4312 	if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4313 		goto gen_shutdown;
4314 
4315 	if (!asoc->stream.si->validate_ftsn(chunk))
4316 		goto gen_shutdown;
4317 
4318 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4319 	if (len > sctp_ftsnhdr_len(&asoc->stream))
4320 		sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4321 				SCTP_CHUNK(chunk));
4322 
4323 	/* Go a head and force a SACK, since we are shutting down. */
4324 gen_shutdown:
4325 	/* Implementor's Guide.
4326 	 *
4327 	 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4328 	 * respond to each received packet containing one or more DATA chunk(s)
4329 	 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4330 	 */
4331 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4332 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4333 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4334 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4335 
4336 	return SCTP_DISPOSITION_CONSUME;
4337 }
4338 
4339 /*
4340  * SCTP-AUTH Section 6.3 Receiving authenticated chunks
4341  *
4342  *    The receiver MUST use the HMAC algorithm indicated in the HMAC
4343  *    Identifier field.  If this algorithm was not specified by the
4344  *    receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4345  *    during association setup, the AUTH chunk and all chunks after it MUST
4346  *    be discarded and an ERROR chunk SHOULD be sent with the error cause
4347  *    defined in Section 4.1.
4348  *
4349  *    If an endpoint with no shared key receives a Shared Key Identifier
4350  *    other than 0, it MUST silently discard all authenticated chunks.  If
4351  *    the endpoint has at least one endpoint pair shared key for the peer,
4352  *    it MUST use the key specified by the Shared Key Identifier if a
4353  *    key has been configured for that Shared Key Identifier.  If no
4354  *    endpoint pair shared key has been configured for that Shared Key
4355  *    Identifier, all authenticated chunks MUST be silently discarded.
4356  *
4357  * Verification Tag:  8.5 Verification Tag [Normal verification]
4358  *
4359  * The return value is the disposition of the chunk.
4360  */
sctp_sf_authenticate(const struct sctp_association * asoc,struct sctp_chunk * chunk)4361 static enum sctp_ierror sctp_sf_authenticate(
4362 					const struct sctp_association *asoc,
4363 					struct sctp_chunk *chunk)
4364 {
4365 	struct sctp_shared_key *sh_key = NULL;
4366 	struct sctp_authhdr *auth_hdr;
4367 	__u8 *save_digest, *digest;
4368 	const struct sctp_hmac *hmac;
4369 	unsigned int sig_len;
4370 	__u16 key_id;
4371 
4372 	/* Pull in the auth header, so we can do some more verification */
4373 	auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4374 	chunk->subh.auth_hdr = auth_hdr;
4375 	skb_pull(chunk->skb, sizeof(*auth_hdr));
4376 
4377 	/* Make sure that we support the HMAC algorithm from the auth
4378 	 * chunk.
4379 	 */
4380 	if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4381 		return SCTP_IERROR_AUTH_BAD_HMAC;
4382 
4383 	/* Make sure that the provided shared key identifier has been
4384 	 * configured
4385 	 */
4386 	key_id = ntohs(auth_hdr->shkey_id);
4387 	if (key_id != asoc->active_key_id) {
4388 		sh_key = sctp_auth_get_shkey(asoc, key_id);
4389 		if (!sh_key)
4390 			return SCTP_IERROR_AUTH_BAD_KEYID;
4391 	}
4392 
4393 	/* Make sure that the length of the signature matches what
4394 	 * we expect.
4395 	 */
4396 	sig_len = ntohs(chunk->chunk_hdr->length) -
4397 		  sizeof(struct sctp_auth_chunk);
4398 	hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4399 	if (sig_len != hmac->hmac_len)
4400 		return SCTP_IERROR_PROTO_VIOLATION;
4401 
4402 	/* Now that we've done validation checks, we can compute and
4403 	 * verify the hmac.  The steps involved are:
4404 	 *  1. Save the digest from the chunk.
4405 	 *  2. Zero out the digest in the chunk.
4406 	 *  3. Compute the new digest
4407 	 *  4. Compare saved and new digests.
4408 	 */
4409 	digest = (u8 *)(auth_hdr + 1);
4410 	skb_pull(chunk->skb, sig_len);
4411 
4412 	save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4413 	if (!save_digest)
4414 		goto nomem;
4415 
4416 	memset(digest, 0, sig_len);
4417 
4418 	sctp_auth_calculate_hmac(asoc, chunk->skb,
4419 				 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4420 				 sh_key, GFP_ATOMIC);
4421 
4422 	/* Discard the packet if the digests do not match */
4423 	if (crypto_memneq(save_digest, digest, sig_len)) {
4424 		kfree(save_digest);
4425 		return SCTP_IERROR_BAD_SIG;
4426 	}
4427 
4428 	kfree(save_digest);
4429 	chunk->auth = 1;
4430 
4431 	return SCTP_IERROR_NO_ERROR;
4432 nomem:
4433 	return SCTP_IERROR_NOMEM;
4434 }
4435 
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)4436 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4437 				       const struct sctp_endpoint *ep,
4438 				       const struct sctp_association *asoc,
4439 				       const union sctp_subtype type,
4440 				       void *arg, struct sctp_cmd_seq *commands)
4441 {
4442 	struct sctp_chunk *chunk = arg;
4443 	struct sctp_authhdr *auth_hdr;
4444 	struct sctp_chunk *err_chunk;
4445 	enum sctp_ierror error;
4446 
4447 	/* Make sure that the peer has AUTH capable */
4448 	if (!asoc->peer.auth_capable)
4449 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4450 
4451 	if (!sctp_vtag_verify(chunk, asoc)) {
4452 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4453 				SCTP_NULL());
4454 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4455 	}
4456 
4457 	/* Make sure that the AUTH chunk has valid length.  */
4458 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4459 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4460 						  commands);
4461 
4462 	auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4463 	error = sctp_sf_authenticate(asoc, chunk);
4464 	switch (error) {
4465 	case SCTP_IERROR_AUTH_BAD_HMAC:
4466 		/* Generate the ERROR chunk and discard the rest
4467 		 * of the packet
4468 		 */
4469 		err_chunk = sctp_make_op_error(asoc, chunk,
4470 					       SCTP_ERROR_UNSUP_HMAC,
4471 					       &auth_hdr->hmac_id,
4472 					       sizeof(__u16), 0);
4473 		if (err_chunk) {
4474 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4475 					SCTP_CHUNK(err_chunk));
4476 		}
4477 		fallthrough;
4478 	case SCTP_IERROR_AUTH_BAD_KEYID:
4479 	case SCTP_IERROR_BAD_SIG:
4480 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4481 
4482 	case SCTP_IERROR_PROTO_VIOLATION:
4483 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4484 						  commands);
4485 
4486 	case SCTP_IERROR_NOMEM:
4487 		return SCTP_DISPOSITION_NOMEM;
4488 
4489 	default:			/* Prevent gcc warnings */
4490 		break;
4491 	}
4492 
4493 	if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4494 		struct sctp_ulpevent *ev;
4495 
4496 		ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4497 				    SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4498 
4499 		if (!ev)
4500 			return SCTP_DISPOSITION_NOMEM;
4501 
4502 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4503 				SCTP_ULPEVENT(ev));
4504 	}
4505 
4506 	return SCTP_DISPOSITION_CONSUME;
4507 }
4508 
4509 /*
4510  * Process an unknown chunk.
4511  *
4512  * Section: 3.2. Also, 2.1 in the implementor's guide.
4513  *
4514  * Chunk Types are encoded such that the highest-order two bits specify
4515  * the action that must be taken if the processing endpoint does not
4516  * recognize the Chunk Type.
4517  *
4518  * 00 - Stop processing this SCTP packet and discard it, do not process
4519  *      any further chunks within it.
4520  *
4521  * 01 - Stop processing this SCTP packet and discard it, do not process
4522  *      any further chunks within it, and report the unrecognized
4523  *      chunk in an 'Unrecognized Chunk Type'.
4524  *
4525  * 10 - Skip this chunk and continue processing.
4526  *
4527  * 11 - Skip this chunk and continue processing, but report in an ERROR
4528  *      Chunk using the 'Unrecognized Chunk Type' cause of error.
4529  *
4530  * The return value is the disposition of the chunk.
4531  */
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)4532 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4533 					const struct sctp_endpoint *ep,
4534 					const struct sctp_association *asoc,
4535 					const union sctp_subtype type,
4536 					void *arg,
4537 					struct sctp_cmd_seq *commands)
4538 {
4539 	struct sctp_chunk *unk_chunk = arg;
4540 	struct sctp_chunk *err_chunk;
4541 	struct sctp_chunkhdr *hdr;
4542 
4543 	pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4544 
4545 	if (!sctp_vtag_verify(unk_chunk, asoc))
4546 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4547 
4548 	/* Make sure that the chunk has a valid length.
4549 	 * Since we don't know the chunk type, we use a general
4550 	 * chunkhdr structure to make a comparison.
4551 	 */
4552 	if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4553 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4554 						  commands);
4555 
4556 	switch (type.chunk & SCTP_CID_ACTION_MASK) {
4557 	case SCTP_CID_ACTION_DISCARD:
4558 		/* Discard the packet.  */
4559 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4560 	case SCTP_CID_ACTION_DISCARD_ERR:
4561 		/* Generate an ERROR chunk as response. */
4562 		hdr = unk_chunk->chunk_hdr;
4563 		err_chunk = sctp_make_op_error(asoc, unk_chunk,
4564 					       SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4565 					       SCTP_PAD4(ntohs(hdr->length)),
4566 					       0);
4567 		if (err_chunk) {
4568 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4569 					SCTP_CHUNK(err_chunk));
4570 		}
4571 
4572 		/* Discard the packet.  */
4573 		sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4574 		return SCTP_DISPOSITION_CONSUME;
4575 	case SCTP_CID_ACTION_SKIP:
4576 		/* Skip the chunk.  */
4577 		return SCTP_DISPOSITION_DISCARD;
4578 	case SCTP_CID_ACTION_SKIP_ERR:
4579 		/* Generate an ERROR chunk as response. */
4580 		hdr = unk_chunk->chunk_hdr;
4581 		err_chunk = sctp_make_op_error(asoc, unk_chunk,
4582 					       SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4583 					       SCTP_PAD4(ntohs(hdr->length)),
4584 					       0);
4585 		if (err_chunk) {
4586 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4587 					SCTP_CHUNK(err_chunk));
4588 		}
4589 		/* Skip the chunk.  */
4590 		return SCTP_DISPOSITION_CONSUME;
4591 	default:
4592 		break;
4593 	}
4594 
4595 	return SCTP_DISPOSITION_DISCARD;
4596 }
4597 
4598 /*
4599  * Discard the chunk.
4600  *
4601  * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4602  * [Too numerous to mention...]
4603  * Verification Tag: No verification needed.
4604  * Inputs
4605  * (endpoint, asoc, chunk)
4606  *
4607  * Outputs
4608  * (asoc, reply_msg, msg_up, timers, counters)
4609  *
4610  * The return value is the disposition of the chunk.
4611  */
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)4612 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4613 					    const struct sctp_endpoint *ep,
4614 					    const struct sctp_association *asoc,
4615 					    const union sctp_subtype type,
4616 					    void *arg,
4617 					    struct sctp_cmd_seq *commands)
4618 {
4619 	struct sctp_chunk *chunk = arg;
4620 
4621 	if (asoc && !sctp_vtag_verify(chunk, asoc))
4622 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4623 
4624 	/* Make sure that the chunk has a valid length.
4625 	 * Since we don't know the chunk type, we use a general
4626 	 * chunkhdr structure to make a comparison.
4627 	 */
4628 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4629 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4630 						  commands);
4631 
4632 	pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4633 
4634 	return SCTP_DISPOSITION_DISCARD;
4635 }
4636 
4637 /*
4638  * Discard the whole packet.
4639  *
4640  * Section: 8.4 2)
4641  *
4642  * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4643  *    silently discard the OOTB packet and take no further action.
4644  *
4645  * Verification Tag: No verification necessary
4646  *
4647  * Inputs
4648  * (endpoint, asoc, chunk)
4649  *
4650  * Outputs
4651  * (asoc, reply_msg, msg_up, timers, counters)
4652  *
4653  * The return value is the disposition of the chunk.
4654  */
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)4655 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4656 				       const struct sctp_endpoint *ep,
4657 				       const struct sctp_association *asoc,
4658 				       const union sctp_subtype type,
4659 				       void *arg, struct sctp_cmd_seq *commands)
4660 {
4661 	SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4662 	sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4663 
4664 	return SCTP_DISPOSITION_CONSUME;
4665 }
4666 
4667 
4668 /*
4669  * The other end is violating protocol.
4670  *
4671  * Section: Not specified
4672  * Verification Tag: Not specified
4673  * Inputs
4674  * (endpoint, asoc, chunk)
4675  *
4676  * Outputs
4677  * (asoc, reply_msg, msg_up, timers, counters)
4678  *
4679  * We simply tag the chunk as a violation.  The state machine will log
4680  * the violation and continue.
4681  */
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)4682 enum sctp_disposition sctp_sf_violation(struct net *net,
4683 					const struct sctp_endpoint *ep,
4684 					const struct sctp_association *asoc,
4685 					const union sctp_subtype type,
4686 					void *arg,
4687 					struct sctp_cmd_seq *commands)
4688 {
4689 	struct sctp_chunk *chunk = arg;
4690 
4691 	if (!sctp_vtag_verify(chunk, asoc))
4692 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4693 
4694 	/* Make sure that the chunk has a valid length. */
4695 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4696 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4697 						  commands);
4698 
4699 	return SCTP_DISPOSITION_VIOLATION;
4700 }
4701 
4702 /*
4703  * Common function to handle a protocol violation.
4704  */
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)4705 static enum sctp_disposition sctp_sf_abort_violation(
4706 					struct net *net,
4707 					const struct sctp_endpoint *ep,
4708 					const struct sctp_association *asoc,
4709 					void *arg,
4710 					struct sctp_cmd_seq *commands,
4711 					const __u8 *payload,
4712 					const size_t paylen)
4713 {
4714 	struct sctp_packet *packet = NULL;
4715 	struct sctp_chunk *chunk =  arg;
4716 	struct sctp_chunk *abort = NULL;
4717 
4718 	/* SCTP-AUTH, Section 6.3:
4719 	 *    It should be noted that if the receiver wants to tear
4720 	 *    down an association in an authenticated way only, the
4721 	 *    handling of malformed packets should not result in
4722 	 *    tearing down the association.
4723 	 *
4724 	 * This means that if we only want to abort associations
4725 	 * in an authenticated way (i.e AUTH+ABORT), then we
4726 	 * can't destroy this association just because the packet
4727 	 * was malformed.
4728 	 */
4729 	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4730 		goto discard;
4731 
4732 	/* Make the abort chunk. */
4733 	abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4734 	if (!abort)
4735 		goto nomem;
4736 
4737 	if (asoc) {
4738 		/* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4739 		if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4740 		    !asoc->peer.i.init_tag) {
4741 			struct sctp_initack_chunk *initack;
4742 
4743 			initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4744 			if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4745 				abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4746 			else {
4747 				unsigned int inittag;
4748 
4749 				inittag = ntohl(initack->init_hdr.init_tag);
4750 				sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4751 						SCTP_U32(inittag));
4752 			}
4753 		}
4754 
4755 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4756 		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4757 
4758 		if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4759 			sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4760 					SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4761 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4762 					SCTP_ERROR(ECONNREFUSED));
4763 			sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4764 					SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4765 		} else {
4766 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4767 					SCTP_ERROR(ECONNABORTED));
4768 			sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4769 					SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4770 			SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4771 		}
4772 	} else {
4773 		packet = sctp_ootb_pkt_new(net, asoc, chunk);
4774 
4775 		if (!packet)
4776 			goto nomem_pkt;
4777 
4778 		if (sctp_test_T_bit(abort))
4779 			packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4780 
4781 		abort->skb->sk = ep->base.sk;
4782 
4783 		sctp_packet_append_chunk(packet, abort);
4784 
4785 		sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4786 			SCTP_PACKET(packet));
4787 
4788 		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4789 	}
4790 
4791 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4792 
4793 discard:
4794 	sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4795 	return SCTP_DISPOSITION_ABORT;
4796 
4797 nomem_pkt:
4798 	sctp_chunk_free(abort);
4799 nomem:
4800 	return SCTP_DISPOSITION_NOMEM;
4801 }
4802 
4803 /*
4804  * Handle a protocol violation when the chunk length is invalid.
4805  * "Invalid" length is identified as smaller than the minimal length a
4806  * given chunk can be.  For example, a SACK chunk has invalid length
4807  * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4808  *
4809  * We inform the other end by sending an ABORT with a Protocol Violation
4810  * error code.
4811  *
4812  * Section: Not specified
4813  * Verification Tag:  Nothing to do
4814  * Inputs
4815  * (endpoint, asoc, chunk)
4816  *
4817  * Outputs
4818  * (reply_msg, msg_up, counters)
4819  *
4820  * Generate an  ABORT chunk and terminate the association.
4821  */
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)4822 static enum sctp_disposition sctp_sf_violation_chunklen(
4823 					struct net *net,
4824 					const struct sctp_endpoint *ep,
4825 					const struct sctp_association *asoc,
4826 					const union sctp_subtype type,
4827 					void *arg,
4828 					struct sctp_cmd_seq *commands)
4829 {
4830 	static const char err_str[] = "The following chunk had invalid length:";
4831 
4832 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4833 				       sizeof(err_str));
4834 }
4835 
4836 /*
4837  * Handle a protocol violation when the parameter length is invalid.
4838  * If the length is smaller than the minimum length of a given parameter,
4839  * or accumulated length in multi parameters exceeds the end of the chunk,
4840  * the length is considered as invalid.
4841  */
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)4842 static enum sctp_disposition sctp_sf_violation_paramlen(
4843 					struct net *net,
4844 					const struct sctp_endpoint *ep,
4845 					const struct sctp_association *asoc,
4846 					const union sctp_subtype type,
4847 					void *arg, void *ext,
4848 					struct sctp_cmd_seq *commands)
4849 {
4850 	struct sctp_paramhdr *param = ext;
4851 	struct sctp_chunk *abort = NULL;
4852 	struct sctp_chunk *chunk = arg;
4853 
4854 	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4855 		goto discard;
4856 
4857 	/* Make the abort chunk. */
4858 	abort = sctp_make_violation_paramlen(asoc, chunk, param);
4859 	if (!abort)
4860 		goto nomem;
4861 
4862 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4863 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4864 
4865 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4866 			SCTP_ERROR(ECONNABORTED));
4867 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4868 			SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4869 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4870 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4871 
4872 discard:
4873 	sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4874 	return SCTP_DISPOSITION_ABORT;
4875 nomem:
4876 	return SCTP_DISPOSITION_NOMEM;
4877 }
4878 
4879 /* Handle a protocol violation when the peer trying to advance the
4880  * cumulative tsn ack to a point beyond the max tsn currently sent.
4881  *
4882  * We inform the other end by sending an ABORT with a Protocol Violation
4883  * error code.
4884  */
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)4885 static enum sctp_disposition sctp_sf_violation_ctsn(
4886 					struct net *net,
4887 					const struct sctp_endpoint *ep,
4888 					const struct sctp_association *asoc,
4889 					const union sctp_subtype type,
4890 					void *arg,
4891 					struct sctp_cmd_seq *commands)
4892 {
4893 	static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4894 
4895 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4896 				       sizeof(err_str));
4897 }
4898 
4899 /* Handle protocol violation of an invalid chunk bundling.  For example,
4900  * when we have an association and we receive bundled INIT-ACK, or
4901  * SHUTDOWN-COMPLETE, our peer is clearly violating the "MUST NOT bundle"
4902  * statement from the specs.  Additionally, there might be an attacker
4903  * on the path and we may not want to continue this communication.
4904  */
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)4905 static enum sctp_disposition sctp_sf_violation_chunk(
4906 					struct net *net,
4907 					const struct sctp_endpoint *ep,
4908 					const struct sctp_association *asoc,
4909 					const union sctp_subtype type,
4910 					void *arg,
4911 					struct sctp_cmd_seq *commands)
4912 {
4913 	static const char err_str[] = "The following chunk violates protocol:";
4914 
4915 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4916 				       sizeof(err_str));
4917 }
4918 /***************************************************************************
4919  * These are the state functions for handling primitive (Section 10) events.
4920  ***************************************************************************/
4921 /*
4922  * sctp_sf_do_prm_asoc
4923  *
4924  * Section: 10.1 ULP-to-SCTP
4925  * B) Associate
4926  *
4927  * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4928  * outbound stream count)
4929  * -> association id [,destination transport addr list] [,outbound stream
4930  * count]
4931  *
4932  * This primitive allows the upper layer to initiate an association to a
4933  * specific peer endpoint.
4934  *
4935  * The peer endpoint shall be specified by one of the transport addresses
4936  * which defines the endpoint (see Section 1.4).  If the local SCTP
4937  * instance has not been initialized, the ASSOCIATE is considered an
4938  * error.
4939  * [This is not relevant for the kernel implementation since we do all
4940  * initialization at boot time.  It we hadn't initialized we wouldn't
4941  * get anywhere near this code.]
4942  *
4943  * An association id, which is a local handle to the SCTP association,
4944  * will be returned on successful establishment of the association. If
4945  * SCTP is not able to open an SCTP association with the peer endpoint,
4946  * an error is returned.
4947  * [In the kernel implementation, the struct sctp_association needs to
4948  * be created BEFORE causing this primitive to run.]
4949  *
4950  * Other association parameters may be returned, including the
4951  * complete destination transport addresses of the peer as well as the
4952  * outbound stream count of the local endpoint. One of the transport
4953  * address from the returned destination addresses will be selected by
4954  * the local endpoint as default primary path for sending SCTP packets
4955  * to this peer.  The returned "destination transport addr list" can
4956  * be used by the ULP to change the default primary path or to force
4957  * sending a packet to a specific transport address.  [All of this
4958  * stuff happens when the INIT ACK arrives.  This is a NON-BLOCKING
4959  * function.]
4960  *
4961  * Mandatory attributes:
4962  *
4963  * o local SCTP instance name - obtained from the INITIALIZE operation.
4964  *   [This is the argument asoc.]
4965  * o destination transport addr - specified as one of the transport
4966  * addresses of the peer endpoint with which the association is to be
4967  * established.
4968  *  [This is asoc->peer.active_path.]
4969  * o outbound stream count - the number of outbound streams the ULP
4970  * would like to open towards this peer endpoint.
4971  * [BUG: This is not currently implemented.]
4972  * Optional attributes:
4973  *
4974  * None.
4975  *
4976  * The return value is a disposition.
4977  */
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)4978 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4979 					  const struct sctp_endpoint *ep,
4980 					  const struct sctp_association *asoc,
4981 					  const union sctp_subtype type,
4982 					  void *arg,
4983 					  struct sctp_cmd_seq *commands)
4984 {
4985 	struct sctp_association *my_asoc;
4986 	struct sctp_chunk *repl;
4987 
4988 	/* The comment below says that we enter COOKIE-WAIT AFTER
4989 	 * sending the INIT, but that doesn't actually work in our
4990 	 * implementation...
4991 	 */
4992 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4993 			SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4994 
4995 	/* RFC 2960 5.1 Normal Establishment of an Association
4996 	 *
4997 	 * A) "A" first sends an INIT chunk to "Z".  In the INIT, "A"
4998 	 * must provide its Verification Tag (Tag_A) in the Initiate
4999 	 * Tag field.  Tag_A SHOULD be a random number in the range of
5000 	 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
5001 	 */
5002 
5003 	repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
5004 	if (!repl)
5005 		goto nomem;
5006 
5007 	/* Choose transport for INIT. */
5008 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5009 			SCTP_CHUNK(repl));
5010 
5011 	/* Cast away the const modifier, as we want to just
5012 	 * rerun it through as a sideffect.
5013 	 */
5014 	my_asoc = (struct sctp_association *)asoc;
5015 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
5016 
5017 	/* After sending the INIT, "A" starts the T1-init timer and
5018 	 * enters the COOKIE-WAIT state.
5019 	 */
5020 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5021 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5022 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5023 	return SCTP_DISPOSITION_CONSUME;
5024 
5025 nomem:
5026 	return SCTP_DISPOSITION_NOMEM;
5027 }
5028 
5029 /*
5030  * Process the SEND primitive.
5031  *
5032  * Section: 10.1 ULP-to-SCTP
5033  * E) Send
5034  *
5035  * Format: SEND(association id, buffer address, byte count [,context]
5036  *         [,stream id] [,life time] [,destination transport address]
5037  *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
5038  * -> result
5039  *
5040  * This is the main method to send user data via SCTP.
5041  *
5042  * Mandatory attributes:
5043  *
5044  *  o association id - local handle to the SCTP association
5045  *
5046  *  o buffer address - the location where the user message to be
5047  *    transmitted is stored;
5048  *
5049  *  o byte count - The size of the user data in number of bytes;
5050  *
5051  * Optional attributes:
5052  *
5053  *  o context - an optional 32 bit integer that will be carried in the
5054  *    sending failure notification to the ULP if the transportation of
5055  *    this User Message fails.
5056  *
5057  *  o stream id - to indicate which stream to send the data on. If not
5058  *    specified, stream 0 will be used.
5059  *
5060  *  o life time - specifies the life time of the user data. The user data
5061  *    will not be sent by SCTP after the life time expires. This
5062  *    parameter can be used to avoid efforts to transmit stale
5063  *    user messages. SCTP notifies the ULP if the data cannot be
5064  *    initiated to transport (i.e. sent to the destination via SCTP's
5065  *    send primitive) within the life time variable. However, the
5066  *    user data will be transmitted if SCTP has attempted to transmit a
5067  *    chunk before the life time expired.
5068  *
5069  *  o destination transport address - specified as one of the destination
5070  *    transport addresses of the peer endpoint to which this packet
5071  *    should be sent. Whenever possible, SCTP should use this destination
5072  *    transport address for sending the packets, instead of the current
5073  *    primary path.
5074  *
5075  *  o unorder flag - this flag, if present, indicates that the user
5076  *    would like the data delivered in an unordered fashion to the peer
5077  *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
5078  *    message).
5079  *
5080  *  o no-bundle flag - instructs SCTP not to bundle this user data with
5081  *    other outbound DATA chunks. SCTP MAY still bundle even when
5082  *    this flag is present, when faced with network congestion.
5083  *
5084  *  o payload protocol-id - A 32 bit unsigned integer that is to be
5085  *    passed to the peer indicating the type of payload protocol data
5086  *    being transmitted. This value is passed as opaque data by SCTP.
5087  *
5088  * The return value is the disposition.
5089  */
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)5090 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
5091 					  const struct sctp_endpoint *ep,
5092 					  const struct sctp_association *asoc,
5093 					  const union sctp_subtype type,
5094 					  void *arg,
5095 					  struct sctp_cmd_seq *commands)
5096 {
5097 	struct sctp_datamsg *msg = arg;
5098 
5099 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
5100 	return SCTP_DISPOSITION_CONSUME;
5101 }
5102 
5103 /*
5104  * Process the SHUTDOWN primitive.
5105  *
5106  * Section: 10.1:
5107  * C) Shutdown
5108  *
5109  * Format: SHUTDOWN(association id)
5110  * -> result
5111  *
5112  * Gracefully closes an association. Any locally queued user data
5113  * will be delivered to the peer. The association will be terminated only
5114  * after the peer acknowledges all the SCTP packets sent.  A success code
5115  * will be returned on successful termination of the association. If
5116  * attempting to terminate the association results in a failure, an error
5117  * code shall be returned.
5118  *
5119  * Mandatory attributes:
5120  *
5121  *  o association id - local handle to the SCTP association
5122  *
5123  * Optional attributes:
5124  *
5125  * None.
5126  *
5127  * The return value is the disposition.
5128  */
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)5129 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
5130 					struct net *net,
5131 					const struct sctp_endpoint *ep,
5132 					const struct sctp_association *asoc,
5133 					const union sctp_subtype type,
5134 					void *arg,
5135 					struct sctp_cmd_seq *commands)
5136 {
5137 	enum sctp_disposition disposition;
5138 
5139 	/* From 9.2 Shutdown of an Association
5140 	 * Upon receipt of the SHUTDOWN primitive from its upper
5141 	 * layer, the endpoint enters SHUTDOWN-PENDING state and
5142 	 * remains there until all outstanding data has been
5143 	 * acknowledged by its peer. The endpoint accepts no new data
5144 	 * from its upper layer, but retransmits data to the far end
5145 	 * if necessary to fill gaps.
5146 	 */
5147 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5148 			SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5149 
5150 	disposition = SCTP_DISPOSITION_CONSUME;
5151 	if (sctp_outq_is_empty(&asoc->outqueue)) {
5152 		disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5153 							    arg, commands);
5154 	}
5155 
5156 	return disposition;
5157 }
5158 
5159 /*
5160  * Process the ABORT primitive.
5161  *
5162  * Section: 10.1:
5163  * C) Abort
5164  *
5165  * Format: Abort(association id [, cause code])
5166  * -> result
5167  *
5168  * Ungracefully closes an association. Any locally queued user data
5169  * will be discarded and an ABORT chunk is sent to the peer.  A success code
5170  * will be returned on successful abortion of the association. If
5171  * attempting to abort the association results in a failure, an error
5172  * code shall be returned.
5173  *
5174  * Mandatory attributes:
5175  *
5176  *  o association id - local handle to the SCTP association
5177  *
5178  * Optional attributes:
5179  *
5180  *  o cause code - reason of the abort to be passed to the peer
5181  *
5182  * None.
5183  *
5184  * The return value is the disposition.
5185  */
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)5186 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5187 					struct net *net,
5188 					const struct sctp_endpoint *ep,
5189 					const struct sctp_association *asoc,
5190 					const union sctp_subtype type,
5191 					void *arg,
5192 					struct sctp_cmd_seq *commands)
5193 {
5194 	/* From 9.1 Abort of an Association
5195 	 * Upon receipt of the ABORT primitive from its upper
5196 	 * layer, the endpoint enters CLOSED state and
5197 	 * discard all outstanding data has been
5198 	 * acknowledged by its peer. The endpoint accepts no new data
5199 	 * from its upper layer, but retransmits data to the far end
5200 	 * if necessary to fill gaps.
5201 	 */
5202 	struct sctp_chunk *abort = arg;
5203 
5204 	if (abort)
5205 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5206 
5207 	/* Even if we can't send the ABORT due to low memory delete the
5208 	 * TCB.  This is a departure from our typical NOMEM handling.
5209 	 */
5210 
5211 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5212 			SCTP_ERROR(ECONNABORTED));
5213 	/* Delete the established association. */
5214 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5215 			SCTP_PERR(SCTP_ERROR_USER_ABORT));
5216 
5217 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5218 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5219 
5220 	return SCTP_DISPOSITION_ABORT;
5221 }
5222 
5223 /* 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)5224 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5225 					   const struct sctp_endpoint *ep,
5226 					   const struct sctp_association *asoc,
5227 					   const union sctp_subtype type,
5228 					   void *arg,
5229 					   struct sctp_cmd_seq *commands)
5230 {
5231 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5232 	return SCTP_DISPOSITION_CONSUME;
5233 }
5234 
5235 /* We tried an illegal operation on an association which is shutting
5236  * down.
5237  */
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)5238 enum sctp_disposition sctp_sf_error_shutdown(
5239 					struct net *net,
5240 					const struct sctp_endpoint *ep,
5241 					const struct sctp_association *asoc,
5242 					const union sctp_subtype type,
5243 					void *arg,
5244 					struct sctp_cmd_seq *commands)
5245 {
5246 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5247 			SCTP_ERROR(-ESHUTDOWN));
5248 	return SCTP_DISPOSITION_CONSUME;
5249 }
5250 
5251 /*
5252  * sctp_cookie_wait_prm_shutdown
5253  *
5254  * Section: 4 Note: 2
5255  * Verification Tag:
5256  * Inputs
5257  * (endpoint, asoc)
5258  *
5259  * The RFC does not explicitly address this issue, but is the route through the
5260  * state table when someone issues a shutdown while in COOKIE_WAIT state.
5261  *
5262  * Outputs
5263  * (timers)
5264  */
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)5265 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5266 					struct net *net,
5267 					const struct sctp_endpoint *ep,
5268 					const struct sctp_association *asoc,
5269 					const union sctp_subtype type,
5270 					void *arg,
5271 					struct sctp_cmd_seq *commands)
5272 {
5273 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5274 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5275 
5276 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5277 			SCTP_STATE(SCTP_STATE_CLOSED));
5278 
5279 	SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5280 
5281 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5282 
5283 	return SCTP_DISPOSITION_DELETE_TCB;
5284 }
5285 
5286 /*
5287  * sctp_cookie_echoed_prm_shutdown
5288  *
5289  * Section: 4 Note: 2
5290  * Verification Tag:
5291  * Inputs
5292  * (endpoint, asoc)
5293  *
5294  * The RFC does not explicitly address this issue, but is the route through the
5295  * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5296  *
5297  * Outputs
5298  * (timers)
5299  */
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)5300 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5301 					struct net *net,
5302 					const struct sctp_endpoint *ep,
5303 					const struct sctp_association *asoc,
5304 					const union sctp_subtype type,
5305 					void *arg,
5306 					struct sctp_cmd_seq *commands)
5307 {
5308 	/* There is a single T1 timer, so we should be able to use
5309 	 * common function with the COOKIE-WAIT state.
5310 	 */
5311 	return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5312 }
5313 
5314 /*
5315  * sctp_sf_cookie_wait_prm_abort
5316  *
5317  * Section: 4 Note: 2
5318  * Verification Tag:
5319  * Inputs
5320  * (endpoint, asoc)
5321  *
5322  * The RFC does not explicitly address this issue, but is the route through the
5323  * state table when someone issues an abort while in COOKIE_WAIT state.
5324  *
5325  * Outputs
5326  * (timers)
5327  */
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)5328 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5329 					struct net *net,
5330 					const struct sctp_endpoint *ep,
5331 					const struct sctp_association *asoc,
5332 					const union sctp_subtype type,
5333 					void *arg,
5334 					struct sctp_cmd_seq *commands)
5335 {
5336 	struct sctp_chunk *abort = arg;
5337 
5338 	/* Stop T1-init timer */
5339 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5340 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5341 
5342 	if (abort)
5343 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5344 
5345 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5346 			SCTP_STATE(SCTP_STATE_CLOSED));
5347 
5348 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5349 
5350 	/* Even if we can't send the ABORT due to low memory delete the
5351 	 * TCB.  This is a departure from our typical NOMEM handling.
5352 	 */
5353 
5354 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5355 			SCTP_ERROR(ECONNREFUSED));
5356 	/* Delete the established association. */
5357 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5358 			SCTP_PERR(SCTP_ERROR_USER_ABORT));
5359 
5360 	return SCTP_DISPOSITION_ABORT;
5361 }
5362 
5363 /*
5364  * sctp_sf_cookie_echoed_prm_abort
5365  *
5366  * Section: 4 Note: 3
5367  * Verification Tag:
5368  * Inputs
5369  * (endpoint, asoc)
5370  *
5371  * The RFC does not explcitly address this issue, but is the route through the
5372  * state table when someone issues an abort while in COOKIE_ECHOED state.
5373  *
5374  * Outputs
5375  * (timers)
5376  */
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)5377 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5378 					struct net *net,
5379 					const struct sctp_endpoint *ep,
5380 					const struct sctp_association *asoc,
5381 					const union sctp_subtype type,
5382 					void *arg,
5383 					struct sctp_cmd_seq *commands)
5384 {
5385 	/* There is a single T1 timer, so we should be able to use
5386 	 * common function with the COOKIE-WAIT state.
5387 	 */
5388 	return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5389 }
5390 
5391 /*
5392  * sctp_sf_shutdown_pending_prm_abort
5393  *
5394  * Inputs
5395  * (endpoint, asoc)
5396  *
5397  * The RFC does not explicitly address this issue, but is the route through the
5398  * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5399  *
5400  * Outputs
5401  * (timers)
5402  */
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)5403 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5404 					struct net *net,
5405 					const struct sctp_endpoint *ep,
5406 					const struct sctp_association *asoc,
5407 					const union sctp_subtype type,
5408 					void *arg,
5409 					struct sctp_cmd_seq *commands)
5410 {
5411 	/* Stop the T5-shutdown guard timer.  */
5412 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5413 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5414 
5415 	return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5416 }
5417 
5418 /*
5419  * sctp_sf_shutdown_sent_prm_abort
5420  *
5421  * Inputs
5422  * (endpoint, asoc)
5423  *
5424  * The RFC does not explicitly address this issue, but is the route through the
5425  * state table when someone issues an abort while in SHUTDOWN-SENT state.
5426  *
5427  * Outputs
5428  * (timers)
5429  */
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)5430 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5431 					struct net *net,
5432 					const struct sctp_endpoint *ep,
5433 					const struct sctp_association *asoc,
5434 					const union sctp_subtype type,
5435 					void *arg,
5436 					struct sctp_cmd_seq *commands)
5437 {
5438 	/* Stop the T2-shutdown timer.  */
5439 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5440 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5441 
5442 	/* Stop the T5-shutdown guard timer.  */
5443 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5444 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5445 
5446 	return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5447 }
5448 
5449 /*
5450  * sctp_sf_cookie_echoed_prm_abort
5451  *
5452  * Inputs
5453  * (endpoint, asoc)
5454  *
5455  * The RFC does not explcitly address this issue, but is the route through the
5456  * state table when someone issues an abort while in COOKIE_ECHOED state.
5457  *
5458  * Outputs
5459  * (timers)
5460  */
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)5461 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5462 					struct net *net,
5463 					const struct sctp_endpoint *ep,
5464 					const struct sctp_association *asoc,
5465 					const union sctp_subtype type,
5466 					void *arg,
5467 					struct sctp_cmd_seq *commands)
5468 {
5469 	/* The same T2 timer, so we should be able to use
5470 	 * common function with the SHUTDOWN-SENT state.
5471 	 */
5472 	return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5473 }
5474 
5475 /*
5476  * Process the REQUESTHEARTBEAT primitive
5477  *
5478  * 10.1 ULP-to-SCTP
5479  * J) Request Heartbeat
5480  *
5481  * Format: REQUESTHEARTBEAT(association id, destination transport address)
5482  *
5483  * -> result
5484  *
5485  * Instructs the local endpoint to perform a HeartBeat on the specified
5486  * destination transport address of the given association. The returned
5487  * result should indicate whether the transmission of the HEARTBEAT
5488  * chunk to the destination address is successful.
5489  *
5490  * Mandatory attributes:
5491  *
5492  * o association id - local handle to the SCTP association
5493  *
5494  * o destination transport address - the transport address of the
5495  *   association on which a heartbeat should be issued.
5496  */
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)5497 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5498 					struct net *net,
5499 					const struct sctp_endpoint *ep,
5500 					const struct sctp_association *asoc,
5501 					const union sctp_subtype type,
5502 					void *arg,
5503 					struct sctp_cmd_seq *commands)
5504 {
5505 	if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5506 				      (struct sctp_transport *)arg, commands))
5507 		return SCTP_DISPOSITION_NOMEM;
5508 
5509 	/*
5510 	 * RFC 2960 (bis), section 8.3
5511 	 *
5512 	 *    D) Request an on-demand HEARTBEAT on a specific destination
5513 	 *    transport address of a given association.
5514 	 *
5515 	 *    The endpoint should increment the respective error  counter of
5516 	 *    the destination transport address each time a HEARTBEAT is sent
5517 	 *    to that address and not acknowledged within one RTO.
5518 	 *
5519 	 */
5520 	sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5521 			SCTP_TRANSPORT(arg));
5522 	return SCTP_DISPOSITION_CONSUME;
5523 }
5524 
5525 /*
5526  * ADDIP Section 4.1 ASCONF Chunk Procedures
5527  * When an endpoint has an ASCONF signaled change to be sent to the
5528  * remote endpoint it should do A1 to A9
5529  */
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)5530 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5531 					    const struct sctp_endpoint *ep,
5532 					    const struct sctp_association *asoc,
5533 					    const union sctp_subtype type,
5534 					    void *arg,
5535 					    struct sctp_cmd_seq *commands)
5536 {
5537 	struct sctp_chunk *chunk = arg;
5538 
5539 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5540 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5541 			SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5542 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5543 	return SCTP_DISPOSITION_CONSUME;
5544 }
5545 
5546 /* 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)5547 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5548 					    const struct sctp_endpoint *ep,
5549 					    const struct sctp_association *asoc,
5550 					    const union sctp_subtype type,
5551 					    void *arg,
5552 					    struct sctp_cmd_seq *commands)
5553 {
5554 	struct sctp_chunk *chunk = arg;
5555 
5556 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5557 	return SCTP_DISPOSITION_CONSUME;
5558 }
5559 
5560 /*
5561  * Ignore the primitive event
5562  *
5563  * The return value is the disposition of the primitive.
5564  */
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)5565 enum sctp_disposition sctp_sf_ignore_primitive(
5566 					struct net *net,
5567 					const struct sctp_endpoint *ep,
5568 					const struct sctp_association *asoc,
5569 					const union sctp_subtype type,
5570 					void *arg,
5571 					struct sctp_cmd_seq *commands)
5572 {
5573 	pr_debug("%s: primitive type:%d is ignored\n", __func__,
5574 		 type.primitive);
5575 
5576 	return SCTP_DISPOSITION_DISCARD;
5577 }
5578 
5579 /***************************************************************************
5580  * These are the state functions for the OTHER events.
5581  ***************************************************************************/
5582 
5583 /*
5584  * When the SCTP stack has no more user data to send or retransmit, this
5585  * notification is given to the user. Also, at the time when a user app
5586  * subscribes to this event, if there is no data to be sent or
5587  * retransmit, the stack will immediately send up this notification.
5588  */
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)5589 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5590 					struct net *net,
5591 					const struct sctp_endpoint *ep,
5592 					const struct sctp_association *asoc,
5593 					const union sctp_subtype type,
5594 					void *arg,
5595 					struct sctp_cmd_seq *commands)
5596 {
5597 	struct sctp_ulpevent *event;
5598 
5599 	event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5600 	if (!event)
5601 		return SCTP_DISPOSITION_NOMEM;
5602 
5603 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5604 
5605 	return SCTP_DISPOSITION_CONSUME;
5606 }
5607 
5608 /*
5609  * Start the shutdown negotiation.
5610  *
5611  * From Section 9.2:
5612  * Once all its outstanding data has been acknowledged, the endpoint
5613  * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5614  * TSN Ack field the last sequential TSN it has received from the peer.
5615  * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5616  * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5617  * with the updated last sequential TSN received from its peer.
5618  *
5619  * The return value is the disposition.
5620  */
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)5621 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5622 					struct net *net,
5623 					const struct sctp_endpoint *ep,
5624 					const struct sctp_association *asoc,
5625 					const union sctp_subtype type,
5626 					void *arg,
5627 					struct sctp_cmd_seq *commands)
5628 {
5629 	struct sctp_chunk *reply;
5630 
5631 	/* Once all its outstanding data has been acknowledged, the
5632 	 * endpoint shall send a SHUTDOWN chunk to its peer including
5633 	 * in the Cumulative TSN Ack field the last sequential TSN it
5634 	 * has received from the peer.
5635 	 */
5636 	reply = sctp_make_shutdown(asoc, arg);
5637 	if (!reply)
5638 		goto nomem;
5639 
5640 	/* Set the transport for the SHUTDOWN chunk and the timeout for the
5641 	 * T2-shutdown timer.
5642 	 */
5643 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5644 
5645 	/* It shall then start the T2-shutdown timer */
5646 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5647 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5648 
5649 	/* RFC 4960 Section 9.2
5650 	 * The sender of the SHUTDOWN MAY also start an overall guard timer
5651 	 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5652 	 */
5653 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5654 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5655 
5656 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5657 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5658 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5659 
5660 	/* and enter the SHUTDOWN-SENT state.  */
5661 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5662 			SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5663 
5664 	/* sctp-implguide 2.10 Issues with Heartbeating and failover
5665 	 *
5666 	 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5667 	 * or SHUTDOWN-ACK.
5668 	 */
5669 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5670 
5671 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5672 
5673 	return SCTP_DISPOSITION_CONSUME;
5674 
5675 nomem:
5676 	return SCTP_DISPOSITION_NOMEM;
5677 }
5678 
5679 /*
5680  * Generate a SHUTDOWN ACK now that everything is SACK'd.
5681  *
5682  * From Section 9.2:
5683  *
5684  * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5685  * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5686  * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5687  * endpoint must re-send the SHUTDOWN ACK.
5688  *
5689  * The return value is the disposition.
5690  */
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)5691 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5692 					struct net *net,
5693 					const struct sctp_endpoint *ep,
5694 					const struct sctp_association *asoc,
5695 					const union sctp_subtype type,
5696 					void *arg,
5697 					struct sctp_cmd_seq *commands)
5698 {
5699 	struct sctp_chunk *chunk = arg;
5700 	struct sctp_chunk *reply;
5701 
5702 	/* There are 2 ways of getting here:
5703 	 *    1) called in response to a SHUTDOWN chunk
5704 	 *    2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5705 	 *
5706 	 * For the case (2), the arg parameter is set to NULL.  We need
5707 	 * to check that we have a chunk before accessing it's fields.
5708 	 */
5709 	if (chunk) {
5710 		if (!sctp_vtag_verify(chunk, asoc))
5711 			return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5712 						commands);
5713 
5714 		/* Make sure that the SHUTDOWN chunk has a valid length. */
5715 		if (!sctp_chunk_length_valid(
5716 				chunk, sizeof(struct sctp_shutdown_chunk)))
5717 			return sctp_sf_violation_chunklen(net, ep, asoc, type,
5718 							  arg, commands);
5719 	}
5720 
5721 	/* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5722 	 * shall send a SHUTDOWN ACK ...
5723 	 */
5724 	reply = sctp_make_shutdown_ack(asoc, chunk);
5725 	if (!reply)
5726 		goto nomem;
5727 
5728 	/* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5729 	 * the T2-shutdown timer.
5730 	 */
5731 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5732 
5733 	/* and start/restart a T2-shutdown timer of its own, */
5734 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5735 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5736 
5737 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5738 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5739 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5740 
5741 	/* Enter the SHUTDOWN-ACK-SENT state.  */
5742 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5743 			SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5744 
5745 	/* sctp-implguide 2.10 Issues with Heartbeating and failover
5746 	 *
5747 	 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5748 	 * or SHUTDOWN-ACK.
5749 	 */
5750 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5751 
5752 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5753 
5754 	return SCTP_DISPOSITION_CONSUME;
5755 
5756 nomem:
5757 	return SCTP_DISPOSITION_NOMEM;
5758 }
5759 
5760 /*
5761  * Ignore the event defined as other
5762  *
5763  * The return value is the disposition of the event.
5764  */
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)5765 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5766 					   const struct sctp_endpoint *ep,
5767 					   const struct sctp_association *asoc,
5768 					   const union sctp_subtype type,
5769 					   void *arg,
5770 					   struct sctp_cmd_seq *commands)
5771 {
5772 	pr_debug("%s: the event other type:%d is ignored\n",
5773 		 __func__, type.other);
5774 
5775 	return SCTP_DISPOSITION_DISCARD;
5776 }
5777 
5778 /************************************************************
5779  * These are the state functions for handling timeout events.
5780  ************************************************************/
5781 
5782 /*
5783  * RTX Timeout
5784  *
5785  * Section: 6.3.3 Handle T3-rtx Expiration
5786  *
5787  * Whenever the retransmission timer T3-rtx expires for a destination
5788  * address, do the following:
5789  * [See below]
5790  *
5791  * The return value is the disposition of the chunk.
5792  */
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)5793 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5794 					   const struct sctp_endpoint *ep,
5795 					   const struct sctp_association *asoc,
5796 					   const union sctp_subtype type,
5797 					   void *arg,
5798 					   struct sctp_cmd_seq *commands)
5799 {
5800 	struct sctp_transport *transport = arg;
5801 
5802 	SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5803 
5804 	if (asoc->overall_error_count >= asoc->max_retrans) {
5805 		if (asoc->peer.zero_window_announced &&
5806 		    asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5807 			/*
5808 			 * We are here likely because the receiver had its rwnd
5809 			 * closed for a while and we have not been able to
5810 			 * transmit the locally queued data within the maximum
5811 			 * retransmission attempts limit.  Start the T5
5812 			 * shutdown guard timer to give the receiver one last
5813 			 * chance and some additional time to recover before
5814 			 * aborting.
5815 			 */
5816 			sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5817 				SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5818 		} else {
5819 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5820 					SCTP_ERROR(ETIMEDOUT));
5821 			/* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5822 			sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5823 					SCTP_PERR(SCTP_ERROR_NO_ERROR));
5824 			SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5825 			SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5826 			return SCTP_DISPOSITION_DELETE_TCB;
5827 		}
5828 	}
5829 
5830 	/* E1) For the destination address for which the timer
5831 	 * expires, adjust its ssthresh with rules defined in Section
5832 	 * 7.2.3 and set the cwnd <- MTU.
5833 	 */
5834 
5835 	/* E2) For the destination address for which the timer
5836 	 * expires, set RTO <- RTO * 2 ("back off the timer").  The
5837 	 * maximum value discussed in rule C7 above (RTO.max) may be
5838 	 * used to provide an upper bound to this doubling operation.
5839 	 */
5840 
5841 	/* E3) Determine how many of the earliest (i.e., lowest TSN)
5842 	 * outstanding DATA chunks for the address for which the
5843 	 * T3-rtx has expired will fit into a single packet, subject
5844 	 * to the MTU constraint for the path corresponding to the
5845 	 * destination transport address to which the retransmission
5846 	 * is being sent (this may be different from the address for
5847 	 * which the timer expires [see Section 6.4]).  Call this
5848 	 * value K. Bundle and retransmit those K DATA chunks in a
5849 	 * single packet to the destination endpoint.
5850 	 *
5851 	 * Note: Any DATA chunks that were sent to the address for
5852 	 * which the T3-rtx timer expired but did not fit in one MTU
5853 	 * (rule E3 above), should be marked for retransmission and
5854 	 * sent as soon as cwnd allows (normally when a SACK arrives).
5855 	 */
5856 
5857 	/* Do some failure management (Section 8.2). */
5858 	sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5859 
5860 	/* NB: Rules E4 and F1 are implicit in R1.  */
5861 	sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5862 
5863 	return SCTP_DISPOSITION_CONSUME;
5864 }
5865 
5866 /*
5867  * Generate delayed SACK on timeout
5868  *
5869  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
5870  *
5871  * The guidelines on delayed acknowledgement algorithm specified in
5872  * Section 4.2 of [RFC2581] SHOULD be followed.  Specifically, an
5873  * acknowledgement SHOULD be generated for at least every second packet
5874  * (not every second DATA chunk) received, and SHOULD be generated
5875  * within 200 ms of the arrival of any unacknowledged DATA chunk.  In
5876  * some situations it may be beneficial for an SCTP transmitter to be
5877  * more conservative than the algorithms detailed in this document
5878  * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5879  * the following algorithms allow.
5880  */
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)5881 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5882 					  const struct sctp_endpoint *ep,
5883 					  const struct sctp_association *asoc,
5884 					  const union sctp_subtype type,
5885 					  void *arg,
5886 					  struct sctp_cmd_seq *commands)
5887 {
5888 	SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5889 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5890 	return SCTP_DISPOSITION_CONSUME;
5891 }
5892 
5893 /*
5894  * sctp_sf_t1_init_timer_expire
5895  *
5896  * Section: 4 Note: 2
5897  * Verification Tag:
5898  * Inputs
5899  * (endpoint, asoc)
5900  *
5901  *  RFC 2960 Section 4 Notes
5902  *  2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5903  *     and re-start the T1-init timer without changing state.  This MUST
5904  *     be repeated up to 'Max.Init.Retransmits' times.  After that, the
5905  *     endpoint MUST abort the initialization process and report the
5906  *     error to SCTP user.
5907  *
5908  * Outputs
5909  * (timers, events)
5910  *
5911  */
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)5912 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5913 					struct net *net,
5914 					const struct sctp_endpoint *ep,
5915 					const struct sctp_association *asoc,
5916 					const union sctp_subtype type,
5917 					void *arg,
5918 					struct sctp_cmd_seq *commands)
5919 {
5920 	int attempts = asoc->init_err_counter + 1;
5921 	struct sctp_chunk *repl = NULL;
5922 	struct sctp_bind_addr *bp;
5923 
5924 	pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5925 
5926 	SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5927 
5928 	if (attempts <= asoc->max_init_attempts) {
5929 		bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5930 		repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5931 		if (!repl)
5932 			return SCTP_DISPOSITION_NOMEM;
5933 
5934 		/* Choose transport for INIT. */
5935 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5936 				SCTP_CHUNK(repl));
5937 
5938 		/* Issue a sideeffect to do the needed accounting. */
5939 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5940 				SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5941 
5942 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5943 	} else {
5944 		pr_debug("%s: giving up on INIT, attempts:%d "
5945 			 "max_init_attempts:%d\n", __func__, attempts,
5946 			 asoc->max_init_attempts);
5947 
5948 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5949 				SCTP_ERROR(ETIMEDOUT));
5950 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5951 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
5952 		return SCTP_DISPOSITION_DELETE_TCB;
5953 	}
5954 
5955 	return SCTP_DISPOSITION_CONSUME;
5956 }
5957 
5958 /*
5959  * sctp_sf_t1_cookie_timer_expire
5960  *
5961  * Section: 4 Note: 2
5962  * Verification Tag:
5963  * Inputs
5964  * (endpoint, asoc)
5965  *
5966  *  RFC 2960 Section 4 Notes
5967  *  3) If the T1-cookie timer expires, the endpoint MUST retransmit
5968  *     COOKIE ECHO and re-start the T1-cookie timer without changing
5969  *     state.  This MUST be repeated up to 'Max.Init.Retransmits' times.
5970  *     After that, the endpoint MUST abort the initialization process and
5971  *     report the error to SCTP user.
5972  *
5973  * Outputs
5974  * (timers, events)
5975  *
5976  */
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)5977 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5978 					struct net *net,
5979 					const struct sctp_endpoint *ep,
5980 					const struct sctp_association *asoc,
5981 					const union sctp_subtype type,
5982 					void *arg,
5983 					struct sctp_cmd_seq *commands)
5984 {
5985 	int attempts = asoc->init_err_counter + 1;
5986 	struct sctp_chunk *repl = NULL;
5987 
5988 	pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5989 
5990 	SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5991 
5992 	if (attempts <= asoc->max_init_attempts) {
5993 		repl = sctp_make_cookie_echo(asoc, NULL);
5994 		if (!repl)
5995 			return SCTP_DISPOSITION_NOMEM;
5996 
5997 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5998 				SCTP_CHUNK(repl));
5999 		/* Issue a sideeffect to do the needed accounting. */
6000 		sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
6001 				SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
6002 
6003 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
6004 	} else {
6005 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6006 				SCTP_ERROR(ETIMEDOUT));
6007 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
6008 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
6009 		return SCTP_DISPOSITION_DELETE_TCB;
6010 	}
6011 
6012 	return SCTP_DISPOSITION_CONSUME;
6013 }
6014 
6015 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
6016  * with the updated last sequential TSN received from its peer.
6017  *
6018  * An endpoint should limit the number of retransmission of the
6019  * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
6020  * If this threshold is exceeded the endpoint should destroy the TCB and
6021  * MUST report the peer endpoint unreachable to the upper layer (and
6022  * thus the association enters the CLOSED state).  The reception of any
6023  * packet from its peer (i.e. as the peer sends all of its queued DATA
6024  * chunks) should clear the endpoint's retransmission count and restart
6025  * the T2-Shutdown timer,  giving its peer ample opportunity to transmit
6026  * all of its queued DATA chunks that have not yet been sent.
6027  */
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)6028 enum sctp_disposition sctp_sf_t2_timer_expire(
6029 					struct net *net,
6030 					const struct sctp_endpoint *ep,
6031 					const struct sctp_association *asoc,
6032 					const union sctp_subtype type,
6033 					void *arg,
6034 					struct sctp_cmd_seq *commands)
6035 {
6036 	struct sctp_chunk *reply = NULL;
6037 
6038 	pr_debug("%s: timer T2 expired\n", __func__);
6039 
6040 	SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
6041 
6042 	((struct sctp_association *)asoc)->shutdown_retries++;
6043 
6044 	if (asoc->overall_error_count >= asoc->max_retrans) {
6045 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6046 				SCTP_ERROR(ETIMEDOUT));
6047 		/* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
6048 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6049 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
6050 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6051 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6052 		return SCTP_DISPOSITION_DELETE_TCB;
6053 	}
6054 
6055 	switch (asoc->state) {
6056 	case SCTP_STATE_SHUTDOWN_SENT:
6057 		reply = sctp_make_shutdown(asoc, NULL);
6058 		break;
6059 
6060 	case SCTP_STATE_SHUTDOWN_ACK_SENT:
6061 		reply = sctp_make_shutdown_ack(asoc, NULL);
6062 		break;
6063 
6064 	default:
6065 		BUG();
6066 		break;
6067 	}
6068 
6069 	if (!reply)
6070 		goto nomem;
6071 
6072 	/* Do some failure management (Section 8.2).
6073 	 * If we remove the transport an SHUTDOWN was last sent to, don't
6074 	 * do failure management.
6075 	 */
6076 	if (asoc->shutdown_last_sent_to)
6077 		sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6078 				SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
6079 
6080 	/* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
6081 	 * the T2-shutdown timer.
6082 	 */
6083 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
6084 
6085 	/* Restart the T2-shutdown timer.  */
6086 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6087 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
6088 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6089 	return SCTP_DISPOSITION_CONSUME;
6090 
6091 nomem:
6092 	return SCTP_DISPOSITION_NOMEM;
6093 }
6094 
6095 /*
6096  * ADDIP Section 4.1 ASCONF Chunk Procedures
6097  * If the T4 RTO timer expires the endpoint should do B1 to B5
6098  */
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)6099 enum sctp_disposition sctp_sf_t4_timer_expire(
6100 					struct net *net,
6101 					const struct sctp_endpoint *ep,
6102 					const struct sctp_association *asoc,
6103 					const union sctp_subtype type,
6104 					void *arg,
6105 					struct sctp_cmd_seq *commands)
6106 {
6107 	struct sctp_chunk *chunk = asoc->addip_last_asconf;
6108 	struct sctp_transport *transport = chunk->transport;
6109 
6110 	SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
6111 
6112 	/* ADDIP 4.1 B1) Increment the error counters and perform path failure
6113 	 * detection on the appropriate destination address as defined in
6114 	 * RFC2960 [5] section 8.1 and 8.2.
6115 	 */
6116 	if (transport)
6117 		sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6118 				SCTP_TRANSPORT(transport));
6119 
6120 	/* Reconfig T4 timer and transport. */
6121 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
6122 
6123 	/* ADDIP 4.1 B2) Increment the association error counters and perform
6124 	 * endpoint failure detection on the association as defined in
6125 	 * RFC2960 [5] section 8.1 and 8.2.
6126 	 * association error counter is incremented in SCTP_CMD_STRIKE.
6127 	 */
6128 	if (asoc->overall_error_count >= asoc->max_retrans) {
6129 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
6130 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6131 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6132 				SCTP_ERROR(ETIMEDOUT));
6133 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6134 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
6135 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6136 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6137 		return SCTP_DISPOSITION_ABORT;
6138 	}
6139 
6140 	/* ADDIP 4.1 B3) Back-off the destination address RTO value to which
6141 	 * the ASCONF chunk was sent by doubling the RTO timer value.
6142 	 * This is done in SCTP_CMD_STRIKE.
6143 	 */
6144 
6145 	/* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
6146 	 * choose an alternate destination address (please refer to RFC2960
6147 	 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
6148 	 * chunk, it MUST be the same (including its serial number) as the last
6149 	 * ASCONF sent.
6150 	 */
6151 	sctp_chunk_hold(asoc->addip_last_asconf);
6152 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6153 			SCTP_CHUNK(asoc->addip_last_asconf));
6154 
6155 	/* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
6156 	 * destination is selected, then the RTO used will be that of the new
6157 	 * destination address.
6158 	 */
6159 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6160 			SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6161 
6162 	return SCTP_DISPOSITION_CONSUME;
6163 }
6164 
6165 /* sctpimpguide-05 Section 2.12.2
6166  * The sender of the SHUTDOWN MAY also start an overall guard timer
6167  * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6168  * At the expiration of this timer the sender SHOULD abort the association
6169  * by sending an ABORT chunk.
6170  */
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)6171 enum sctp_disposition sctp_sf_t5_timer_expire(
6172 					struct net *net,
6173 					const struct sctp_endpoint *ep,
6174 					const struct sctp_association *asoc,
6175 					const union sctp_subtype type,
6176 					void *arg,
6177 					struct sctp_cmd_seq *commands)
6178 {
6179 	struct sctp_chunk *reply = NULL;
6180 
6181 	pr_debug("%s: timer T5 expired\n", __func__);
6182 
6183 	SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6184 
6185 	reply = sctp_make_abort(asoc, NULL, 0);
6186 	if (!reply)
6187 		goto nomem;
6188 
6189 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6190 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6191 			SCTP_ERROR(ETIMEDOUT));
6192 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6193 			SCTP_PERR(SCTP_ERROR_NO_ERROR));
6194 
6195 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6196 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6197 
6198 	return SCTP_DISPOSITION_DELETE_TCB;
6199 nomem:
6200 	return SCTP_DISPOSITION_NOMEM;
6201 }
6202 
6203 /* Handle expiration of AUTOCLOSE timer.  When the autoclose timer expires,
6204  * the association is automatically closed by starting the shutdown process.
6205  * The work that needs to be done is same as when SHUTDOWN is initiated by
6206  * the user.  So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6207  */
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)6208 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6209 					struct net *net,
6210 					const struct sctp_endpoint *ep,
6211 					const struct sctp_association *asoc,
6212 					const union sctp_subtype type,
6213 					void *arg,
6214 					struct sctp_cmd_seq *commands)
6215 {
6216 	enum sctp_disposition disposition;
6217 
6218 	SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6219 
6220 	/* From 9.2 Shutdown of an Association
6221 	 * Upon receipt of the SHUTDOWN primitive from its upper
6222 	 * layer, the endpoint enters SHUTDOWN-PENDING state and
6223 	 * remains there until all outstanding data has been
6224 	 * acknowledged by its peer. The endpoint accepts no new data
6225 	 * from its upper layer, but retransmits data to the far end
6226 	 * if necessary to fill gaps.
6227 	 */
6228 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6229 			SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6230 
6231 	disposition = SCTP_DISPOSITION_CONSUME;
6232 	if (sctp_outq_is_empty(&asoc->outqueue)) {
6233 		disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6234 							    NULL, commands);
6235 	}
6236 
6237 	return disposition;
6238 }
6239 
6240 /*****************************************************************************
6241  * These are sa state functions which could apply to all types of events.
6242  ****************************************************************************/
6243 
6244 /*
6245  * This table entry is not implemented.
6246  *
6247  * Inputs
6248  * (endpoint, asoc, chunk)
6249  *
6250  * The return value is the disposition of the chunk.
6251  */
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)6252 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6253 				       const struct sctp_endpoint *ep,
6254 				       const struct sctp_association *asoc,
6255 				       const union sctp_subtype type,
6256 				       void *arg, struct sctp_cmd_seq *commands)
6257 {
6258 	return SCTP_DISPOSITION_NOT_IMPL;
6259 }
6260 
6261 /*
6262  * This table entry represents a bug.
6263  *
6264  * Inputs
6265  * (endpoint, asoc, chunk)
6266  *
6267  * The return value is the disposition of the chunk.
6268  */
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)6269 enum sctp_disposition sctp_sf_bug(struct net *net,
6270 				  const struct sctp_endpoint *ep,
6271 				  const struct sctp_association *asoc,
6272 				  const union sctp_subtype type,
6273 				  void *arg, struct sctp_cmd_seq *commands)
6274 {
6275 	return SCTP_DISPOSITION_BUG;
6276 }
6277 
6278 /*
6279  * This table entry represents the firing of a timer in the wrong state.
6280  * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6281  * when the association is in the wrong state.   This event should
6282  * be ignored, so as to prevent any rearming of the timer.
6283  *
6284  * Inputs
6285  * (endpoint, asoc, chunk)
6286  *
6287  * The return value is the disposition of the chunk.
6288  */
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)6289 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6290 					   const struct sctp_endpoint *ep,
6291 					   const struct sctp_association *asoc,
6292 					   const union sctp_subtype type,
6293 					   void *arg,
6294 					   struct sctp_cmd_seq *commands)
6295 {
6296 	pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6297 
6298 	return SCTP_DISPOSITION_CONSUME;
6299 }
6300 
6301 /********************************************************************
6302  * 2nd Level Abstractions
6303  ********************************************************************/
6304 
6305 /* Pull the SACK chunk based on the SACK header. */
sctp_sm_pull_sack(struct sctp_chunk * chunk)6306 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6307 {
6308 	struct sctp_sackhdr *sack;
6309 	__u16 num_dup_tsns;
6310 	unsigned int len;
6311 	__u16 num_blocks;
6312 
6313 	/* Protect ourselves from reading too far into
6314 	 * the skb from a bogus sender.
6315 	 */
6316 	sack = (struct sctp_sackhdr *) chunk->skb->data;
6317 
6318 	num_blocks = ntohs(sack->num_gap_ack_blocks);
6319 	num_dup_tsns = ntohs(sack->num_dup_tsns);
6320 	len = sizeof(struct sctp_sackhdr);
6321 	len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6322 	if (len > chunk->skb->len)
6323 		return NULL;
6324 
6325 	skb_pull(chunk->skb, len);
6326 
6327 	return sack;
6328 }
6329 
6330 /* Create an ABORT packet to be sent as a response, with the specified
6331  * error causes.
6332  */
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)6333 static struct sctp_packet *sctp_abort_pkt_new(
6334 					struct net *net,
6335 					const struct sctp_endpoint *ep,
6336 					const struct sctp_association *asoc,
6337 					struct sctp_chunk *chunk,
6338 					const void *payload, size_t paylen)
6339 {
6340 	struct sctp_packet *packet;
6341 	struct sctp_chunk *abort;
6342 
6343 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
6344 
6345 	if (packet) {
6346 		/* Make an ABORT.
6347 		 * The T bit will be set if the asoc is NULL.
6348 		 */
6349 		abort = sctp_make_abort(asoc, chunk, paylen);
6350 		if (!abort) {
6351 			sctp_ootb_pkt_free(packet);
6352 			return NULL;
6353 		}
6354 
6355 		/* Reflect vtag if T-Bit is set */
6356 		if (sctp_test_T_bit(abort))
6357 			packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6358 
6359 		/* Add specified error causes, i.e., payload, to the
6360 		 * end of the chunk.
6361 		 */
6362 		sctp_addto_chunk(abort, paylen, payload);
6363 
6364 		/* Set the skb to the belonging sock for accounting.  */
6365 		abort->skb->sk = ep->base.sk;
6366 
6367 		sctp_packet_append_chunk(packet, abort);
6368 
6369 	}
6370 
6371 	return packet;
6372 }
6373 
6374 /* 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)6375 static struct sctp_packet *sctp_ootb_pkt_new(
6376 					struct net *net,
6377 					const struct sctp_association *asoc,
6378 					const struct sctp_chunk *chunk)
6379 {
6380 	struct sctp_transport *transport;
6381 	struct sctp_packet *packet;
6382 	__u16 sport, dport;
6383 	__u32 vtag;
6384 
6385 	/* Get the source and destination port from the inbound packet.  */
6386 	sport = ntohs(chunk->sctp_hdr->dest);
6387 	dport = ntohs(chunk->sctp_hdr->source);
6388 
6389 	/* The V-tag is going to be the same as the inbound packet if no
6390 	 * association exists, otherwise, use the peer's vtag.
6391 	 */
6392 	if (asoc) {
6393 		/* Special case the INIT-ACK as there is no peer's vtag
6394 		 * yet.
6395 		 */
6396 		switch (chunk->chunk_hdr->type) {
6397 		case SCTP_CID_INIT:
6398 		case SCTP_CID_INIT_ACK:
6399 		{
6400 			struct sctp_initack_chunk *initack;
6401 
6402 			initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6403 			vtag = ntohl(initack->init_hdr.init_tag);
6404 			break;
6405 		}
6406 		default:
6407 			vtag = asoc->peer.i.init_tag;
6408 			break;
6409 		}
6410 	} else {
6411 		/* Special case the INIT and stale COOKIE_ECHO as there is no
6412 		 * vtag yet.
6413 		 */
6414 		switch (chunk->chunk_hdr->type) {
6415 		case SCTP_CID_INIT:
6416 		{
6417 			struct sctp_init_chunk *init;
6418 
6419 			init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6420 			vtag = ntohl(init->init_hdr.init_tag);
6421 			break;
6422 		}
6423 		default:
6424 			vtag = ntohl(chunk->sctp_hdr->vtag);
6425 			break;
6426 		}
6427 	}
6428 
6429 	/* Make a transport for the bucket, Eliza... */
6430 	transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6431 	if (!transport)
6432 		goto nomem;
6433 
6434 	transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
6435 
6436 	/* Cache a route for the transport with the chunk's destination as
6437 	 * the source address.
6438 	 */
6439 	sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6440 			     sctp_sk(net->sctp.ctl_sock));
6441 
6442 	packet = &transport->packet;
6443 	sctp_packet_init(packet, transport, sport, dport);
6444 	sctp_packet_config(packet, vtag, 0);
6445 
6446 	return packet;
6447 
6448 nomem:
6449 	return NULL;
6450 }
6451 
6452 /* Free the packet allocated earlier for responding in the OOTB condition.  */
sctp_ootb_pkt_free(struct sctp_packet * packet)6453 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6454 {
6455 	sctp_transport_free(packet->transport);
6456 }
6457 
6458 /* 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)6459 static void sctp_send_stale_cookie_err(struct net *net,
6460 				       const struct sctp_endpoint *ep,
6461 				       const struct sctp_association *asoc,
6462 				       const struct sctp_chunk *chunk,
6463 				       struct sctp_cmd_seq *commands,
6464 				       struct sctp_chunk *err_chunk)
6465 {
6466 	struct sctp_packet *packet;
6467 
6468 	if (err_chunk) {
6469 		packet = sctp_ootb_pkt_new(net, asoc, chunk);
6470 		if (packet) {
6471 			struct sctp_signed_cookie *cookie;
6472 
6473 			/* Override the OOTB vtag from the cookie. */
6474 			cookie = chunk->subh.cookie_hdr;
6475 			packet->vtag = cookie->c.peer_vtag;
6476 
6477 			/* Set the skb to the belonging sock for accounting. */
6478 			err_chunk->skb->sk = ep->base.sk;
6479 			sctp_packet_append_chunk(packet, err_chunk);
6480 			sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6481 					SCTP_PACKET(packet));
6482 			SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6483 		} else
6484 			sctp_chunk_free (err_chunk);
6485 	}
6486 }
6487 
6488 
6489 /* Process a data chunk */
sctp_eat_data(const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands)6490 static int sctp_eat_data(const struct sctp_association *asoc,
6491 			 struct sctp_chunk *chunk,
6492 			 struct sctp_cmd_seq *commands)
6493 {
6494 	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6495 	struct sock *sk = asoc->base.sk;
6496 	struct net *net = sock_net(sk);
6497 	struct sctp_datahdr *data_hdr;
6498 	struct sctp_chunk *err;
6499 	enum sctp_verb deliver;
6500 	size_t datalen;
6501 	__u32 tsn;
6502 	int tmp;
6503 
6504 	data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6505 	chunk->subh.data_hdr = data_hdr;
6506 	skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6507 
6508 	tsn = ntohl(data_hdr->tsn);
6509 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6510 
6511 	/* ASSERT:  Now skb->data is really the user data.  */
6512 
6513 	/* Process ECN based congestion.
6514 	 *
6515 	 * Since the chunk structure is reused for all chunks within
6516 	 * a packet, we use ecn_ce_done to track if we've already
6517 	 * done CE processing for this packet.
6518 	 *
6519 	 * We need to do ECN processing even if we plan to discard the
6520 	 * chunk later.
6521 	 */
6522 
6523 	if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6524 		struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6525 		chunk->ecn_ce_done = 1;
6526 
6527 		if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6528 			/* Do real work as side effect. */
6529 			sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6530 					SCTP_U32(tsn));
6531 		}
6532 	}
6533 
6534 	tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6535 	if (tmp < 0) {
6536 		/* The TSN is too high--silently discard the chunk and
6537 		 * count on it getting retransmitted later.
6538 		 */
6539 		if (chunk->asoc)
6540 			chunk->asoc->stats.outofseqtsns++;
6541 		return SCTP_IERROR_HIGH_TSN;
6542 	} else if (tmp > 0) {
6543 		/* This is a duplicate.  Record it.  */
6544 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6545 		return SCTP_IERROR_DUP_TSN;
6546 	}
6547 
6548 	/* This is a new TSN.  */
6549 
6550 	/* Discard if there is no room in the receive window.
6551 	 * Actually, allow a little bit of overflow (up to a MTU).
6552 	 */
6553 	datalen = ntohs(chunk->chunk_hdr->length);
6554 	datalen -= sctp_datachk_len(&asoc->stream);
6555 
6556 	deliver = SCTP_CMD_CHUNK_ULP;
6557 
6558 	/* Think about partial delivery. */
6559 	if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6560 
6561 		/* Even if we don't accept this chunk there is
6562 		 * memory pressure.
6563 		 */
6564 		sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6565 	}
6566 
6567 	/* Spill over rwnd a little bit.  Note: While allowed, this spill over
6568 	 * seems a bit troublesome in that frag_point varies based on
6569 	 * PMTU.  In cases, such as loopback, this might be a rather
6570 	 * large spill over.
6571 	 */
6572 	if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6573 	    (datalen > asoc->rwnd + asoc->frag_point))) {
6574 
6575 		/* If this is the next TSN, consider reneging to make
6576 		 * room.   Note: Playing nice with a confused sender.  A
6577 		 * malicious sender can still eat up all our buffer
6578 		 * space and in the future we may want to detect and
6579 		 * do more drastic reneging.
6580 		 */
6581 		if (sctp_tsnmap_has_gap(map) &&
6582 		    (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6583 			pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6584 			deliver = SCTP_CMD_RENEGE;
6585 		} else {
6586 			pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6587 				 __func__, tsn, datalen, asoc->rwnd);
6588 
6589 			return SCTP_IERROR_IGNORE_TSN;
6590 		}
6591 	}
6592 
6593 	/*
6594 	 * Also try to renege to limit our memory usage in the event that
6595 	 * we are under memory pressure
6596 	 * If we can't renege, don't worry about it, the sk_rmem_schedule
6597 	 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6598 	 * memory usage too much
6599 	 */
6600 	if (sk_under_memory_pressure(sk)) {
6601 		if (sctp_tsnmap_has_gap(map) &&
6602 		    (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6603 			pr_debug("%s: under pressure, reneging for tsn:%u\n",
6604 				 __func__, tsn);
6605 			deliver = SCTP_CMD_RENEGE;
6606 		}
6607 	}
6608 
6609 	/*
6610 	 * Section 3.3.10.9 No User Data (9)
6611 	 *
6612 	 * Cause of error
6613 	 * ---------------
6614 	 * No User Data:  This error cause is returned to the originator of a
6615 	 * DATA chunk if a received DATA chunk has no user data.
6616 	 */
6617 	if (unlikely(0 == datalen)) {
6618 		err = sctp_make_abort_no_data(asoc, chunk, tsn);
6619 		if (err) {
6620 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6621 					SCTP_CHUNK(err));
6622 		}
6623 		/* We are going to ABORT, so we might as well stop
6624 		 * processing the rest of the chunks in the packet.
6625 		 */
6626 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6627 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6628 				SCTP_ERROR(ECONNABORTED));
6629 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6630 				SCTP_PERR(SCTP_ERROR_NO_DATA));
6631 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6632 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6633 		return SCTP_IERROR_NO_DATA;
6634 	}
6635 
6636 	chunk->data_accepted = 1;
6637 
6638 	/* Note: Some chunks may get overcounted (if we drop) or overcounted
6639 	 * if we renege and the chunk arrives again.
6640 	 */
6641 	if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6642 		SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6643 		if (chunk->asoc)
6644 			chunk->asoc->stats.iuodchunks++;
6645 	} else {
6646 		SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6647 		if (chunk->asoc)
6648 			chunk->asoc->stats.iodchunks++;
6649 	}
6650 
6651 	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6652 	 *
6653 	 * If an endpoint receive a DATA chunk with an invalid stream
6654 	 * identifier, it shall acknowledge the reception of the DATA chunk
6655 	 * following the normal procedure, immediately send an ERROR chunk
6656 	 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6657 	 * and discard the DATA chunk.
6658 	 */
6659 	if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6660 		/* Mark tsn as received even though we drop it */
6661 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6662 
6663 		err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6664 					 &data_hdr->stream,
6665 					 sizeof(data_hdr->stream),
6666 					 sizeof(u16));
6667 		if (err)
6668 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6669 					SCTP_CHUNK(err));
6670 		return SCTP_IERROR_BAD_STREAM;
6671 	}
6672 
6673 	/* Check to see if the SSN is possible for this TSN.
6674 	 * The biggest gap we can record is 4K wide.  Since SSNs wrap
6675 	 * at an unsigned short, there is no way that an SSN can
6676 	 * wrap and for a valid TSN.  We can simply check if the current
6677 	 * SSN is smaller then the next expected one.  If it is, it wrapped
6678 	 * and is invalid.
6679 	 */
6680 	if (!asoc->stream.si->validate_data(chunk))
6681 		return SCTP_IERROR_PROTO_VIOLATION;
6682 
6683 	/* Send the data up to the user.  Note:  Schedule  the
6684 	 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6685 	 * chunk needs the updated rwnd.
6686 	 */
6687 	sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6688 
6689 	return SCTP_IERROR_NO_ERROR;
6690 }
6691