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