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