sm_sideeffect.c 35.4 KB
Newer Older
Jon Grimm's avatar
Jon Grimm committed
1
/* SCTP kernel reference Implementation
Jon Grimm's avatar
Jon Grimm committed
2 3 4
 * Copyright (c) 1999 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001-2002 International Business Machines Corp.
Jon Grimm's avatar
Jon Grimm committed
5
 *
Jon Grimm's avatar
Jon Grimm committed
6
 * This file is part of the SCTP kernel reference Implementation
Jon Grimm's avatar
Jon Grimm committed
7
 *
Jon Grimm's avatar
Jon Grimm committed
8 9 10
 * 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.
Jon Grimm's avatar
Jon Grimm committed
11 12 13
 *
 * The SCTP reference implementation is free software;
 * you can redistribute it and/or modify it under the terms of
Jon Grimm's avatar
Jon Grimm committed
14 15 16
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
Jon Grimm's avatar
Jon Grimm committed
17 18
 *
 * The SCTP reference implementation is distributed in the hope that it
Jon Grimm's avatar
Jon Grimm committed
19 20 21 22
 * 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.
Jon Grimm's avatar
Jon Grimm committed
23
 *
Jon Grimm's avatar
Jon Grimm committed
24 25 26
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
Jon Grimm's avatar
Jon Grimm committed
27 28
 * Boston, MA 02111-1307, USA.
 *
Jon Grimm's avatar
Jon Grimm committed
29 30 31
 * Please send any bug reports or fixes you make to the
 * email address(es):
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
Jon Grimm's avatar
Jon Grimm committed
32
 *
Jon Grimm's avatar
Jon Grimm committed
33 34 35
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *
Jon Grimm's avatar
Jon Grimm committed
36
 * Written or modified by:
Jon Grimm's avatar
Jon Grimm committed
37 38 39 40 41 42 43
 *    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>
44
 *    Ardelle Fan	    <ardelle.fan@intel.com>
Jon Grimm's avatar
Jon Grimm committed
45 46 47 48 49 50 51 52 53 54 55
 *
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 */

#include <linux/skbuff.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/ip.h>
#include <net/sock.h>
#include <net/sctp/sctp.h>
56
#include <net/sctp/sm.h>
Jon Grimm's avatar
Jon Grimm committed
57 58 59

/* Do forward declarations of static functions.  */
static void sctp_do_ecn_ce_work(sctp_association_t *asoc,
60 61 62 63 64 65
				__u32 lowest_tsn);
static sctp_chunk_t *sctp_do_ecn_ecne_work(sctp_association_t *asoc,
					   __u32 lowest_tsn,
					   sctp_chunk_t *);
static void sctp_do_ecn_cwr_work(sctp_association_t *asoc,
				 __u32 lowest_tsn);
Jon Grimm's avatar
Jon Grimm committed
66 67 68

static void sctp_do_8_2_transport_strike(sctp_association_t *asoc,
					 sctp_transport_t *transport);
69
static void sctp_cmd_init_failed(sctp_cmd_seq_t *, sctp_association_t *asoc);
70
static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *, sctp_association_t *asoc,
71 72
				  sctp_event_t event_type, sctp_subtype_t stype,
				  sctp_chunk_t *chunk);
73 74 75 76
static int sctp_cmd_process_init(sctp_cmd_seq_t *, sctp_association_t *asoc,
				 sctp_chunk_t *chunk,
				 sctp_init_chunk_t *peer_init,
				 int priority);
77
static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *, sctp_association_t *);
78 79
static void sctp_cmd_hb_timers_update(sctp_cmd_seq_t *, sctp_association_t *,
				      sctp_transport_t *);
80 81 82 83 84 85 86 87 88 89
static void sctp_cmd_set_bind_addrs(sctp_cmd_seq_t *, sctp_association_t *,
				    sctp_bind_addr_t *);
static void sctp_cmd_transport_reset(sctp_cmd_seq_t *, sctp_association_t *,
				     sctp_transport_t *);
static void sctp_cmd_transport_on(sctp_cmd_seq_t *, sctp_association_t *,
				  sctp_transport_t *, sctp_chunk_t *);
static int sctp_cmd_process_sack(sctp_cmd_seq_t *, sctp_association_t *,
				 sctp_sackhdr_t *);
static void sctp_cmd_setup_t2(sctp_cmd_seq_t *, sctp_association_t *,
			      sctp_chunk_t *);
90 91
static void sctp_cmd_new_state(sctp_cmd_seq_t *, sctp_association_t *,
			       sctp_state_t);
Jon Grimm's avatar
Jon Grimm committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

/* 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.
 */
#define DEBUG_PRE \
	SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
			  "ep %p, %s, %s, asoc %p[%s], %s\n", \
			  ep, sctp_evttype_tbl[event_type], \
			  (*debug_fn)(subtype), asoc, \
			  sctp_state_tbl[state], state_fn->name)

#define DEBUG_POST \
	SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
			  "asoc %p, status: %s\n", \
			  asoc, sctp_status_tbl[status])
108

Jon Grimm's avatar
Jon Grimm committed
109 110 111 112 113 114 115 116 117 118 119 120
#define DEBUG_POST_SFX \
	SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
			  error, asoc, \
			  sctp_state_tbl[sctp_id2assoc(ep->base.sk, \
			  sctp_assoc2id(asoc))?asoc->state:SCTP_STATE_CLOSED])

/*
 * This is the master state machine processing function.
 *
 * If you want to understand all of lksctp, this is a
 * good place to start.
 */
121 122 123 124 125 126
int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
	       sctp_state_t state,
	       sctp_endpoint_t *ep,
	       sctp_association_t *asoc,
	       void *event_arg,
	       int priority)
Jon Grimm's avatar
Jon Grimm committed
127 128
{
	sctp_cmd_seq_t commands;
Jon Grimm's avatar
Jon Grimm committed
129
	sctp_sm_table_entry_t *state_fn;
130 131
	sctp_disposition_t status;
	int error = 0;
Jon Grimm's avatar
Jon Grimm committed
132 133 134 135 136 137 138 139 140 141
	typedef const char *(printfn_t)(sctp_subtype_t);

	static printfn_t *table[] = {
		NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
	};
	printfn_t *debug_fn  __attribute__ ((unused)) = table[event_type];

	/* Look up the state function, run it, and then process the
	 * side effects.  These three steps are the heart of lksctp.
	 */
142
	state_fn = sctp_sm_lookup_event(event_type, state, subtype);
Jon Grimm's avatar
Jon Grimm committed
143 144 145 146 147 148

	sctp_init_cmd_seq(&commands);

	DEBUG_PRE;
	status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
	DEBUG_POST;
149

Jon Grimm's avatar
Jon Grimm committed
150 151 152 153 154 155
	error = sctp_side_effects(event_type, subtype, state,
				  ep, asoc, event_arg,
				  status, &commands,
				  priority);
	DEBUG_POST_SFX;

156 157
	return error;
}
Jon Grimm's avatar
Jon Grimm committed
158 159 160 161 162 163 164

#undef DEBUG_PRE
#undef DEBUG_POST

/*****************************************************************
 * This the master state function side effect processing function.
 *****************************************************************/
165 166 167 168 169 170 171 172
int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
		      sctp_state_t state,
		      sctp_endpoint_t *ep,
		      sctp_association_t *asoc,
		      void *event_arg,
		      sctp_disposition_t status,
		      sctp_cmd_seq_t *commands,
		      int priority)
Jon Grimm's avatar
Jon Grimm committed
173
{
174 175 176 177
	int error;

	/* FIXME - Most of the dispositions left today would be categorized
	 * as "exceptional" dispositions.  For those dispositions, it
Jon Grimm's avatar
Jon Grimm committed
178 179 180 181 182 183 184
	 * 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,
					       ep, asoc,
					       event_arg, status,
185
					       commands, priority)))
Jon Grimm's avatar
Jon Grimm committed
186 187
		goto bail;

188 189
	switch (status) {
	case SCTP_DISPOSITION_DISCARD:
Jon Grimm's avatar
Jon Grimm committed
190 191 192
		SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
				  "event_type %d, event_id %d\n",
				  state, event_type, subtype.chunk);
193 194
		break;

Jon Grimm's avatar
Jon Grimm committed
195 196 197 198 199 200 201
	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...
		 */
202
		error = -ENOMEM;
Jon Grimm's avatar
Jon Grimm committed
203
		break;
204

Jon Grimm's avatar
Jon Grimm committed
205 206 207
        case SCTP_DISPOSITION_DELETE_TCB:
		/* This should now be a command. */
		break;
208 209 210 211 212

	case SCTP_DISPOSITION_CONSUME:
	case SCTP_DISPOSITION_ABORT:
		/*
		 * We should no longer have much work to do here as the
Jon Grimm's avatar
Jon Grimm committed
213 214 215
		 * real work has been done as explicit commands above.
		 */
		break;
216 217

	case SCTP_DISPOSITION_VIOLATION:
Jon Grimm's avatar
Jon Grimm committed
218 219
		printk(KERN_ERR "sctp protocol violation state %d "
		       "chunkid %d\n", state, subtype.chunk);
220 221 222
		break;

	case SCTP_DISPOSITION_NOT_IMPL:
Jon Grimm's avatar
Jon Grimm committed
223 224 225
		printk(KERN_WARNING "sctp unimplemented feature in state %d, "
		       "event_type %d, event_id %d\n",
		       state, event_type, subtype.chunk);
226 227
		break;

Jon Grimm's avatar
Jon Grimm committed
228 229 230 231 232 233
	case SCTP_DISPOSITION_BUG:
		printk(KERN_ERR "sctp bug in state %d, "
		       "event_type %d, event_id %d\n",
		       state, event_type, subtype.chunk);
		BUG();
		break;
234 235

	default:
Jon Grimm's avatar
Jon Grimm committed
236 237 238 239
		printk(KERN_ERR "sctp impossible disposition %d "
		       "in state %d, event_type %d, event_id %d\n",
		       status, state, event_type, subtype.chunk);
		BUG();
240 241
		break;
	};
Jon Grimm's avatar
Jon Grimm committed
242

243 244 245
bail:
	return error;
}
Jon Grimm's avatar
Jon Grimm committed
246 247 248 249 250 251

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

/* This is the side-effect interpreter.  */
252 253 254 255 256
int sctp_cmd_interpreter(sctp_event_t event_type, sctp_subtype_t subtype,
			 sctp_state_t state, sctp_endpoint_t *ep,
			 sctp_association_t *asoc, void *event_arg,
			 sctp_disposition_t status, sctp_cmd_seq_t *commands,
			 int priority)
Jon Grimm's avatar
Jon Grimm committed
257
{
258
	int error = 0;
Jon Grimm's avatar
Jon Grimm committed
259 260 261
	int force;
	sctp_cmd_t *command;
	sctp_chunk_t *new_obj;
262
	sctp_chunk_t *chunk = NULL;
Jon Grimm's avatar
Jon Grimm committed
263
	sctp_packet_t *packet;
Jon Grimm's avatar
Jon Grimm committed
264
	struct list_head *pos;
265 266 267 268
	struct timer_list *timer;
	unsigned long timeout;
	sctp_transport_t *t;
	sctp_sackhdr_t sackh;
Jon Grimm's avatar
Jon Grimm committed
269

270 271
	if(SCTP_EVENT_T_TIMEOUT != event_type)
		chunk = (sctp_chunk_t *) event_arg;
Jon Grimm's avatar
Jon Grimm committed
272

273 274 275
	/* Note:  This whole file is a huge candidate for rework.
	 * For example, each command could either have its own handler, so
	 * the loop would look like:
Jon Grimm's avatar
Jon Grimm committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	 *     while (cmds)
	 *         cmd->handle(x, y, z)
	 * --jgrimm
	 */
	while (NULL != (command = sctp_next_cmd(commands))) {
		switch (command->verb) {
		case SCTP_CMD_NOP:
			/* Do nothing. */
			break;

		case SCTP_CMD_NEW_ASOC:
			/* Register a new association.  */
			asoc = command->obj.ptr;
			/* Register with the endpoint.  */
			sctp_endpoint_add_asoc(ep, asoc);
			sctp_hash_established(asoc);
			break;

		case SCTP_CMD_UPDATE_ASSOC:
		       sctp_assoc_update(asoc, command->obj.ptr);
		       break;

		case SCTP_CMD_PURGE_OUTQUEUE:
		       sctp_outqueue_teardown(&asoc->outqueue);
		       break;
301

Jon Grimm's avatar
Jon Grimm committed
302 303 304 305 306 307 308 309
		case SCTP_CMD_DELETE_TCB:
			/* Delete the current association.  */
			sctp_unhash_established(asoc);
			sctp_association_free(asoc);
			asoc = NULL;
			break;

		case SCTP_CMD_NEW_STATE:
310
			/* Enter a new state.  */
311
			sctp_cmd_new_state(commands, asoc, command->obj.state);
Jon Grimm's avatar
Jon Grimm committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
			break;

		case SCTP_CMD_REPORT_TSN:
			/* Record the arrival of a TSN.  */
			sctp_tsnmap_mark(&asoc->peer.tsn_map,
					 command->obj.u32);
			break;

		case SCTP_CMD_GEN_SACK:
			/* Generate a Selective ACK.
			 * The argument tells us whether to just count
			 * the packet and MAYBE generate a SACK, or
			 * force a SACK out.
			 */
			force = command->obj.i32;
			error = sctp_gen_sack(asoc, force, commands);
			break;

		case SCTP_CMD_PROCESS_SACK:
			/* Process an inbound SACK.  */
332
			error = sctp_cmd_process_sack(commands, asoc,
Jon Grimm's avatar
Jon Grimm committed
333 334 335 336 337
						      command->obj.ptr);
			break;

		case SCTP_CMD_GEN_INIT_ACK:
			/* Generate an INIT ACK chunk.  */
338 339
			new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
						     0);
340 341
			if (!new_obj)
				goto nomem;
Jon Grimm's avatar
Jon Grimm committed
342

343
			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
Jon Grimm's avatar
Jon Grimm committed
344 345 346 347
					SCTP_CHUNK(new_obj));
			break;

		case SCTP_CMD_PEER_INIT:
348 349 350 351 352 353 354 355
			/* Process a unified INIT from the peer.
			 * Note: Only used during INIT-ACK processing.  If
			 * there is an error just return to the outter
			 * layer which will bail.
			 */
			error = sctp_cmd_process_init(commands, asoc, chunk,
						      command->obj.ptr,
						      priority);
Jon Grimm's avatar
Jon Grimm committed
356 357 358
			break;

		case SCTP_CMD_GEN_COOKIE_ECHO:
359
			/* Generate a COOKIE ECHO chunk.  */
Jon Grimm's avatar
Jon Grimm committed
360
			new_obj = sctp_make_cookie_echo(asoc, chunk);
361 362 363
			if (!new_obj) {
				if (command->obj.ptr)
					sctp_free_chunk(command->obj.ptr);
364
				goto nomem;
365
			}
366
			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
Jon Grimm's avatar
Jon Grimm committed
367
					SCTP_CHUNK(new_obj));
368 369 370 371 372 373 374

			/* If there is an ERROR chunk to be sent along with
			 * the COOKIE_ECHO, send it, too.
			 */
			if (command->obj.ptr)
				sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
						SCTP_CHUNK(command->obj.ptr));
Jon Grimm's avatar
Jon Grimm committed
375 376 377
			break;

		case SCTP_CMD_GEN_SHUTDOWN:
378 379
			/* Generate SHUTDOWN when in SHUTDOWN_SENT state.
			 * Reset error counts.
Jon Grimm's avatar
Jon Grimm committed
380 381 382
			 */
			asoc->overall_error_count = 0;

383
			/* Generate a SHUTDOWN chunk.  */
Jon Grimm's avatar
Jon Grimm committed
384
			new_obj = sctp_make_shutdown(asoc);
385 386 387
			if (!new_obj)
				goto nomem;
			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
Jon Grimm's avatar
Jon Grimm committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
					SCTP_CHUNK(new_obj));
			break;

		case SCTP_CMD_CHUNK_ULP:
			/* Send a chunk to the sockets layer.  */
			SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
					  "chunk_up:",
					  command->obj.ptr,
					  "ulpq:",
					  &asoc->ulpq);
			sctp_ulpqueue_tail_data(&asoc->ulpq,
						command->obj.ptr,
						GFP_ATOMIC);
			break;

		case SCTP_CMD_EVENT_ULP:
404
			/* Send a notification to the sockets layer.  */
Jon Grimm's avatar
Jon Grimm committed
405 406 407 408 409
			SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
					  "event_up:",
					  command->obj.ptr,
					  "ulpq:",
					  &asoc->ulpq);
410
			sctp_ulpqueue_tail_event(&asoc->ulpq,
Jon Grimm's avatar
Jon Grimm committed
411 412 413 414 415
						 command->obj.ptr);
			break;

		case SCTP_CMD_REPLY:
			/* Send a chunk to our peer.  */
416
			error = sctp_push_outqueue(&asoc->outqueue,
Jon Grimm's avatar
Jon Grimm committed
417 418 419 420 421 422
						   command->obj.ptr);
			break;

		case SCTP_CMD_SEND_PKT:
			/* Send a full packet to our peer.  */
			packet = command->obj.ptr;
423
			sctp_packet_transmit(packet);
424
			sctp_ootb_pkt_free(packet);
Jon Grimm's avatar
Jon Grimm committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			break;

		case SCTP_CMD_RETRAN:
			/* Mark a transport for retransmission.  */
			sctp_retransmit(&asoc->outqueue,
					command->obj.transport, 0);
			break;

		case SCTP_CMD_TRANSMIT:
			/* Kick start transmission. */
			error = sctp_flush_outqueue(&asoc->outqueue, 0);
			break;

		case SCTP_CMD_ECN_CE:
			/* Do delayed CE processing.   */
			sctp_do_ecn_ce_work(asoc, command->obj.u32);
			break;

		case SCTP_CMD_ECN_ECNE:
			/* Do delayed ECNE processing. */
			new_obj = sctp_do_ecn_ecne_work(asoc,
							command->obj.u32,
							chunk);
			if (new_obj) {
				sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
						SCTP_CHUNK(new_obj));
			}
			break;

		case SCTP_CMD_ECN_CWR:
			/* Do delayed CWR processing.  */
			sctp_do_ecn_cwr_work(asoc, command->obj.u32);
			break;

		case SCTP_CMD_SETUP_T2:
			sctp_cmd_setup_t2(commands, asoc, command->obj.ptr);
			break;

463
		case SCTP_CMD_TIMER_START:
Jon Grimm's avatar
Jon Grimm committed
464 465
			timer = &asoc->timers[command->obj.to];
			timeout = asoc->timeouts[command->obj.to];
466 467
			if (!timeout)
				BUG();
Jon Grimm's avatar
Jon Grimm committed
468

469
			timer->expires = jiffies + timeout;
Jon Grimm's avatar
Jon Grimm committed
470
			sctp_association_hold(asoc);
471 472
			add_timer(timer);
			break;
Jon Grimm's avatar
Jon Grimm committed
473

474
		case SCTP_CMD_TIMER_RESTART:
Jon Grimm's avatar
Jon Grimm committed
475 476
			timer = &asoc->timers[command->obj.to];
			timeout = asoc->timeouts[command->obj.to];
477
			if (!mod_timer(timer, jiffies + timeout))
Jon Grimm's avatar
Jon Grimm committed
478
				sctp_association_hold(asoc);
479
			break;
Jon Grimm's avatar
Jon Grimm committed
480

481
		case SCTP_CMD_TIMER_STOP:
Jon Grimm's avatar
Jon Grimm committed
482
			timer = &asoc->timers[command->obj.to];
483
			if (timer_pending(timer) && del_timer(timer))
Jon Grimm's avatar
Jon Grimm committed
484
				sctp_association_put(asoc);
485
			break;
Jon Grimm's avatar
Jon Grimm committed
486 487

		case SCTP_CMD_INIT_RESTART:
Jon Grimm's avatar
Jon Grimm committed
488

Jon Grimm's avatar
Jon Grimm committed
489 490
			/* Do the needed accounting and updates
			 * associated with restarting an initialization
491
			 * timer.
Jon Grimm's avatar
Jon Grimm committed
492 493 494
			 */
			asoc->counters[SCTP_COUNTER_INIT_ERROR]++;
			asoc->timeouts[command->obj.to] *= 2;
495 496 497 498
			if (asoc->timeouts[command->obj.to] >
			    asoc->max_init_timeo) {
				asoc->timeouts[command->obj.to] =
					asoc->max_init_timeo;
Jon Grimm's avatar
Jon Grimm committed
499
			}
500

Jon Grimm's avatar
Jon Grimm committed
501 502 503 504 505 506 507 508 509
			/* If we've sent any data bundled with
			 * COOKIE-ECHO we need to resend.
			 */
			list_for_each(pos, &asoc->peer.transport_addr_list) {
				t = list_entry(pos, sctp_transport_t,
					       transports);
				sctp_retransmit_mark(&asoc->outqueue, t, 0);
			}

Jon Grimm's avatar
Jon Grimm committed
510 511 512 513 514 515 516 517 518 519
			sctp_add_cmd_sf(commands,
					SCTP_CMD_TIMER_RESTART,
					SCTP_TO(command->obj.to));
			break;

		case SCTP_CMD_INIT_FAILED:
			sctp_cmd_init_failed(commands, asoc);
			break;

		case SCTP_CMD_ASSOC_FAILED:
520
			sctp_cmd_assoc_failed(commands, asoc, event_type,
521
					      subtype, chunk);
Jon Grimm's avatar
Jon Grimm committed
522 523 524 525 526
			break;

		case SCTP_CMD_COUNTER_INC:
			asoc->counters[command->obj.counter]++;
			break;
527

Jon Grimm's avatar
Jon Grimm committed
528
		case SCTP_CMD_COUNTER_RESET:
529
			asoc->counters[command->obj.counter] = 0;
Jon Grimm's avatar
Jon Grimm committed
530 531 532 533
			break;

		case SCTP_CMD_REPORT_DUP:
			if (asoc->peer.next_dup_tsn < SCTP_MAX_DUP_TSNS) {
534 535
				asoc->peer.dup_tsns[asoc->peer.next_dup_tsn++] =
					ntohl(command->obj.u32);
Jon Grimm's avatar
Jon Grimm committed
536 537 538 539 540 541 542 543 544
			}
			break;

		case SCTP_CMD_REPORT_BIGGAP:
			SCTP_DEBUG_PRINTK("Big gap: %x to %x\n",
					  sctp_tsnmap_get_ctsn(
						  &asoc->peer.tsn_map),
					  command->obj.u32);
			break;
545

Jon Grimm's avatar
Jon Grimm committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		case SCTP_CMD_REPORT_BAD_TAG:
			SCTP_DEBUG_PRINTK("vtag mismatch!\n");
			break;

		case SCTP_CMD_SET_BIND_ADDR:
		        sctp_cmd_set_bind_addrs(commands, asoc,
						command->obj.bp);
			break;

		case SCTP_CMD_STRIKE:
			/* Mark one strike against a transport.  */
			sctp_do_8_2_transport_strike(asoc,
						     command->obj.transport);
			break;

		case SCTP_CMD_TRANSPORT_RESET:
			t = command->obj.transport;
			sctp_cmd_transport_reset(commands, asoc, t);
564 565
			break;

Jon Grimm's avatar
Jon Grimm committed
566
		case SCTP_CMD_TRANSPORT_ON:
567
			t = command->obj.transport;
Jon Grimm's avatar
Jon Grimm committed
568
			sctp_cmd_transport_on(commands, asoc, t, chunk);
569 570
			break;

Jon Grimm's avatar
Jon Grimm committed
571
		case SCTP_CMD_HB_TIMERS_START:
572
			sctp_cmd_hb_timers_start(commands, asoc);
Jon Grimm's avatar
Jon Grimm committed
573
			break;
574

575 576 577 578
		case SCTP_CMD_HB_TIMERS_UPDATE:
			t = command->obj.transport;
			sctp_cmd_hb_timers_update(commands, asoc, t);
			break;
579

Jon Grimm's avatar
Jon Grimm committed
580 581 582 583 584 585 586 587 588 589
		case SCTP_CMD_REPORT_ERROR:
			error = command->obj.error;
			break;

		case SCTP_CMD_PROCESS_CTSN:
			/* Dummy up a SACK for processing. */
			sackh.cum_tsn_ack = command->obj.u32;
			sackh.a_rwnd = 0;
			sackh.num_gap_ack_blocks = 0;
			sackh.num_dup_tsns = 0;
590 591
			sctp_add_cmd_sf(commands,
					SCTP_CMD_PROCESS_SACK,
Jon Grimm's avatar
Jon Grimm committed
592 593
					SCTP_SACKH(&sackh));
			break;
594

Jon Grimm's avatar
Jon Grimm committed
595 596 597 598
		case SCTP_CMD_DISCARD_PACKET:
			/* We need to discard the whole packet.  */
			chunk->pdiscard = 1;
			break;
599

600 601 602 603 604
		case SCTP_CMD_RTO_PENDING:
			t = command->obj.transport;
			t->rto_pending = 1;	
			break;

Jon Grimm's avatar
Jon Grimm committed
605 606 607 608
		default:
			printk(KERN_WARNING "Impossible command: %u, %p\n",
			       command->verb, command->obj.ptr);
			break;
609
		};
610 611
		if (error)
			return error;
612
	}
Jon Grimm's avatar
Jon Grimm committed
613 614 615 616 617 618

	return error;

nomem:
	error = -ENOMEM;
	return error;
619
}
Jon Grimm's avatar
Jon Grimm committed
620 621

/* A helper function for delayed processing of INET ECN CE bit. */
622
static void sctp_do_ecn_ce_work(sctp_association_t *asoc, __u32 lowest_tsn)
Jon Grimm's avatar
Jon Grimm committed
623
{
624
	/* Save the TSN away for comparison when we receive CWR */
Jon Grimm's avatar
Jon Grimm committed
625 626 627

	asoc->last_ecne_tsn = lowest_tsn;
	asoc->need_ecne = 1;
628
}
Jon Grimm's avatar
Jon Grimm committed
629 630 631 632

/* Helper function for delayed processing of SCTP ECNE chunk.  */
/* RFC 2960 Appendix A
 *
633 634 635 636 637 638 639
 * 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
Jon Grimm's avatar
Jon Grimm committed
640 641
 * that was originally marked with the CE bit.
 */
642 643 644
static sctp_chunk_t *sctp_do_ecn_ecne_work(sctp_association_t *asoc,
					   __u32 lowest_tsn,
					   sctp_chunk_t *chunk)
Jon Grimm's avatar
Jon Grimm committed
645 646
{
	sctp_chunk_t *repl;
Jon Grimm's avatar
Jon Grimm committed
647

Jon Grimm's avatar
Jon Grimm committed
648 649
	/* Our previously transmitted packet ran into some congestion
	 * so we should take action by reducing cwnd and ssthresh
650
	 * and then ACK our peer that we we've done so by
Jon Grimm's avatar
Jon Grimm committed
651 652
	 * sending a CWR.
	 */
653

654 655 656
	/* 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.
Jon Grimm's avatar
Jon Grimm committed
657
	 */
658 659
	if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) {
		sctp_transport_t *transport;
660

661 662 663 664
		/* Find which transport's congestion variables
		 * need to be adjusted.
		 */
		transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn);
Jon Grimm's avatar
Jon Grimm committed
665

666 667 668 669 670 671
		/* Update the congestion variables. */
		if (transport)
			sctp_transport_lower_cwnd(transport,
						  SCTP_LOWER_CWND_ECNE);
		asoc->last_cwr_tsn = lowest_tsn;
	}
672

673
	/* Always try to quiet the other end.  In case of lost CWR,
674
	 * resend last_cwr_tsn.
Jon Grimm's avatar
Jon Grimm committed
675
	 */
676
	repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk);
Jon Grimm's avatar
Jon Grimm committed
677 678 679 680 681

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

684 685 686
/* Helper function to do delayed processing of ECN CWR chunk.  */
static void sctp_do_ecn_cwr_work(sctp_association_t *asoc,
				 __u32 lowest_tsn)
Jon Grimm's avatar
Jon Grimm committed
687 688 689 690 691
{
	/* Turn off ECNE getting auto-prepended to every outgoing
	 * packet
	 */
	asoc->need_ecne = 0;
692
}
Jon Grimm's avatar
Jon Grimm committed
693

694
/* This macro is to compress the text a bit...  */
Jon Grimm's avatar
Jon Grimm committed
695 696 697
#define AP(v) asoc->peer.v

/* Generate SACK if necessary.  We call this at the end of a packet.  */
698
int sctp_gen_sack(sctp_association_t *asoc, int force, sctp_cmd_seq_t *commands)
Jon Grimm's avatar
Jon Grimm committed
699
{
700
	__u32 ctsn, max_tsn_seen;
Jon Grimm's avatar
Jon Grimm committed
701 702 703
	sctp_chunk_t *sack;
	int error = 0;

704 705
	if (force)
		asoc->peer.sack_needed = 1;
Jon Grimm's avatar
Jon Grimm committed
706 707 708 709

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

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	/* 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]
	 */
        if (max_tsn_seen != ctsn)
		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) {
Jon Grimm's avatar
Jon Grimm committed
732
		/* We will need a SACK for the next packet.  */
733
		asoc->peer.sack_needed = 1;
Jon Grimm's avatar
Jon Grimm committed
734
		goto out;
735
	} else {
Jon Grimm's avatar
Jon Grimm committed
736
		sack = sctp_make_sack(asoc);
737 738
		if (!sack)
			goto nomem;
Jon Grimm's avatar
Jon Grimm committed
739

740 741 742
		/* Update the last advertised rwnd value. */
		asoc->a_rwnd = asoc->rwnd;

743
		asoc->peer.sack_needed = 0;
Jon Grimm's avatar
Jon Grimm committed
744 745
		asoc->peer.next_dup_tsn = 0;

746
		error = sctp_push_outqueue(&asoc->outqueue, sack);
Jon Grimm's avatar
Jon Grimm committed
747

748
		/* Stop the SACK timer.  */
Jon Grimm's avatar
Jon Grimm committed
749
		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
750 751
				SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
	}
Jon Grimm's avatar
Jon Grimm committed
752

753
out:
Jon Grimm's avatar
Jon Grimm committed
754
	return error;
755 756

nomem:
Jon Grimm's avatar
Jon Grimm committed
757 758
	error = -ENOMEM;
	return error;
759
}
Jon Grimm's avatar
Jon Grimm committed
760 761

/* Handle a duplicate TSN.  */
762
void sctp_do_TSNdup(sctp_association_t *asoc, sctp_chunk_t *chunk, long gap)
Jon Grimm's avatar
Jon Grimm committed
763 764
{
#if 0
765
	sctp_chunk_t *sack;
Jon Grimm's avatar
Jon Grimm committed
766 767 768 769 770 771 772

	/* Caution:  gap < 2 * SCTP_TSN_MAP_SIZE
	 * 	so gap can be negative.
	 *
	 *		--xguo
	 */

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	/* Count this TSN.  */
	if (gap < SCTP_TSN_MAP_SIZE) {
		asoc->peer.tsn_map[gap]++;
	} else {
		asoc->peer.tsn_map_overflow[gap - SCTP_TSN_MAP_SIZE]++;
	}

	/* From 6.2  Acknowledgement on Reception of DATA Chunks
	 *
	 * When a packet arrives with duplicate DATA chunk(s)
	 * and with no new DATA chunk(s), the endpoint MUST
	 * immediately send a SACK with no delay. If a packet
	 * arrives with duplicate DATA chunk(s) bundled with
	 * new DATA chunks, the endpoint MAY immediately send a
	 * SACK.  Normally receipt of duplicate DATA chunks
	 * will occur when the original SACK chunk was lost and
	 * the peer's RTO has expired. The duplicate TSN
	 * number(s) SHOULD be reported in the SACK as
	 * duplicate.
	 */
	asoc->counters[SctpCounterAckState] = 2;
Jon Grimm's avatar
Jon Grimm committed
794 795 796 797 798 799 800 801
#endif /* 0 */
} /* sctp_do_TSNdup() */

#undef AP

/* When the T3-RTX timer expires, it calls this function to create the
 * relevant state machine event.
 */
802
void sctp_generate_t3_rtx_event(unsigned long peer)
Jon Grimm's avatar
Jon Grimm committed
803
{
804 805
	int error;
	sctp_transport_t *transport = (sctp_transport_t *) peer;
Jon Grimm's avatar
Jon Grimm committed
806 807
	sctp_association_t *asoc = transport->asoc;

808
	/* Check whether a task is in the sock.  */
Jon Grimm's avatar
Jon Grimm committed
809 810

	sctp_bh_lock_sock(asoc->base.sk);
811
	if (sock_owned_by_user(asoc->base.sk)) {
812
		SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __FUNCTION__);
Jon Grimm's avatar
Jon Grimm committed
813

814 815
		/* Try again later.  */
		if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
Jon Grimm's avatar
Jon Grimm committed
816 817
			sctp_transport_hold(transport);
		goto out_unlock;
Jon Grimm's avatar
Jon Grimm committed
818
	}
Jon Grimm's avatar
Jon Grimm committed
819 820 821

	/* Is this transport really dead and just waiting around for
	 * the timer to let go of the reference?
822 823
	 */
	if (transport->dead)
Jon Grimm's avatar
Jon Grimm committed
824 825 826
		goto out_unlock;

	/* Run through the state machine.  */
827
	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
Jon Grimm's avatar
Jon Grimm committed
828
			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
829 830 831
			   asoc->state,
			   asoc->ep, asoc,
			   transport, GFP_ATOMIC);
Jon Grimm's avatar
Jon Grimm committed
832

833 834
	if (error)
		asoc->base.sk->err = -error;
Jon Grimm's avatar
Jon Grimm committed
835 836 837 838

out_unlock:
	sctp_bh_unlock_sock(asoc->base.sk);
	sctp_transport_put(transport);
839
}
Jon Grimm's avatar
Jon Grimm committed
840 841 842 843

/* This is a sa interface for producing timeout events.  It works
 * for timeouts which use the association as their parameter.
 */
844 845
static void sctp_generate_timeout_event(sctp_association_t *asoc,
					sctp_event_timeout_t timeout_type)
Jon Grimm's avatar
Jon Grimm committed
846
{
847
	int error = 0;
Jon Grimm's avatar
Jon Grimm committed
848

849
	sctp_bh_lock_sock(asoc->base.sk);
850
	if (sock_owned_by_user(asoc->base.sk)) {
851 852
		SCTP_DEBUG_PRINTK("%s:Sock is busy: timer %d\n",
				  __FUNCTION__,
Jon Grimm's avatar
Jon Grimm committed
853
				  timeout_type);
854 855 856

		/* Try again later.  */
		if (!mod_timer(&asoc->timers[timeout_type], jiffies + (HZ/20)))
Jon Grimm's avatar
Jon Grimm committed
857 858
			sctp_association_hold(asoc);
		goto out_unlock;
859
	}
Jon Grimm's avatar
Jon Grimm committed
860 861 862 863

	/* Is this association really dead and just waiting around for
	 * the timer to let go of the reference?
	 */
864
	if (asoc->base.dead)
Jon Grimm's avatar
Jon Grimm committed
865 866
		goto out_unlock;

867
	/* Run through the state machine.  */
Jon Grimm's avatar
Jon Grimm committed
868 869
	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
			   SCTP_ST_TIMEOUT(timeout_type),
870
			   asoc->state, asoc->ep, asoc,
Jon Grimm's avatar
Jon Grimm committed
871 872 873
			   (void *)timeout_type,
			   GFP_ATOMIC);

874 875 876
	if (error)
		asoc->base.sk->err = -error;

Jon Grimm's avatar
Jon Grimm committed
877 878 879
out_unlock:
	sctp_bh_unlock_sock(asoc->base.sk);
	sctp_association_put(asoc);
880
}
Jon Grimm's avatar
Jon Grimm committed
881

882
void sctp_generate_t1_cookie_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
883
{
884
	sctp_association_t *asoc = (sctp_association_t *) data;
Jon Grimm's avatar
Jon Grimm committed
885
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE);
886
}
Jon Grimm's avatar
Jon Grimm committed
887

888
void sctp_generate_t1_init_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
889
{
890 891 892
	sctp_association_t *asoc = (sctp_association_t *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT);
}
Jon Grimm's avatar
Jon Grimm committed
893

894
void sctp_generate_t2_shutdown_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
895
{
896 897 898
	sctp_association_t *asoc = (sctp_association_t *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN);
}
Jon Grimm's avatar
Jon Grimm committed
899

Jon Grimm's avatar
Jon Grimm committed
900 901 902 903 904 905 906 907
void sctp_generate_t5_shutdown_guard_event(unsigned long data)
{
        sctp_association_t *asoc = (sctp_association_t *)data;
        sctp_generate_timeout_event(asoc,
				    SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD);

} /* sctp_generate_t5_shutdown_guard_event() */

908
void sctp_generate_autoclose_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
909
{
910 911 912
	sctp_association_t *asoc = (sctp_association_t *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE);
}
Jon Grimm's avatar
Jon Grimm committed
913

914 915
/* Generate a heart beat event.  If the sock is busy, reschedule.   Make
 * sure that the transport is still valid.
Jon Grimm's avatar
Jon Grimm committed
916
 */
917
void sctp_generate_heartbeat_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
918
{
919 920
	int error = 0;
	sctp_transport_t *transport = (sctp_transport_t *) data;
Jon Grimm's avatar
Jon Grimm committed
921 922 923
	sctp_association_t *asoc = transport->asoc;

	sctp_bh_lock_sock(asoc->base.sk);
924
	if (sock_owned_by_user(asoc->base.sk)) {
925
		SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __FUNCTION__);
Jon Grimm's avatar
Jon Grimm committed
926

927 928
		/* Try again later.  */
		if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
Jon Grimm's avatar
Jon Grimm committed
929 930
			sctp_transport_hold(transport);
		goto out_unlock;
Jon Grimm's avatar
Jon Grimm committed
931
	}
Jon Grimm's avatar
Jon Grimm committed
932

933
	/* Is this structure just waiting around for us to actually
Jon Grimm's avatar
Jon Grimm committed
934 935
	 * get destroyed?
	 */
936
	if (transport->dead)
Jon Grimm's avatar
Jon Grimm committed
937 938
		goto out_unlock;

939 940 941 942 943
	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
			   asoc->state,
			   asoc->ep, asoc,
			   transport, GFP_ATOMIC);
Jon Grimm's avatar
Jon Grimm committed
944

945 946
         if (error)
		 asoc->base.sk->err = -error;
Jon Grimm's avatar
Jon Grimm committed
947 948 949 950

out_unlock:
	sctp_bh_unlock_sock(asoc->base.sk);
	sctp_transport_put(transport);
951
}
Jon Grimm's avatar
Jon Grimm committed
952

953 954
/* Inject a SACK Timeout event into the state machine.  */
void sctp_generate_sack_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
955
{
956 957 958
	sctp_association_t *asoc = (sctp_association_t *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK);
}
Jon Grimm's avatar
Jon Grimm committed
959

960
void sctp_generate_pmtu_raise_event(unsigned long data)
Jon Grimm's avatar
Jon Grimm committed
961
{
962 963 964
	sctp_association_t *asoc = (sctp_association_t *) data;
	sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_PMTU_RAISE);
}
Jon Grimm's avatar
Jon Grimm committed
965 966 967 968 969 970 971 972

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,
	NULL,
Jon Grimm's avatar
Jon Grimm committed
973
	sctp_generate_t5_shutdown_guard_event,
Jon Grimm's avatar
Jon Grimm committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
	sctp_generate_heartbeat_event,
	sctp_generate_sack_event,
	sctp_generate_autoclose_event,
	sctp_generate_pmtu_raise_event,
};

/********************************************************************
 * 3rd Level Abstractions
 ********************************************************************/

/* 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.
989
 *
Jon Grimm's avatar
Jon Grimm committed
990 991 992 993 994 995 996
 * 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.
997
 *
Jon Grimm's avatar
Jon Grimm committed
998
 */
999 1000
static void sctp_do_8_2_transport_strike(sctp_association_t *asoc,
					 sctp_transport_t *transport)
Jon Grimm's avatar
Jon Grimm committed
1001 1002 1003 1004 1005 1006
{
	/* The check for association's overall error counter exceeding the
	 * threshold is done in the state function.
	 */
	asoc->overall_error_count++;

1007
	if (transport->active &&
1008
	    (transport->error_count++ >= transport->error_threshold)) {
Jon Grimm's avatar
Jon Grimm committed
1009 1010 1011
		SCTP_DEBUG_PRINTK("transport_strike: transport "
				  "IP:%d.%d.%d.%d failed.\n",
				  NIPQUAD(transport->ipaddr.v4.sin_addr));
1012 1013
		sctp_assoc_control_transport(asoc, transport,
					     SCTP_TRANSPORT_DOWN,
Jon Grimm's avatar
Jon Grimm committed
1014 1015 1016
					     SCTP_FAILED_THRESHOLD);
	}

1017 1018 1019 1020 1021
	/* 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.
	 */
Jon Grimm's avatar
Jon Grimm committed
1022
	transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
1023
}
Jon Grimm's avatar
Jon Grimm committed
1024

1025 1026 1027
/* Worker routine to handle INIT command failure.  */
static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
				 sctp_association_t *asoc)
Jon Grimm's avatar
Jon Grimm committed
1028 1029 1030 1031 1032 1033 1034 1035
{
	sctp_ulpevent_t *event;

	event = sctp_ulpevent_make_assoc_change(asoc,
						0,
						SCTP_CANT_STR_ASSOC,
						0, 0, 0,
						GFP_ATOMIC);
1036 1037

	if (event)
Jon Grimm's avatar
Jon Grimm committed
1038 1039 1040 1041 1042 1043 1044 1045 1046
		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
				SCTP_ULPEVENT(event));

	/* FIXME:  We need to handle data possibly either
	 * sent via COOKIE-ECHO bundling or just waiting in
	 * the transmit queue, if the user has enabled
	 * SEND_FAILED notifications.
	 */
	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1047
}
Jon Grimm's avatar
Jon Grimm committed
1048

1049 1050
/* Worker routine to handle SCTP_CMD_ASSOC_FAILED.  */
static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
1051 1052
				  sctp_association_t *asoc,
				  sctp_event_t event_type,
1053
				  sctp_subtype_t subtype,
1054
				  sctp_chunk_t *chunk)
Jon Grimm's avatar
Jon Grimm committed
1055 1056
{
	sctp_ulpevent_t *event;
1057 1058
	__u16 error = 0;

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	switch(event_type) {
	case SCTP_EVENT_T_PRIMITIVE:
		if (SCTP_PRIMITIVE_ABORT == subtype.primitive)
			error = SCTP_ERROR_USER_ABORT;
		break;
	case SCTP_EVENT_T_CHUNK:
		if (chunk && (SCTP_CID_ABORT == chunk->chunk_hdr->type) &&
	    	    (ntohs(chunk->chunk_hdr->length) >=
			(sizeof(struct sctp_chunkhdr) +
				sizeof(struct sctp_errhdr)))) {
			error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
		}
		break;
	default:
		break;
1074
	}
Jon Grimm's avatar
Jon Grimm committed
1075 1076 1077 1078

	event = sctp_ulpevent_make_assoc_change(asoc,
						0,
						SCTP_COMM_LOST,
1079
						error, 0, 0,
Jon Grimm's avatar
Jon Grimm committed
1080
						GFP_ATOMIC);
1081 1082

	if (event)
Jon Grimm's avatar
Jon Grimm committed
1083 1084 1085
		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
				SCTP_ULPEVENT(event));

Jon Grimm's avatar
Jon Grimm committed
1086 1087 1088
	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
			SCTP_STATE(SCTP_STATE_CLOSED));

Jon Grimm's avatar
Jon Grimm committed
1089 1090 1091 1092
	/* FIXME:  We need to handle data that could not be sent or was not
	 * acked, if the user has enabled SEND_FAILED notifications.
	 */
	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1093
}
Jon Grimm's avatar
Jon Grimm committed
1094 1095

/* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
1096 1097 1098
 * 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. 
Jon Grimm's avatar
Jon Grimm committed
1099
 */
1100 1101 1102 1103 1104
static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
				 sctp_association_t *asoc,
				 sctp_chunk_t *chunk,
				 sctp_init_chunk_t *peer_init,
				 int priority)
Jon Grimm's avatar
Jon Grimm committed
1105
{
1106 1107 1108 1109 1110 1111
	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.
Jon Grimm's avatar
Jon Grimm committed
1112
	 */
1113 1114 1115 1116 1117 1118 1119 1120 1121

	if (!sctp_process_init(asoc, chunk->chunk_hdr->type,
			       sctp_source(chunk), peer_init,
			       priority))
		error = -ENOMEM;
	else
		error = 0;

	return error;
1122
}
Jon Grimm's avatar
Jon Grimm committed
1123 1124

/* Helper function to break out starting up of heartbeat timers.  */
1125 1126
static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
				     sctp_association_t *asoc)
Jon Grimm's avatar
Jon Grimm committed
1127 1128
{
	sctp_transport_t *t;
Rusty Russell's avatar
Rusty Russell committed
1129
	struct list_head *pos;
Jon Grimm's avatar
Jon Grimm committed
1130 1131 1132 1133 1134 1135 1136

	/* 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.
	 */
	list_for_each(pos, &asoc->peer.transport_addr_list) {
		t = list_entry(pos, sctp_transport_t, transports);
1137
		if (!mod_timer(&t->hb_timer,
Jon Grimm's avatar
Jon Grimm committed
1138 1139 1140
			       t->hb_interval + t->rto + jiffies)) {
			sctp_transport_hold(t);
		}
1141 1142 1143
	}
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
/* Helper function to update the heartbeat timer. */
static void sctp_cmd_hb_timers_update(sctp_cmd_seq_t *cmds,
				   sctp_association_t *asoc,
				   sctp_transport_t *t)
{
	/* Update the heartbeat timer.  */
	if (!mod_timer(&t->hb_timer, t->hb_interval + t->rto + jiffies))
		sctp_transport_hold(t);
}

1154 1155 1156
/* Helper function to break out SCTP_CMD_SET_BIND_ADDR handling.  */
void sctp_cmd_set_bind_addrs(sctp_cmd_seq_t *cmds, sctp_association_t *asoc,
			     sctp_bind_addr_t *bp)
Jon Grimm's avatar
Jon Grimm committed
1157
{
Rusty Russell's avatar
Rusty Russell committed
1158
	struct list_head *pos, *temp;
Jon Grimm's avatar
Jon Grimm committed
1159 1160 1161 1162 1163 1164 1165

	list_for_each_safe(pos, temp, &bp->address_list) {
		list_del_init(pos);
		list_add_tail(pos, &asoc->base.bind_addr.address_list);
	}

	/* Free the temporary bind addr header, otherwise
1166
	 * there will a memory leak.
Jon Grimm's avatar
Jon Grimm committed
1167 1168
	 */
	sctp_bind_addr_free(bp);
1169
}
Jon Grimm's avatar
Jon Grimm committed
1170

1171
/* Helper function to handle the reception of an HEARTBEAT ACK.  */
1172 1173
static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
				  sctp_association_t *asoc,
1174
				  sctp_transport_t *t, sctp_chunk_t *chunk)
Jon Grimm's avatar
Jon Grimm committed
1175
{
1176
	sctp_sender_hb_info_t *hbinfo;
Jon Grimm's avatar
Jon Grimm committed
1177

1178 1179 1180
	/* 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.
Jon Grimm's avatar
Jon Grimm committed
1181 1182 1183
	 * The association's overall error count is also cleared.
	 */
	t->error_count = 0;
1184
	t->asoc->overall_error_count = 0;
Jon Grimm's avatar
Jon Grimm committed
1185

1186 1187
	/* Mark the destination transport address as active if it is not so
	 * marked.
Jon Grimm's avatar
Jon Grimm committed
1188
	 */
1189
	if (!t->active)
Jon Grimm's avatar
Jon Grimm committed
1190 1191
		sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
					     SCTP_HEARTBEAT_SUCCESS);
1192

Jon Grimm's avatar
Jon Grimm committed
1193 1194 1195 1196
	/* 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.
	 */
1197
	hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Jon Grimm's avatar
Jon Grimm committed
1198
	sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
1199
}
Jon Grimm's avatar
Jon Grimm committed
1200

1201 1202 1203 1204 1205 1206
/* Helper function to do a transport reset at the expiry of the hearbeat
 * timer.
 */
static void sctp_cmd_transport_reset(sctp_cmd_seq_t *cmds,
				     sctp_association_t *asoc,
				     sctp_transport_t *t)
Jon Grimm's avatar
Jon Grimm committed
1207 1208 1209 1210 1211
{
	sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);

	/* Mark one strike against a transport.  */
	sctp_do_8_2_transport_strike(asoc, t);
1212
}
Jon Grimm's avatar
Jon Grimm committed
1213

1214
/* Helper function to process the process SACK command.  */
1215 1216
static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
				 sctp_association_t *asoc,
1217
				 sctp_sackhdr_t *sackh)
Jon Grimm's avatar
Jon Grimm committed
1218 1219 1220 1221
{
	int err;

	if (sctp_sack_outqueue(&asoc->outqueue, sackh)) {
1222
		/* There are no more TSNs awaiting SACK.  */
Jon Grimm's avatar
Jon Grimm committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
		err = sctp_do_sm(SCTP_EVENT_T_OTHER,
				 SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
				 asoc->state, asoc->ep, asoc, NULL,
				 GFP_ATOMIC);
	} else {
		/* Windows may have opened, so we need
		 * to check if we have DATA to transmit
		 */
		err = sctp_flush_outqueue(&asoc->outqueue, 0);
	}

	return err;
1235
}
Jon Grimm's avatar
Jon Grimm committed
1236 1237

/* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
1238
 * the transport for a shutdown chunk.
Jon Grimm's avatar
Jon Grimm committed
1239
 */
1240 1241
static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds, sctp_association_t *asoc,
			      sctp_chunk_t *chunk)
Jon Grimm's avatar
Jon Grimm committed
1242 1243 1244 1245 1246 1247 1248
{
	sctp_transport_t *t;

	t = sctp_assoc_choose_shutdown_transport(asoc);
	asoc->shutdown_last_sent_to = t;
	asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
	chunk->transport = t;
1249
}
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

/* Helper function to change the state of an association. */
static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds, sctp_association_t *asoc,
			       sctp_state_t state)
{
	asoc->state = state;
	asoc->state_timestamp = jiffies;

	/* Wake up any process waiting for the association to
	 * get established.
	 */
	if ((SCTP_STATE_ESTABLISHED == asoc->state) &&
	    (waitqueue_active(&asoc->wait)))
		wake_up_interruptible(&asoc->wait);
}