sm_sideeffect.c 49.9 KB
Newer Older
1
/* SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5
 * (C) Copyright IBM Corp. 2001, 2004
 * Copyright (c) 1999 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 *
6
 * This file is part of the SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
7 8 9 10 11
 *
 * These functions work with the state functions in sctp_sm_statefuns.c
 * to implement that state operations.  These functions implement the
 * steps which require modifying existing data structures.
 *
12
 * This SCTP implementation is free software;
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16 17
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
18
 * This SCTP implementation is distributed in the hope that it
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21 22 23 24
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
25 26
 * along with GNU CC; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29
 *
 * Please send any bug reports or fixes you make to the
 * email address(es):
30
 *    lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds's avatar
Linus Torvalds committed
31 32 33 34 35 36 37 38 39 40 41 42
 *
 * Written or modified by:
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    Jon Grimm             <jgrimm@austin.ibm.com>
 *    Hui Huang		    <hui.huang@nokia.com>
 *    Dajiang Zhang	    <dajiang.zhang@nokia.com>
 *    Daisy Chang	    <daisyc@us.ibm.com>
 *    Sridhar Samudrala	    <sri@us.ibm.com>
 *    Ardelle Fan	    <ardelle.fan@intel.com>
 */

43 44
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48
#include <linux/skbuff.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/ip.h>
49
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53
#include <net/sock.h>
#include <net/sctp/sctp.h>
#include <net/sctp/sm.h>

54
static int sctp_cmd_interpreter(enum sctp_event event_type,
55
				union sctp_subtype subtype,
56
				enum sctp_state state,
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59
				struct sctp_endpoint *ep,
				struct sctp_association *asoc,
				void *event_arg,
60
				enum sctp_disposition status,
61
				struct sctp_cmd_seq *commands,
62
				gfp_t gfp);
63 64
static int sctp_side_effects(enum sctp_event event_type,
			     union sctp_subtype subtype,
65
			     enum sctp_state state,
Linus Torvalds's avatar
Linus Torvalds committed
66
			     struct sctp_endpoint *ep,
67
			     struct sctp_association **asoc,
Linus Torvalds's avatar
Linus Torvalds committed
68
			     void *event_arg,
69
			     enum sctp_disposition status,
70
			     struct sctp_cmd_seq *commands,
71
			     gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74 75 76 77

/********************************************************************
 * Helper functions
 ********************************************************************/

/* A helper function for delayed processing of INET ECN CE bit. */
78
static void sctp_do_ecn_ce_work(struct sctp_association *asoc,
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
				__u32 lowest_tsn)
{
	/* Save the TSN away for comparison when we receive CWR */

	asoc->last_ecne_tsn = lowest_tsn;
	asoc->need_ecne = 1;
}

/* Helper function for delayed processing of SCTP ECNE chunk.  */
/* RFC 2960 Appendix A
 *
 * RFC 2481 details a specific bit for a sender to send in
 * the header of its next outbound TCP segment to indicate to
 * its peer that it has reduced its congestion window.  This
 * is termed the CWR bit.  For SCTP the same indication is made
 * by including the CWR chunk.  This chunk contains one data
 * element, i.e. the TSN number that was sent in the ECNE chunk.
 * This element represents the lowest TSN number in the datagram
 * that was originally marked with the CE bit.
 */
static struct sctp_chunk *sctp_do_ecn_ecne_work(struct sctp_association *asoc,
100 101
						__u32 lowest_tsn,
						struct sctp_chunk *chunk)
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
	struct sctp_chunk *repl;

	/* Our previously transmitted packet ran into some congestion
	 * so we should take action by reducing cwnd and ssthresh
	 * and then ACK our peer that we we've done so by
	 * sending a CWR.
	 */

	/* First, try to determine if we want to actually lower
	 * our cwnd variables.  Only lower them if the ECNE looks more
	 * recent than the last response.
	 */
	if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) {
		struct sctp_transport *transport;

		/* Find which transport's congestion variables
		 * need to be adjusted.
		 */
		transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn);

		/* Update the congestion variables. */
		if (transport)
			sctp_transport_lower_cwnd(transport,
						  SCTP_LOWER_CWND_ECNE);
		asoc->last_cwr_tsn = lowest_tsn;
	}

	/* Always try to quiet the other end.  In case of lost CWR,
	 * resend last_cwr_tsn.
	 */
	repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk);

	/* If we run out of memory, it will look like a lost CWR.  We'll
	 * get back in sync eventually.
	 */
	return repl;
}

/* Helper function to do delayed processing of ECN CWR chunk.  */
static void sctp_do_ecn_cwr_work(struct sctp_association *asoc,
				 __u32 lowest_tsn)
{
	/* Turn off ECNE getting auto-prepended to every outgoing
	 * packet
	 */
	asoc->need_ecne = 0;
}

/* Generate SACK if necessary.  We call this at the end of a packet.  */
static int sctp_gen_sack(struct sctp_association *asoc, int force,
153
			 struct sctp_cmd_seq *commands)
Linus Torvalds's avatar
Linus Torvalds committed
154
{
155
	struct sctp_transport *trans = asoc->peer.last_data_from;
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159
	__u32 ctsn, max_tsn_seen;
	struct sctp_chunk *sack;
	int error = 0;

160
	if (force ||
161 162
	    (!trans && (asoc->param_flags & SPP_SACKDELAY_DISABLE)) ||
	    (trans && (trans->param_flags & SPP_SACKDELAY_DISABLE)))
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		asoc->peer.sack_needed = 1;

	ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
	max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);

	/* From 12.2 Parameters necessary per association (i.e. the TCB):
	 *
	 * Ack State : This flag indicates if the next received packet
	 * 	     : is to be responded to with a SACK. ...
	 *	     : When DATA chunks are out of order, SACK's
	 *           : are not delayed (see Section 6).
	 *
	 * [This is actually not mentioned in Section 6, but we
	 * implement it here anyway. --piggy]
	 */
178
	if (max_tsn_seen != ctsn)
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181 182 183 184 185 186 187 188 189
		asoc->peer.sack_needed = 1;

	/* From 6.2  Acknowledgement on Reception of DATA Chunks:
	 *
	 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically,
	 * an acknowledgement SHOULD be generated for at least every
	 * second packet (not every second DATA chunk) received, and
	 * SHOULD be generated within 200 ms of the arrival of any
	 * unacknowledged DATA chunk. ...
	 */
	if (!asoc->peer.sack_needed) {
190
		asoc->peer.sack_cnt++;
191 192 193 194 195 196

		/* Set the SACK delay timeout based on the
		 * SACK delay for the last transport
		 * data was received from, or the default
		 * for the association.
		 */
197 198 199 200 201
		if (trans) {
			/* We will need a SACK for the next packet.  */
			if (asoc->peer.sack_cnt >= trans->sackfreq - 1)
				asoc->peer.sack_needed = 1;

202
			asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
203
				trans->sackdelay;
204 205 206 207 208
		} else {
			/* We will need a SACK for the next packet.  */
			if (asoc->peer.sack_cnt >= asoc->sackfreq - 1)
				asoc->peer.sack_needed = 1;

209
			asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
210
				asoc->sackdelay;
211
		}
212 213 214 215

		/* Restart the SACK timer. */
		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
				SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds's avatar
Linus Torvalds committed
216
	} else {
217 218
		__u32 old_a_rwnd = asoc->a_rwnd;

219
		asoc->a_rwnd = asoc->rwnd;
Linus Torvalds's avatar
Linus Torvalds committed
220
		sack = sctp_make_sack(asoc);
221 222
		if (!sack) {
			asoc->a_rwnd = old_a_rwnd;
Linus Torvalds's avatar
Linus Torvalds committed
223
			goto nomem;
224
		}
Linus Torvalds's avatar
Linus Torvalds committed
225 226

		asoc->peer.sack_needed = 0;
227
		asoc->peer.sack_cnt = 0;
Linus Torvalds's avatar
Linus Torvalds committed
228

229
		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(sack));
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232 233 234

		/* Stop the SACK timer.  */
		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
				SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
	}
235

Linus Torvalds's avatar
Linus Torvalds committed
236 237 238 239 240 241 242 243 244 245 246 247 248
	return error;
nomem:
	error = -ENOMEM;
	return error;
}

/* When the T3-RTX timer expires, it calls this function to create the
 * relevant state machine event.
 */
void sctp_generate_t3_rtx_event(unsigned long peer)
{
	struct sctp_transport *transport = (struct sctp_transport *) peer;
	struct sctp_association *asoc = transport->asoc;
249 250
	struct sock *sk = asoc->base.sk;
	struct net *net = sock_net(sk);
251
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254

	/* Check whether a task is in the sock.  */

255 256
	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
257
		pr_debug("%s: sock is busy\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262 263 264 265

		/* Try again later.  */
		if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
			sctp_transport_hold(transport);
		goto out_unlock;
	}

	/* Run through the state machine.  */
266
	error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT,
Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270 271 272
			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
			   asoc->state,
			   asoc->ep, asoc,
			   transport, GFP_ATOMIC);

	if (error)
273
		sk->sk_err = -error;
Linus Torvalds's avatar
Linus Torvalds committed
274 275

out_unlock:
276
	bh_unlock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283
	sctp_transport_put(transport);
}

/* This is a sa interface for producing timeout events.  It works
 * for timeouts which use the association as their parameter.
 */
static void sctp_generate_timeout_event(struct sctp_association *asoc,
284
					enum sctp_event_timeout timeout_type)
Linus Torvalds's avatar
Linus Torvalds committed
285
{
286 287
	struct sock *sk = asoc->base.sk;
	struct net *net = sock_net(sk);
Linus Torvalds's avatar
Linus Torvalds committed
288 289
	int error = 0;

290 291
	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
292 293
		pr_debug("%s: sock is busy: timer %d\n", __func__,
			 timeout_type);
Linus Torvalds's avatar
Linus Torvalds committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307

		/* Try again later.  */
		if (!mod_timer(&asoc->timers[timeout_type], jiffies + (HZ/20)))
			sctp_association_hold(asoc);
		goto out_unlock;
	}

	/* Is this association really dead and just waiting around for
	 * the timer to let go of the reference?
	 */
	if (asoc->base.dead)
		goto out_unlock;

	/* Run through the state machine.  */
308
	error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT,
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312 313
			   SCTP_ST_TIMEOUT(timeout_type),
			   asoc->state, asoc->ep, asoc,
			   (void *)timeout_type, GFP_ATOMIC);

	if (error)
314
		sk->sk_err = -error;
Linus Torvalds's avatar
Linus Torvalds committed
315 316

out_unlock:
317
	bh_unlock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	sctp_association_put(asoc);
}

static void sctp_generate_t1_cookie_event(unsigned long data)
{
	struct sctp_association *asoc = (struct sctp_association *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE);
}

static void sctp_generate_t1_init_event(unsigned long data)
{
	struct sctp_association *asoc = (struct sctp_association *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT);
}

static void sctp_generate_t2_shutdown_event(unsigned long data)
{
	struct sctp_association *asoc = (struct sctp_association *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN);
}

static void sctp_generate_t4_rto_event(unsigned long data)
{
	struct sctp_association *asoc = (struct sctp_association *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T4_RTO);
}

static void sctp_generate_t5_shutdown_guard_event(unsigned long data)
{
347 348
	struct sctp_association *asoc = (struct sctp_association *)data;
	sctp_generate_timeout_event(asoc,
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
				    SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD);

} /* sctp_generate_t5_shutdown_guard_event() */

static void sctp_generate_autoclose_event(unsigned long data)
{
	struct sctp_association *asoc = (struct sctp_association *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE);
}

/* Generate a heart beat event.  If the sock is busy, reschedule.   Make
 * sure that the transport is still valid.
 */
void sctp_generate_heartbeat_event(unsigned long data)
{
	struct sctp_transport *transport = (struct sctp_transport *) data;
	struct sctp_association *asoc = transport->asoc;
366 367
	struct sock *sk = asoc->base.sk;
	struct net *net = sock_net(sk);
368
	u32 elapsed, timeout;
369
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
370

371 372
	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
373
		pr_debug("%s: sock is busy\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
374 375 376 377 378 379 380

		/* Try again later.  */
		if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
			sctp_transport_hold(transport);
		goto out_unlock;
	}

381 382 383 384 385 386 387 388 389 390
	/* Check if we should still send the heartbeat or reschedule */
	elapsed = jiffies - transport->last_time_sent;
	timeout = sctp_transport_timeout(transport);
	if (elapsed < timeout) {
		elapsed = timeout - elapsed;
		if (!mod_timer(&transport->hb_timer, jiffies + elapsed))
			sctp_transport_hold(transport);
		goto out_unlock;
	}

391
	error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT,
Linus Torvalds's avatar
Linus Torvalds committed
392 393 394 395
			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
			   asoc->state, asoc->ep, asoc,
			   transport, GFP_ATOMIC);

Karl Heiss's avatar
Karl Heiss committed
396
	if (error)
397
		sk->sk_err = -error;
Linus Torvalds's avatar
Linus Torvalds committed
398 399

out_unlock:
400
	bh_unlock_sock(sk);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403
	sctp_transport_put(transport);
}

404 405 406 407 408
/* Handle the timeout of the ICMP protocol unreachable timer.  Trigger
 * the correct state machine transition that will close the association.
 */
void sctp_generate_proto_unreach_event(unsigned long data)
{
409
	struct sctp_transport *transport = (struct sctp_transport *)data;
410
	struct sctp_association *asoc = transport->asoc;
411 412
	struct sock *sk = asoc->base.sk;
	struct net *net = sock_net(sk);
413

414 415
	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
416
		pr_debug("%s: sock is busy\n", __func__);
417 418 419 420 421 422 423 424 425 426 427 428 429 430

		/* Try again later.  */
		if (!mod_timer(&transport->proto_unreach_timer,
				jiffies + (HZ/20)))
			sctp_association_hold(asoc);
		goto out_unlock;
	}

	/* Is this structure just waiting around for us to actually
	 * get destroyed?
	 */
	if (asoc->base.dead)
		goto out_unlock;

431
	sctp_do_sm(net, SCTP_EVENT_T_OTHER,
432 433 434 435
		   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
		   asoc->state, asoc->ep, asoc, transport, GFP_ATOMIC);

out_unlock:
436
	bh_unlock_sock(sk);
437 438 439
	sctp_association_put(asoc);
}

Xin Long's avatar
Xin Long committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
 /* Handle the timeout of the RE-CONFIG timer. */
void sctp_generate_reconf_event(unsigned long data)
{
	struct sctp_transport *transport = (struct sctp_transport *)data;
	struct sctp_association *asoc = transport->asoc;
	struct sock *sk = asoc->base.sk;
	struct net *net = sock_net(sk);
	int error = 0;

	bh_lock_sock(sk);
	if (sock_owned_by_user(sk)) {
		pr_debug("%s: sock is busy\n", __func__);

		/* Try again later.  */
		if (!mod_timer(&transport->reconf_timer, jiffies + (HZ / 20)))
			sctp_transport_hold(transport);
		goto out_unlock;
	}

	error = sctp_do_sm(net, SCTP_EVENT_T_TIMEOUT,
			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_RECONF),
			   asoc->state, asoc->ep, asoc,
			   transport, GFP_ATOMIC);

	if (error)
		sk->sk_err = -error;

out_unlock:
	bh_unlock_sock(sk);
	sctp_transport_put(transport);
}
471

Linus Torvalds's avatar
Linus Torvalds committed
472 473 474
/* Inject a SACK Timeout event into the state machine.  */
static void sctp_generate_sack_event(unsigned long data)
{
475
	struct sctp_association *asoc = (struct sctp_association *)data;
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478 479 480 481 482 483 484 485 486
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK);
}

sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES] = {
	NULL,
	sctp_generate_t1_cookie_event,
	sctp_generate_t1_init_event,
	sctp_generate_t2_shutdown_event,
	NULL,
	sctp_generate_t4_rto_event,
	sctp_generate_t5_shutdown_guard_event,
487
	NULL,
Xin Long's avatar
Xin Long committed
488
	NULL,
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	sctp_generate_sack_event,
	sctp_generate_autoclose_event,
};


/* RFC 2960 8.2 Path Failure Detection
 *
 * When its peer endpoint is multi-homed, an endpoint should keep a
 * error counter for each of the destination transport addresses of the
 * peer endpoint.
 *
 * Each time the T3-rtx timer expires on any address, or when a
 * HEARTBEAT sent to an idle address is not acknowledged within a RTO,
 * the error counter of that destination address will be incremented.
 * When the value in the error counter exceeds the protocol parameter
 * 'Path.Max.Retrans' of that destination address, the endpoint should
 * mark the destination transport address as inactive, and a
 * notification SHOULD be sent to the upper layer.
 *
 */
509
static void sctp_do_8_2_transport_strike(struct sctp_cmd_seq *commands,
510
					 struct sctp_association *asoc,
511 512
					 struct sctp_transport *transport,
					 int is_hb)
Linus Torvalds's avatar
Linus Torvalds committed
513
{
514 515
	struct net *net = sock_net(asoc->base.sk);

Linus Torvalds's avatar
Linus Torvalds committed
516 517 518
	/* The check for association's overall error counter exceeding the
	 * threshold is done in the state function.
	 */
519 520 521 522 523
	/* We are here due to a timer expiration.  If the timer was
	 * not a HEARTBEAT, then normal error tracking is done.
	 * If the timer was a heartbeat, we only increment error counts
	 * when we already have an outstanding HEARTBEAT that has not
	 * been acknowledged.
Lucas De Marchi's avatar
Lucas De Marchi committed
524
	 * Additionally, some tranport states inhibit error increments.
525
	 */
526
	if (!is_hb) {
527
		asoc->overall_error_count++;
528 529 530 531 532 533 534 535
		if (transport->state != SCTP_INACTIVE)
			transport->error_count++;
	 } else if (transport->hb_sent) {
		if (transport->state != SCTP_UNCONFIRMED)
			asoc->overall_error_count++;
		if (transport->state != SCTP_INACTIVE)
			transport->error_count++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
536

537
	/* If the transport error count is greater than the pf_retrans
538
	 * threshold, and less than pathmaxrtx, and if the current state
539 540
	 * is SCTP_ACTIVE, then mark this transport as Partially Failed,
	 * see SCTP Quick Failover Draft, section 5.1
541
	 */
542 543
	if (net->sctp.pf_enable &&
	   (transport->state == SCTP_ACTIVE) &&
544 545 546 547 548 549 550 551
	   (asoc->pf_retrans < transport->pathmaxrxt) &&
	   (transport->error_count > asoc->pf_retrans)) {

		sctp_assoc_control_transport(asoc, transport,
					     SCTP_TRANSPORT_PF,
					     0);

		/* Update the hb timer to resend a heartbeat every rto */
552
		sctp_transport_reset_hb_timer(transport);
553 554
	}

555
	if (transport->state != SCTP_INACTIVE &&
556
	    (transport->error_count > transport->pathmaxrxt)) {
557 558 559
		pr_debug("%s: association:%p transport addr:%pISpc failed\n",
			 __func__, asoc, &transport->ipaddr.sa);

Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567 568
		sctp_assoc_control_transport(asoc, transport,
					     SCTP_TRANSPORT_DOWN,
					     SCTP_FAILED_THRESHOLD);
	}

	/* E2) For the destination address for which the timer
	 * expires, set RTO <- RTO * 2 ("back off the timer").  The
	 * maximum value discussed in rule C7 above (RTO.max) may be
	 * used to provide an upper bound to this doubling operation.
569 570
	 *
	 * Special Case:  the first HB doesn't trigger exponential backoff.
571
	 * The first unacknowledged HB triggers it.  We do this with a flag
572
	 * that indicates that we have an outstanding HB.
Linus Torvalds's avatar
Linus Torvalds committed
573
	 */
574
	if (!is_hb || transport->hb_sent) {
575
		transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
576
		sctp_max_rto(asoc, transport);
577
	}
Linus Torvalds's avatar
Linus Torvalds committed
578 579 580
}

/* Worker routine to handle INIT command failure.  */
581
static void sctp_cmd_init_failed(struct sctp_cmd_seq *commands,
Linus Torvalds's avatar
Linus Torvalds committed
582
				 struct sctp_association *asoc,
583
				 unsigned int error)
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586
{
	struct sctp_ulpevent *event;

587
	event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_CANT_STR_ASSOC,
588
						(__u16)error, 0, 0, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
						GFP_ATOMIC);

	if (event)
		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
				SCTP_ULPEVENT(event));

	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
			SCTP_STATE(SCTP_STATE_CLOSED));

	/* SEND_FAILED sent later when cleaning up the association. */
	asoc->outqueue.error = error;
	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
}

/* Worker routine to handle SCTP_CMD_ASSOC_FAILED.  */
604
static void sctp_cmd_assoc_failed(struct sctp_cmd_seq *commands,
Linus Torvalds's avatar
Linus Torvalds committed
605
				  struct sctp_association *asoc,
606
				  enum sctp_event event_type,
607
				  union sctp_subtype subtype,
Linus Torvalds's avatar
Linus Torvalds committed
608
				  struct sctp_chunk *chunk,
609
				  unsigned int error)
Linus Torvalds's avatar
Linus Torvalds committed
610 611
{
	struct sctp_ulpevent *event;
612
	struct sctp_chunk *abort;
613

Linus Torvalds's avatar
Linus Torvalds committed
614 615 616
	/* Cancel any partial delivery in progress. */
	sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);

617 618 619 620 621 622 623
	if (event_type == SCTP_EVENT_T_CHUNK && subtype.chunk == SCTP_CID_ABORT)
		event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
						(__u16)error, 0, 0, chunk,
						GFP_ATOMIC);
	else
		event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
						(__u16)error, 0, 0, NULL,
Linus Torvalds's avatar
Linus Torvalds committed
624 625 626 627 628
						GFP_ATOMIC);
	if (event)
		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
				SCTP_ULPEVENT(event));

629 630 631 632 633 634 635
	if (asoc->overall_error_count >= asoc->max_retrans) {
		abort = sctp_make_violation_max_retrans(asoc, chunk);
		if (abort)
			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
					SCTP_CHUNK(abort));
	}

Linus Torvalds's avatar
Linus Torvalds committed
636 637 638 639 640 641 642 643 644 645 646 647 648
	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
			SCTP_STATE(SCTP_STATE_CLOSED));

	/* SEND_FAILED sent later when cleaning up the association. */
	asoc->outqueue.error = error;
	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
}

/* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
 * inside the cookie.  In reality, this is only used for INIT-ACK processing
 * since all other cases use "temporary" associations and can do all
 * their work in statefuns directly.
 */
649
static int sctp_cmd_process_init(struct sctp_cmd_seq *commands,
Linus Torvalds's avatar
Linus Torvalds committed
650 651
				 struct sctp_association *asoc,
				 struct sctp_chunk *chunk,
652
				 struct sctp_init_chunk *peer_init,
653
				 gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658 659 660 661
{
	int error;

	/* We only process the init as a sideeffect in a single
	 * case.   This is when we process the INIT-ACK.   If we
	 * fail during INIT processing (due to malloc problems),
	 * just return the error and stop processing the stack.
	 */
662
	if (!sctp_process_init(asoc, chunk, sctp_source(chunk), peer_init, gfp))
Linus Torvalds's avatar
Linus Torvalds committed
663 664 665 666 667 668 669 670
		error = -ENOMEM;
	else
		error = 0;

	return error;
}

/* Helper function to break out starting up of heartbeat timers.  */
671
static void sctp_cmd_hb_timers_start(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
672 673 674 675 676 677 678 679
				     struct sctp_association *asoc)
{
	struct sctp_transport *t;

	/* Start a heartbeat timer for each transport on the association.
	 * hold a reference on the transport to make sure none of
	 * the needed data structures go away.
	 */
680 681
	list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
		sctp_transport_reset_hb_timer(t);
Linus Torvalds's avatar
Linus Torvalds committed
682 683
}

684
static void sctp_cmd_hb_timers_stop(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
685 686 687 688 689 690
				    struct sctp_association *asoc)
{
	struct sctp_transport *t;

	/* Stop all heartbeat timers. */

691 692
	list_for_each_entry(t, &asoc->peer.transport_addr_list,
			transports) {
Linus Torvalds's avatar
Linus Torvalds committed
693 694 695 696 697 698
		if (del_timer(&t->hb_timer))
			sctp_transport_put(t);
	}
}

/* Helper function to stop any pending T3-RTX timers */
699
static void sctp_cmd_t3_rtx_timers_stop(struct sctp_cmd_seq *cmds,
700
					struct sctp_association *asoc)
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703
{
	struct sctp_transport *t;

704 705
	list_for_each_entry(t, &asoc->peer.transport_addr_list,
			transports) {
706
		if (del_timer(&t->T3_rtx_timer))
Linus Torvalds's avatar
Linus Torvalds committed
707 708 709 710 711 712
			sctp_transport_put(t);
	}
}


/* Helper function to handle the reception of an HEARTBEAT ACK.  */
713
static void sctp_cmd_transport_on(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717
				  struct sctp_association *asoc,
				  struct sctp_transport *t,
				  struct sctp_chunk *chunk)
{
718
	struct sctp_sender_hb_info *hbinfo;
719
	int was_unconfirmed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
720 721 722 723 724 725

	/* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the
	 * HEARTBEAT should clear the error counter of the destination
	 * transport address to which the HEARTBEAT was sent.
	 */
	t->error_count = 0;
726 727 728 729 730 731 732 733 734

	/*
	 * Although RFC4960 specifies that the overall error count must
	 * be cleared when a HEARTBEAT ACK is received, we make an
	 * exception while in SHUTDOWN PENDING. If the peer keeps its
	 * window shut forever, we may never be able to transmit our
	 * outstanding data and rely on the retransmission limit be reached
	 * to shutdown the association.
	 */
735
	if (t->asoc->state < SCTP_STATE_SHUTDOWN_PENDING)
736
		t->asoc->overall_error_count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
737

738 739 740 741 742
	/* Clear the hb_sent flag to signal that we had a good
	 * acknowledgement.
	 */
	t->hb_sent = 0;

Linus Torvalds's avatar
Linus Torvalds committed
743 744 745
	/* Mark the destination transport address as active if it is not so
	 * marked.
	 */
746 747
	if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED)) {
		was_unconfirmed = 1;
Linus Torvalds's avatar
Linus Torvalds committed
748 749
		sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
					     SCTP_HEARTBEAT_SUCCESS);
750
	}
Linus Torvalds's avatar
Linus Torvalds committed
751

752 753 754 755
	if (t->state == SCTP_PF)
		sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
					     SCTP_HEARTBEAT_SUCCESS);

756 757 758 759
	/* HB-ACK was received for a the proper HB.  Consider this
	 * forward progress.
	 */
	if (t->dst)
760
		sctp_transport_dst_confirm(t);
761

Linus Torvalds's avatar
Linus Torvalds committed
762 763 764
	/* The receiver of the HEARTBEAT ACK should also perform an
	 * RTT measurement for that destination transport address
	 * using the time value carried in the HEARTBEAT ACK chunk.
765 766 767
	 * If the transport's rto_pending variable has been cleared,
	 * it was most likely due to a retransmit.  However, we want
	 * to re-enable it to properly update the rto.
Linus Torvalds's avatar
Linus Torvalds committed
768
	 */
769 770 771
	if (t->rto_pending == 0)
		t->rto_pending = 1;

772
	hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
Linus Torvalds's avatar
Linus Torvalds committed
773
	sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
774 775

	/* Update the heartbeat timer.  */
776
	sctp_transport_reset_hb_timer(t);
777 778 779

	if (was_unconfirmed && asoc->peer.transport_count == 1)
		sctp_transport_immediate_rtx(t);
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783
}


/* Helper function to process the process SACK command.  */
784
static int sctp_cmd_process_sack(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
785
				 struct sctp_association *asoc,
786
				 struct sctp_chunk *chunk)
Linus Torvalds's avatar
Linus Torvalds committed
787
{
788
	int err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
789

790
	if (sctp_outq_sack(&asoc->outqueue, chunk)) {
791 792
		struct net *net = sock_net(asoc->base.sk);

Linus Torvalds's avatar
Linus Torvalds committed
793
		/* There are no more TSNs awaiting SACK.  */
794
		err = sctp_do_sm(net, SCTP_EVENT_T_OTHER,
Linus Torvalds's avatar
Linus Torvalds committed
795 796 797 798 799 800 801 802 803 804 805
				 SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
				 asoc->state, asoc->ep, asoc, NULL,
				 GFP_ATOMIC);
	}

	return err;
}

/* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
 * the transport for a shutdown chunk.
 */
806
static void sctp_cmd_setup_t2(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
807 808 809 810 811
			      struct sctp_association *asoc,
			      struct sctp_chunk *chunk)
{
	struct sctp_transport *t;

812 813 814 815
	if (chunk->transport)
		t = chunk->transport;
	else {
		t = sctp_assoc_choose_alter_transport(asoc,
816
					      asoc->shutdown_last_sent_to);
817 818
		chunk->transport = t;
	}
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821 822
	asoc->shutdown_last_sent_to = t;
	asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
}

823
static void sctp_cmd_assoc_update(struct sctp_cmd_seq *cmds,
824 825 826 827 828 829 830 831 832
				  struct sctp_association *asoc,
				  struct sctp_association *new)
{
	struct net *net = sock_net(asoc->base.sk);
	struct sctp_chunk *abort;

	if (!sctp_assoc_update(asoc, new))
		return;

833
	abort = sctp_make_abort(asoc, NULL, sizeof(struct sctp_errhdr));
834 835 836 837 838 839 840 841 842 843 844
	if (abort) {
		sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
		sctp_add_cmd_sf(cmds, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
	}
	sctp_add_cmd_sf(cmds, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNABORTED));
	sctp_add_cmd_sf(cmds, SCTP_CMD_ASSOC_FAILED,
			SCTP_PERR(SCTP_ERROR_RSRC_LOW));
	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
}

Linus Torvalds's avatar
Linus Torvalds committed
845
/* Helper function to change the state of an association. */
846
static void sctp_cmd_new_state(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
847
			       struct sctp_association *asoc,
848
			       enum sctp_state state)
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853
{
	struct sock *sk = asoc->base.sk;

	asoc->state = state;

854
	pr_debug("%s: asoc:%p[%s]\n", __func__, asoc, sctp_state_tbl[state]);
855

Linus Torvalds's avatar
Linus Torvalds committed
856
	if (sctp_style(sk, TCP)) {
857
		/* Change the sk->sk_state of a TCP-style socket that has
858
		 * successfully completed a connect() call.
Linus Torvalds's avatar
Linus Torvalds committed
859 860 861 862 863 864
		 */
		if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED))
			sk->sk_state = SCTP_SS_ESTABLISHED;

		/* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */
		if (sctp_state(asoc, SHUTDOWN_RECEIVED) &&
865 866
		    sctp_sstate(sk, ESTABLISHED)) {
			sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds's avatar
Linus Torvalds committed
867
			sk->sk_shutdown |= RCV_SHUTDOWN;
868
		}
Linus Torvalds's avatar
Linus Torvalds committed
869 870
	}

871 872 873 874 875
	if (sctp_state(asoc, COOKIE_WAIT)) {
		/* Reset init timeouts since they may have been
		 * increased due to timer expirations.
		 */
		asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
876
						asoc->rto_initial;
877
		asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
878
						asoc->rto_initial;
879 880
	}

881 882 883 884 885
	if (sctp_state(asoc, ESTABLISHED)) {
		kfree(asoc->peer.cookie);
		asoc->peer.cookie = NULL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890
	if (sctp_state(asoc, ESTABLISHED) ||
	    sctp_state(asoc, CLOSED) ||
	    sctp_state(asoc, SHUTDOWN_RECEIVED)) {
		/* Wake up any processes waiting in the asoc's wait queue in
		 * sctp_wait_for_connect() or sctp_wait_for_sndbuf().
891
		 */
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896 897 898 899 900 901 902 903
		if (waitqueue_active(&asoc->wait))
			wake_up_interruptible(&asoc->wait);

		/* Wake up any processes waiting in the sk's sleep queue of
		 * a TCP-style or UDP-style peeled-off socket in
		 * sctp_wait_for_accept() or sctp_wait_for_packet().
		 * For a UDP-style socket, the waiters are woken up by the
		 * notifications.
		 */
		if (!sctp_style(sk, UDP))
			sk->sk_state_change(sk);
	}
904 905 906 907

	if (sctp_state(asoc, SHUTDOWN_PENDING) &&
	    !sctp_outq_is_empty(&asoc->outqueue))
		sctp_outq_uncork(&asoc->outqueue, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910
}

/* Helper function to delete an association. */
911
static void sctp_cmd_delete_tcb(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
912 913 914 915 916
				struct sctp_association *asoc)
{
	struct sock *sk = asoc->base.sk;

	/* If it is a non-temporary association belonging to a TCP-style
917
	 * listening socket that is not closed, do not free it so that accept()
Linus Torvalds's avatar
Linus Torvalds committed
918
	 * can pick it up later.
919
	 */
Linus Torvalds's avatar
Linus Torvalds committed
920 921 922 923 924 925 926 927 928 929 930
	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
	    (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
		return;

	sctp_association_free(asoc);
}

/*
 * ADDIP Section 4.1 ASCONF Chunk Procedures
 * A4) Start a T-4 RTO timer, using the RTO value of the selected
 * destination address (we use active path instead of primary path just
931
 * because primary path may be inactive.
Linus Torvalds's avatar
Linus Torvalds committed
932
 */
933 934 935
static void sctp_cmd_setup_t4(struct sctp_cmd_seq *cmds,
			      struct sctp_association *asoc,
			      struct sctp_chunk *chunk)
Linus Torvalds's avatar
Linus Torvalds committed
936 937 938
{
	struct sctp_transport *t;

939
	t = sctp_assoc_choose_alter_transport(asoc, chunk->transport);
Linus Torvalds's avatar
Linus Torvalds committed
940 941 942 943
	asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
	chunk->transport = t;
}

944
/* Process an incoming Operation Error Chunk. */
945
static void sctp_cmd_process_operr(struct sctp_cmd_seq *cmds,
Linus Torvalds's avatar
Linus Torvalds committed
946 947 948 949
				   struct sctp_association *asoc,
				   struct sctp_chunk *chunk)
{
	struct sctp_errhdr *err_hdr;
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	struct sctp_ulpevent *ev;

	while (chunk->chunk_end > chunk->skb->data) {
		err_hdr = (struct sctp_errhdr *)(chunk->skb->data);

		ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
						     GFP_ATOMIC);
		if (!ev)
			return;

		sctp_ulpq_tail_event(&asoc->ulpq, ev);

		switch (err_hdr->cause) {
		case SCTP_ERROR_UNKNOWN_CHUNK:
		{
965
			struct sctp_chunkhdr *unk_chunk_hdr;
966

967 968
			unk_chunk_hdr = (struct sctp_chunkhdr *)
							err_hdr->variable;
969 970 971 972 973 974 975 976 977 978 979 980 981
			switch (unk_chunk_hdr->type) {
			/* ADDIP 4.1 A9) If the peer responds to an ASCONF with
			 * an ERROR chunk reporting that it did not recognized
			 * the ASCONF chunk type, the sender of the ASCONF MUST
			 * NOT send any further ASCONF chunks and MUST stop its
			 * T-4 timer.
			 */
			case SCTP_CID_ASCONF:
				if (asoc->peer.asconf_capable == 0)
					break;

				asoc->peer.asconf_capable = 0;
				sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
Linus Torvalds's avatar
Linus Torvalds committed
982
					SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
983 984 985 986
				break;
			default:
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
987
			break;
988
		}
Linus Torvalds's avatar
Linus Torvalds committed
989 990 991 992 993 994 995
		default:
			break;
		}
	}
}

/* Process variable FWDTSN chunk information. */
996
static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
Linus Torvalds's avatar
Linus Torvalds committed
997 998 999
				    struct sctp_chunk *chunk)
{
	struct sctp_fwdtsn_skip *skip;
1000

Linus Torvalds's avatar
Linus Torvalds committed
1001 1002 1003 1004 1005 1006
	/* Walk through all the skipped SSNs */
	sctp_walk_fwdtsn(skip, chunk) {
		sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
	}
}

1007
/* Helper function to remove the association non-primary peer
Linus Torvalds's avatar
Linus Torvalds committed
1008
 * transports.
1009
 */
Linus Torvalds's avatar
Linus Torvalds committed
1010 1011 1012 1013
static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
{
	struct sctp_transport *t;
	struct list_head *temp;
1014
	struct list_head *pos;
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016 1017

	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
		t = list_entry(pos, struct sctp_transport, transports);
1018
		if (!sctp_cmp_addr_exact(&t->ipaddr,
1019
					 &asoc->peer.primary_addr)) {
1020
			sctp_assoc_rm_peer(asoc, t);
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024
		}
	}
}

1025 1026 1027 1028 1029 1030 1031 1032 1033
/* Helper function to set sk_err on a 1-1 style socket. */
static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
{
	struct sock *sk = asoc->base.sk;

	if (!sctp_style(sk, UDP))
		sk->sk_err = error;
}

1034
/* Helper function to generate an association change event */
1035 1036 1037
static void sctp_cmd_assoc_change(struct sctp_cmd_seq *commands,
				  struct sctp_association *asoc,
				  u8 state)
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
	struct sctp_ulpevent *ev;

	ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
					    asoc->c.sinit_num_ostreams,
					    asoc->c.sinit_max_instreams,
					    NULL, GFP_ATOMIC);
	if (ev)
		sctp_ulpq_tail_event(&asoc->ulpq, ev);
}

/* Helper function to generate an adaptation indication event */
1050
static void sctp_cmd_adaptation_ind(struct sctp_cmd_seq *commands,
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
				    struct sctp_association *asoc)
{
	struct sctp_ulpevent *ev;

	ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);

	if (ev)
		sctp_ulpq_tail_event(&asoc->ulpq, ev);
}

1061 1062

static void sctp_cmd_t1_timer_update(struct sctp_association *asoc,
1063 1064
				     enum sctp_event_timeout timer,
				     char *name)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
{
	struct sctp_transport *t;

	t = asoc->init_last_sent_to;
	asoc->init_err_counter++;

	if (t->init_sent_count > (asoc->init_cycle + 1)) {
		asoc->timeouts[timer] *= 2;
		if (asoc->timeouts[timer] > asoc->max_init_timeo) {
			asoc->timeouts[timer] = asoc->max_init_timeo;
		}
		asoc->init_cycle++;
1077 1078 1079 1080 1081

		pr_debug("%s: T1[%s] timeout adjustment init_err_counter:%d"
			 " cycle:%d timeout:%ld\n", __func__, name,
			 asoc->init_err_counter, asoc->init_cycle,
			 asoc->timeouts[timer]);
1082 1083 1084 1085
	}

}

1086 1087 1088 1089
/* Send the whole message, chunk by chunk, to the outqueue.
 * This way the whole message is queued up and bundling if
 * encouraged for small fragments.
 */
1090 1091
static void sctp_cmd_send_msg(struct sctp_association *asoc,
			      struct sctp_datamsg *msg, gfp_t gfp)
1092 1093 1094
{
	struct sctp_chunk *chunk;

1095 1096
	list_for_each_entry(chunk, &msg->chunks, frag_list)
		sctp_outq_tail(&asoc->outqueue, chunk, gfp);
1097 1098 1099
}


Linus Torvalds's avatar
Linus Torvalds committed
1100 1101 1102 1103
/* These three macros allow us to pull the debugging code out of the
 * main flow of sctp_do_sm() to keep attention focused on the real
 * functionality there.
 */
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
#define debug_pre_sfn() \
	pr_debug("%s[pre-fn]: ep:%p, %s, %s, asoc:%p[%s], %s\n", __func__, \
		 ep, sctp_evttype_tbl[event_type], (*debug_fn)(subtype),   \
		 asoc, sctp_state_tbl[state], state_fn->name)

#define debug_post_sfn() \
	pr_debug("%s[post-fn]: asoc:%p, status:%s\n", __func__, asoc, \
		 sctp_status_tbl[status])

#define debug_post_sfx() \
	pr_debug("%s[post-sfx]: error:%d, asoc:%p[%s]\n", __func__, error, \
		 asoc, sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
		 sctp_assoc2id(asoc))) ? asoc->state : SCTP_STATE_CLOSED])
Linus Torvalds's avatar
Linus Torvalds committed
1117 1118 1119 1120 1121 1122 1123

/*
 * This is the master state machine processing function.
 *
 * If you want to understand all of lksctp, this is a
 * good place to start.
 */
1124
int sctp_do_sm(struct net *net, enum sctp_event event_type,
1125
	       union sctp_subtype subtype, enum sctp_state state,
1126 1127
	       struct sctp_endpoint *ep, struct sctp_association *asoc,
	       void *event_arg, gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
1128
{
1129
	typedef const char *(printfn_t)(union sctp_subtype);
Linus Torvalds's avatar
Linus Torvalds committed
1130 1131 1132 1133
	static printfn_t *table[] = {
		NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
	};
	printfn_t *debug_fn  __attribute__ ((unused)) = table[event_type];
1134 1135 1136 1137
	const struct sctp_sm_table_entry *state_fn;
	struct sctp_cmd_seq commands;
	enum sctp_disposition status;
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1138 1139 1140 1141

	/* Look up the state function, run it, and then process the
	 * side effects.  These three steps are the heart of lksctp.
	 */
1142
	state_fn = sctp_sm_lookup_event(net, event_type, state, subtype);
Linus Torvalds's avatar
Linus Torvalds committed
1143 1144 1145

	sctp_init_cmd_seq(&commands);

1146
	debug_pre_sfn();
1147
	status = state_fn->fn(net, ep, asoc, subtype, event_arg, &commands);
1148
	debug_post_sfn();
Linus Torvalds's avatar
Linus Torvalds committed
1149 1150

	error = sctp_side_effects(event_type, subtype, state,
1151
				  ep, &asoc, event_arg, status,
Linus Torvalds's avatar
Linus Torvalds committed
1152
				  &commands, gfp);
1153
	debug_post_sfx();
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155 1156 1157 1158 1159 1160

	return error;
}

/*****************************************************************
 * This the master state function side effect processing function.
 *****************************************************************/
1161 1162
static int sctp_side_effects(enum sctp_event event_type,
			     union sctp_subtype subtype,
1163
			     enum sctp_state state,
Linus Torvalds's avatar
Linus Torvalds committed
1164
			     struct sctp_endpoint *ep,
1165
			     struct sctp_association **asoc,
Linus Torvalds's avatar
Linus Torvalds committed
1166
			     void *event_arg,
1167
			     enum sctp_disposition status,
1168
			     struct sctp_cmd_seq *commands,
1169
			     gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
{
	int error;

	/* FIXME - Most of the dispositions left today would be categorized
	 * as "exceptional" dispositions.  For those dispositions, it
	 * may not be proper to run through any of the commands at all.
	 * For example, the command interpreter might be run only with
	 * disposition SCTP_DISPOSITION_CONSUME.
	 */
	if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
1180
					       ep, *asoc,
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183 1184 1185 1186
					       event_arg, status,
					       commands, gfp)))
		goto bail;

	switch (status) {
	case SCTP_DISPOSITION_DISCARD:
1187 1188 1189
		pr_debug("%s: ignored sctp protocol event - state:%d, "
			 "event_type:%d, event_id:%d\n", __func__, state,
			 event_type, subtype.chunk);
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
		break;

	case SCTP_DISPOSITION_NOMEM:
		/* We ran out of memory, so we need to discard this
		 * packet.
		 */
		/* BUG--we should now recover some memory, probably by
		 * reneging...
		 */
		error = -ENOMEM;
		break;

1202
	case SCTP_DISPOSITION_DELETE_TCB:
1203
	case SCTP_DISPOSITION_ABORT:
Linus Torvalds's avatar
Linus Torvalds committed
1204
		/* This should now be a command. */
1205
		*asoc = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
		break;

	case SCTP_DISPOSITION_CONSUME:
		/*
		 * We should no longer have much work to do here as the
		 * real work has been done as explicit commands above.
		 */
		break;

	case SCTP_DISPOSITION_VIOLATION:
1216 1217
		net_err_ratelimited("protocol violation state %d chunkid %d\n",
				    state, subtype.chunk);
Linus Torvalds's avatar
Linus Torvalds committed
1218 1219 1220
		break;

	case SCTP_DISPOSITION_NOT_IMPL:
1221 1222
		pr_warn("unimplemented feature in state %d, event_type %d, event_id %d\n",
			state, event_type, subtype.chunk);
Linus Torvalds's avatar
Linus Torvalds committed
1223 1224 1225
		break;

	case SCTP_DISPOSITION_BUG:
1226
		pr_err("bug in state %d, event_type %d, event_id %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
1227 1228 1229 1230 1231
		       state, event_type, subtype.chunk);
		BUG();
		break;

	default:
1232
		pr_err("impossible disposition %d in state %d, event_type %d, event_id %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
1233 1234 1235
		       status, state, event_type, subtype.chunk);
		BUG();
		break;
1236
	}
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

bail:
	return error;
}

/********************************************************************
 * 2nd Level Abstractions
 ********************************************************************/

/* This is the side-effect interpreter.  */
1247
static int sctp_cmd_interpreter(enum sctp_event event_type,
1248
				union sctp_subtype subtype,