Blame view

net/sctp/sm_sideeffect.c 47.9 KB
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
1
  /* SCTP kernel implementation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
   * (C) Copyright IBM Corp. 2001, 2004
   * Copyright (c) 1999 Cisco, Inc.
   * Copyright (c) 1999-2001 Motorola, Inc.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
6
   * This file is part of the SCTP kernel implementation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
12
   * This SCTP implementation is free software;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
18
   * This SCTP implementation is distributed in the hope that it
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
   * 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
   * along with GNU CC; see the file COPYING.  If not, write to
   * the Free Software Foundation, 59 Temple Place - Suite 330,
   * Boston, MA 02111-1307, USA.
   *
   * Please send any bug reports or fixes you make to the
   * email address(es):
   *    lksctp developers <lksctp-developers@lists.sourceforge.net>
   *
   * Or submit a bug report through the following website:
   *    http://www.sf.net/projects/lksctp
   *
   * 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>
   *
   * Any bugs reported given to us we will try to fix... any fixes shared will
   * be incorporated into the next SCTP release.
   */
145ce502e   Joe Perches   net/sctp: Use pr_...
49
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
  #include <linux/skbuff.h>
  #include <linux/types.h>
  #include <linux/socket.h>
  #include <linux/ip.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
54
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
59
60
61
62
63
64
  #include <net/sock.h>
  #include <net/sctp/sctp.h>
  #include <net/sctp/sm.h>
  
  static int sctp_cmd_interpreter(sctp_event_t event_type,
  				sctp_subtype_t subtype,
  				sctp_state_t state,
  				struct sctp_endpoint *ep,
  				struct sctp_association *asoc,
  				void *event_arg,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
65
  				sctp_disposition_t status,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  				sctp_cmd_seq_t *commands,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
67
  				gfp_t gfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
74
  static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
  			     sctp_state_t state,
  			     struct sctp_endpoint *ep,
  			     struct sctp_association *asoc,
  			     void *event_arg,
  			     sctp_disposition_t status,
  			     sctp_cmd_seq_t *commands,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
75
  			     gfp_t gfp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
80
81
  
  /********************************************************************
   * Helper functions
   ********************************************************************/
  
  /* A helper function for delayed processing of INET ECN CE bit. */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
82
  static void sctp_do_ecn_ce_work(struct sctp_association *asoc,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
153
154
155
156
157
158
159
160
  				__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,
  					   __u32 lowest_tsn,
  					   struct sctp_chunk *chunk)
  {
  	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,
  			 sctp_cmd_seq_t *commands)
  {
  	__u32 ctsn, max_tsn_seen;
  	struct sctp_chunk *sack;
52ccb8e90   Frank Filz   [SCTP]: Update SC...
161
  	struct sctp_transport *trans = asoc->peer.last_data_from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  	int error = 0;
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
163
  	if (force ||
52ccb8e90   Frank Filz   [SCTP]: Update SC...
164
165
  	    (!trans && (asoc->param_flags & SPP_SACKDELAY_DISABLE)) ||
  	    (trans && (trans->param_flags & SPP_SACKDELAY_DISABLE)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  		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]
  	 */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
181
  	if (max_tsn_seen != ctsn)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
191
192
  		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) {
d364d9276   Wei Yongjun   sctp: Bring SCTP_...
193
  		asoc->peer.sack_cnt++;
52ccb8e90   Frank Filz   [SCTP]: Update SC...
194
195
196
197
198
199
  
  		/* Set the SACK delay timeout based on the
  		 * SACK delay for the last transport
  		 * data was received from, or the default
  		 * for the association.
  		 */
d364d9276   Wei Yongjun   sctp: Bring SCTP_...
200
201
202
203
  		if (trans) {
  			/* We will need a SACK for the next packet.  */
  			if (asoc->peer.sack_cnt >= trans->sackfreq - 1)
  				asoc->peer.sack_needed = 1;
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
204
  			asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
52ccb8e90   Frank Filz   [SCTP]: Update SC...
205
  				trans->sackdelay;
d364d9276   Wei Yongjun   sctp: Bring SCTP_...
206
207
208
209
  		} else {
  			/* We will need a SACK for the next packet.  */
  			if (asoc->peer.sack_cnt >= asoc->sackfreq - 1)
  				asoc->peer.sack_needed = 1;
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
210
  			asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
52ccb8e90   Frank Filz   [SCTP]: Update SC...
211
  				asoc->sackdelay;
d364d9276   Wei Yongjun   sctp: Bring SCTP_...
212
  		}
52ccb8e90   Frank Filz   [SCTP]: Update SC...
213
214
215
216
  
  		/* Restart the SACK timer. */
  		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  				SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
  	} else {
90f2f5318   Vlad Yasevich   sctp: Update SWS ...
218
  		asoc->a_rwnd = asoc->rwnd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
219
220
221
222
223
  		sack = sctp_make_sack(asoc);
  		if (!sack)
  			goto nomem;
  
  		asoc->peer.sack_needed = 0;
d364d9276   Wei Yongjun   sctp: Bring SCTP_...
224
  		asoc->peer.sack_cnt = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

732ba35e7   Vlad Yasevich   [SCTP]: Fix SACK ...
226
  		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(sack));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
  
  		/* Stop the SACK timer.  */
  		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  				SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
  	}
52ccb8e90   Frank Filz   [SCTP]: Update SC...
232

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	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)
  {
  	int error;
  	struct sctp_transport *transport = (struct sctp_transport *) peer;
  	struct sctp_association *asoc = transport->asoc;
  
  	/* Check whether a task is in the sock.  */
  
  	sctp_bh_lock_sock(asoc->base.sk);
  	if (sock_owned_by_user(asoc->base.sk)) {
0dc47877a   Harvey Harrison   net: replace rema...
252
253
  		SCTP_DEBUG_PRINTK("%s:Sock is busy.
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  
  		/* Try again later.  */
  		if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
  			sctp_transport_hold(transport);
  		goto out_unlock;
  	}
  
  	/* Is this transport really dead and just waiting around for
  	 * the timer to let go of the reference?
  	 */
  	if (transport->dead)
  		goto out_unlock;
  
  	/* Run through the state machine.  */
  	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
  			   asoc->state,
  			   asoc->ep, asoc,
  			   transport, GFP_ATOMIC);
  
  	if (error)
  		asoc->base.sk->sk_err = -error;
  
  out_unlock:
  	sctp_bh_unlock_sock(asoc->base.sk);
  	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,
  					sctp_event_timeout_t timeout_type)
  {
  	int error = 0;
  
  	sctp_bh_lock_sock(asoc->base.sk);
  	if (sock_owned_by_user(asoc->base.sk)) {
  		SCTP_DEBUG_PRINTK("%s:Sock is busy: timer %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
294
  				  __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
347
348
  				  timeout_type);
  
  		/* 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.  */
  	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  			   SCTP_ST_TIMEOUT(timeout_type),
  			   asoc->state, asoc->ep, asoc,
  			   (void *)timeout_type, GFP_ATOMIC);
  
  	if (error)
  		asoc->base.sk->sk_err = -error;
  
  out_unlock:
  	sctp_bh_unlock_sock(asoc->base.sk);
  	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)
  {
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
349
350
  	struct sctp_association *asoc = (struct sctp_association *)data;
  	sctp_generate_timeout_event(asoc,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  				    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)
  {
  	int error = 0;
  	struct sctp_transport *transport = (struct sctp_transport *) data;
  	struct sctp_association *asoc = transport->asoc;
  
  	sctp_bh_lock_sock(asoc->base.sk);
  	if (sock_owned_by_user(asoc->base.sk)) {
0dc47877a   Harvey Harrison   net: replace rema...
372
373
  		SCTP_DEBUG_PRINTK("%s:Sock is busy.
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  
  		/* Try again later.  */
  		if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
  			sctp_transport_hold(transport);
  		goto out_unlock;
  	}
  
  	/* Is this structure just waiting around for us to actually
  	 * get destroyed?
  	 */
  	if (transport->dead)
  		goto out_unlock;
  
  	error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  			   SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
  			   asoc->state, asoc->ep, asoc,
  			   transport, GFP_ATOMIC);
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
391
  	 if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
392
393
394
395
396
397
  		 asoc->base.sk->sk_err = -error;
  
  out_unlock:
  	sctp_bh_unlock_sock(asoc->base.sk);
  	sctp_transport_put(transport);
  }
50b5d6ad6   Vlad Yasevich   sctp: Fix a race ...
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  /* 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)
  {
  	struct sctp_transport *transport = (struct sctp_transport *) data;
  	struct sctp_association *asoc = transport->asoc;
  	
  	sctp_bh_lock_sock(asoc->base.sk);
  	if (sock_owned_by_user(asoc->base.sk)) {
  		SCTP_DEBUG_PRINTK("%s:Sock is busy.
  ", __func__);
  
  		/* 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;
  
  	sctp_do_sm(SCTP_EVENT_T_OTHER,
  		   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  		   asoc->state, asoc->ep, asoc, transport, GFP_ATOMIC);
  
  out_unlock:
  	sctp_bh_unlock_sock(asoc->base.sk);
  	sctp_association_put(asoc);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  /* Inject a SACK Timeout event into the state machine.  */
  static void sctp_generate_sack_event(unsigned long data)
  {
  	struct sctp_association *asoc = (struct sctp_association *) data;
  	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,
1e7d3d90c   Vladislav Yasevich   [SCTP]: Remove ti...
447
  	NULL,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  	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.
   *
   */
  static void sctp_do_8_2_transport_strike(struct sctp_association *asoc,
7e99013a5   Vlad Yasevich   sctp: Fix broken ...
469
470
  					 struct sctp_transport *transport,
  					 int is_hb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
474
  {
  	/* The check for association's overall error counter exceeding the
  	 * threshold is done in the state function.
  	 */
b9f847868   Vlad Yasevich   sctp: Fix error c...
475
476
477
478
479
  	/* 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.
25985edce   Lucas De Marchi   Fix common misspe...
480
  	 * Additionally, some tranport states inhibit error increments.
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
481
  	 */
b9f847868   Vlad Yasevich   sctp: Fix error c...
482
  	if (!is_hb) {
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
483
  		asoc->overall_error_count++;
b9f847868   Vlad Yasevich   sctp: Fix error c...
484
485
486
487
488
489
490
491
  		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++;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492

3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
493
  	if (transport->state != SCTP_INACTIVE &&
b9f847868   Vlad Yasevich   sctp: Fix error c...
494
  	    (transport->error_count > transport->pathmaxrxt)) {
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
495
496
497
498
  		SCTP_DEBUG_PRINTK_IPADDR("transport_strike:association %p",
  					 " transport IP: port:%d failed.
  ",
  					 asoc,
b3f5b3b66   Al Viro   [SCTP]: Trivial -...
499
500
  					 (&transport->ipaddr),
  					 ntohs(transport->ipaddr.v4.sin_port));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
503
504
505
506
507
508
509
  		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.
faee47cdb   Vlad Yasevich   sctp: Fix the RTO...
510
511
  	 *
  	 * Special Case:  the first HB doesn't trigger exponential backoff.
3ad2f3fbb   Daniel Mack   tree-wide: Assort...
512
  	 * The first unacknowledged HB triggers it.  We do this with a flag
faee47cdb   Vlad Yasevich   sctp: Fix the RTO...
513
  	 * that indicates that we have an outstanding HB.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
  	 */
7e99013a5   Vlad Yasevich   sctp: Fix broken ...
515
  	if (!is_hb || transport->hb_sent) {
faee47cdb   Vlad Yasevich   sctp: Fix the RTO...
516
517
  		transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
520
521
522
523
524
525
526
527
  }
  
  /* Worker routine to handle INIT command failure.  */
  static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
  				 struct sctp_association *asoc,
  				 unsigned error)
  {
  	struct sctp_ulpevent *event;
  
  	event = sctp_ulpevent_make_assoc_change(asoc,0, SCTP_CANT_STR_ASSOC,
a5a35e767   Vlad Yasevich   [SCTP]: Implement...
528
  						(__u16)error, 0, 0, NULL,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  						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.  */
  static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
  				  struct sctp_association *asoc,
  				  sctp_event_t event_type,
  				  sctp_subtype_t subtype,
  				  struct sctp_chunk *chunk,
  				  unsigned error)
  {
  	struct sctp_ulpevent *event;
  
  	/* Cancel any partial delivery in progress. */
  	sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
a5a35e767   Vlad Yasevich   [SCTP]: Implement...
555
556
557
558
559
560
561
  	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,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
564
565
566
567
568
  						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));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
574
575
576
577
578
579
580
581
  	/* 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.
   */
  static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
  				 struct sctp_association *asoc,
  				 struct sctp_chunk *chunk,
3182cd84f   Alexey Dobriyan   [SCTP]: __nocast ...
582
  				 sctp_init_chunk_t *peer_init,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
583
  				 gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
585
586
587
588
589
590
591
  {
  	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.
  	 */
de6becdc0   Wei Yongjun   sctp: fix to chec...
592
  	if (!sctp_process_init(asoc, chunk, sctp_source(chunk), peer_init, gfp))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
595
596
597
598
599
600
601
602
603
604
  		error = -ENOMEM;
  	else
  		error = 0;
  
  	return error;
  }
  
  /* Helper function to break out starting up of heartbeat timers.  */
  static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
  				     struct sctp_association *asoc)
  {
  	struct sctp_transport *t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
608
609
  
  	/* 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.
  	 */
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
610
  	list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
612
613
614
615
616
617
618
619
620
  
  		if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  			sctp_transport_hold(t);
  	}
  }
  
  static void sctp_cmd_hb_timers_stop(sctp_cmd_seq_t *cmds,
  				    struct sctp_association *asoc)
  {
  	struct sctp_transport *t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
622
  
  	/* Stop all heartbeat timers. */
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
623
624
  	list_for_each_entry(t, &asoc->peer.transport_addr_list,
  			transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
626
627
628
629
630
631
  		if (del_timer(&t->hb_timer))
  			sctp_transport_put(t);
  	}
  }
  
  /* Helper function to stop any pending T3-RTX timers */
  static void sctp_cmd_t3_rtx_timers_stop(sctp_cmd_seq_t *cmds,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
632
  					struct sctp_association *asoc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
  {
  	struct sctp_transport *t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635

9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
636
637
  	list_for_each_entry(t, &asoc->peer.transport_addr_list,
  			transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
639
640
641
642
643
644
645
646
647
  		if (timer_pending(&t->T3_rtx_timer) &&
  		    del_timer(&t->T3_rtx_timer)) {
  			sctp_transport_put(t);
  		}
  	}
  }
  
  
  /* Helper function to update the heartbeat timer. */
  static void sctp_cmd_hb_timer_update(sctp_cmd_seq_t *cmds,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  				     struct sctp_transport *t)
  {
  	/* Update the heartbeat timer.  */
  	if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  		sctp_transport_hold(t);
  }
  
  /* Helper function to handle the reception of an HEARTBEAT ACK.  */
  static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
  				  struct sctp_association *asoc,
  				  struct sctp_transport *t,
  				  struct sctp_chunk *chunk)
  {
  	sctp_sender_hb_info_t *hbinfo;
34d2d89f2   Michio Honda   sctp: fasthandoff...
662
  	int was_unconfirmed = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
665
666
  
  	/* 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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667
668
  	 */
  	t->error_count = 0;
f8d960524   Thomas Graf   sctp: Enforce ret...
669
670
671
672
673
674
675
676
677
678
679
  
  	/*
  	 * 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.
  	 */
  	if (t->asoc->state != SCTP_STATE_SHUTDOWN_PENDING)
  		t->asoc->overall_error_count = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680

faee47cdb   Vlad Yasevich   sctp: Fix the RTO...
681
682
683
684
  	/* Clear the hb_sent flag to signal that we had a good
  	 * acknowledgement.
  	 */
  	t->hb_sent = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
686
687
  	/* Mark the destination transport address as active if it is not so
  	 * marked.
  	 */
34d2d89f2   Michio Honda   sctp: fasthandoff...
688
689
  	if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED)) {
  		was_unconfirmed = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
  		sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
  					     SCTP_HEARTBEAT_SUCCESS);
34d2d89f2   Michio Honda   sctp: fasthandoff...
692
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
694
695
696
  
  	/* 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.
e533ca16f   Vlad Yasevich   [SCTP]: Force upd...
697
698
699
  	 * 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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
700
  	 */
e533ca16f   Vlad Yasevich   [SCTP]: Force upd...
701
702
  	if (t->rto_pending == 0)
  		t->rto_pending = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
  	hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
  	sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
705
706
707
708
  
  	/* Update the heartbeat timer.  */
  	if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  		sctp_transport_hold(t);
34d2d89f2   Michio Honda   sctp: fasthandoff...
709
710
711
  
  	if (was_unconfirmed && asoc->peer.transport_count == 1)
  		sctp_transport_immediate_rtx(t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
713
714
715
716
717
718
  
  /* Helper function to process the process SACK command.  */
  static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
  				 struct sctp_association *asoc,
  				 struct sctp_sackhdr *sackh)
  {
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
719
  	int err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
723
724
725
726
  
  	if (sctp_outq_sack(&asoc->outqueue, sackh)) {
  		/* There are no more TSNs awaiting SACK.  */
  		err = sctp_do_sm(SCTP_EVENT_T_OTHER,
  				 SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
  				 asoc->state, asoc->ep, asoc, NULL,
  				 GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
728
729
730
731
732
733
734
  	}
  
  	return err;
  }
  
  /* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
   * the transport for a shutdown chunk.
   */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
735
  static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
739
  			      struct sctp_association *asoc,
  			      struct sctp_chunk *chunk)
  {
  	struct sctp_transport *t;
c17b02b38   Vlad Yasevich   sctp: send SHUTDO...
740
741
742
743
  	if (chunk->transport)
  		t = chunk->transport;
  	else {
  		t = sctp_assoc_choose_alter_transport(asoc,
9919b455f   Wei Yongjun   sctp: fix to choo...
744
  					      asoc->shutdown_last_sent_to);
c17b02b38   Vlad Yasevich   sctp: send SHUTDO...
745
746
  		chunk->transport = t;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
748
  	asoc->shutdown_last_sent_to = t;
  	asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
750
751
  }
  
  /* Helper function to change the state of an association. */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
752
  static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
758
  			       struct sctp_association *asoc,
  			       sctp_state_t state)
  {
  	struct sock *sk = asoc->base.sk;
  
  	asoc->state = state;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
759
760
761
  	SCTP_DEBUG_PRINTK("sctp_cmd_new_state: asoc %p[%s]
  ",
  			  asoc, sctp_state_tbl[state]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
  	if (sctp_style(sk, TCP)) {
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
763
  		/* Change the sk->sk_state of a TCP-style socket that has
af901ca18   André Goddard Rosa   tree-wide: fix as...
764
  		 * successfully completed a connect() call.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
766
767
768
769
770
771
772
773
  		 */
  		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) &&
  		    sctp_sstate(sk, ESTABLISHED))
  			sk->sk_shutdown |= RCV_SHUTDOWN;
  	}
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
774
775
776
777
778
  	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] =
1e7d3d90c   Vladislav Yasevich   [SCTP]: Remove ti...
779
  						asoc->rto_initial;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
780
  		asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
1e7d3d90c   Vladislav Yasevich   [SCTP]: Remove ti...
781
  						asoc->rto_initial;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
782
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
784
785
786
787
  	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().
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
788
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
  		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);
  	}
  }
  
  /* Helper function to delete an association. */
  static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
  				struct sctp_association *asoc)
  {
  	struct sock *sk = asoc->base.sk;
  
  	/* If it is a non-temporary association belonging to a TCP-style
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
810
  	 * listening socket that is not closed, do not free it so that accept()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
  	 * can pick it up later.
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
812
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
814
815
816
817
818
819
820
821
822
823
824
  	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
  	    (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
  		return;
  
  	sctp_unhash_established(asoc);
  	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
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
825
   * because primary path may be inactive.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
828
829
830
831
   */
  static void sctp_cmd_setup_t4(sctp_cmd_seq_t *cmds,
  				struct sctp_association *asoc,
  				struct sctp_chunk *chunk)
  {
  	struct sctp_transport *t;
9919b455f   Wei Yongjun   sctp: fix to choo...
832
  	t = sctp_assoc_choose_alter_transport(asoc, chunk->transport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
834
835
  	asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
  	chunk->transport = t;
  }
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
836
  /* Process an incoming Operation Error Chunk. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
838
839
840
  static void sctp_cmd_process_operr(sctp_cmd_seq_t *cmds,
  				   struct sctp_association *asoc,
  				   struct sctp_chunk *chunk)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
  	struct sctp_errhdr *err_hdr;
3df267873   Wei Yongjun   sctp: fix kernel ...
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
  	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:
  		{
  			sctp_chunkhdr_t *unk_chunk_hdr;
  
  			unk_chunk_hdr = (sctp_chunkhdr_t *)err_hdr->variable;
  			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,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
  					SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3df267873   Wei Yongjun   sctp: fix kernel ...
874
875
876
877
  				break;
  			default:
  				break;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
  			break;
3df267873   Wei Yongjun   sctp: fix kernel ...
879
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
880
881
882
  		default:
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
885
886
  	}
  }
  
  /* Process variable FWDTSN chunk information. */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
887
  static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
892
893
894
  				    struct sctp_chunk *chunk)
  {
  	struct sctp_fwdtsn_skip *skip;
  	/* Walk through all the skipped SSNs */
  	sctp_walk_fwdtsn(skip, chunk) {
  		sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895
  }
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
896
  /* Helper function to remove the association non-primary peer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
   * transports.
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
898
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
900
901
902
903
904
905
906
  static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
  {
  	struct sctp_transport *t;
  	struct list_head *pos;
  	struct list_head *temp;
  
  	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  		t = list_entry(pos, struct sctp_transport, transports);
5f242a13e   Al Viro   [SCTP]: Switch ->...
907
  		if (!sctp_cmp_addr_exact(&t->ipaddr,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
908
  					 &asoc->peer.primary_addr)) {
38a03145e   Al Viro   [SCTP]: sctp_asso...
909
  			sctp_assoc_del_peer(asoc, &t->ipaddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
  }
8de8c8738   Sridhar Samudrala   [SCTP]: Set sk_er...
913
914
915
916
917
918
919
920
  /* 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;
  }
07d939677   Vlad Yasevich   [SCTP]: Set assoc...
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
  /* Helper function to generate an association change event */
  static void sctp_cmd_assoc_change(sctp_cmd_seq_t *commands,
  				 struct sctp_association *asoc,
  				 u8 state)
  {
  	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 */
  static void sctp_cmd_adaptation_ind(sctp_cmd_seq_t *commands,
  				    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);
  }
96cd0d3d7   Vlad Yasevich   sctp: enable cook...
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
  
  static void sctp_cmd_t1_timer_update(struct sctp_association *asoc,
  				    sctp_event_timeout_t timer,
  				    char *name)
  {
  	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++;
  		SCTP_DEBUG_PRINTK(
  			"T1 %s Timeout adjustment"
  			" init_err_counter: %d"
  			" cycle: %d"
  			" timeout: %ld
  ",
  			name,
  			asoc->init_err_counter,
  			asoc->init_cycle,
  			asoc->timeouts[timer]);
  	}
  
  }
9c5c62be2   Vlad Yasevich   sctp: Send user m...
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
  /* 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.
   */
  static int sctp_cmd_send_msg(struct sctp_association *asoc,
  				struct sctp_datamsg *msg)
  {
  	struct sctp_chunk *chunk;
  	int error = 0;
  
  	list_for_each_entry(chunk, &msg->chunks, frag_list) {
  		error = sctp_outq_tail(&asoc->outqueue, chunk);
  		if (error)
  			break;
  	}
  
  	return error;
  }
c07866934   Vlad Yasevich   sctp: Fix oops wh...
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
  /* Sent the next ASCONF packet currently stored in the association.
   * This happens after the ASCONF_ACK was succeffully processed.
   */
  static void sctp_cmd_send_asconf(struct sctp_association *asoc)
  {
  	/* Send the next asconf chunk from the addip chunk
  	 * queue.
  	 */
  	if (!list_empty(&asoc->addip_chunk_list)) {
  		struct list_head *entry = asoc->addip_chunk_list.next;
  		struct sctp_chunk *asconf = list_entry(entry,
  						struct sctp_chunk, list);
  		list_del_init(entry);
  
  		/* Hold the chunk until an ASCONF_ACK is received. */
  		sctp_chunk_hold(asconf);
  		if (sctp_primitive_ASCONF(asoc, asconf))
  			sctp_chunk_free(asconf);
  		else
  			asoc->addip_last_asconf = asconf;
  	}
  }
9c5c62be2   Vlad Yasevich   sctp: Send user m...
1016

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  /* 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
  ", \
  			  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
  ", \
  			  asoc, sctp_status_tbl[status])
  
  #define DEBUG_POST_SFX \
  	SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]
  ", \
  			  error, asoc, \
  			  sctp_state_tbl[(asoc && 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.
   */
  int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
  	       sctp_state_t state,
  	       struct sctp_endpoint *ep,
  	       struct sctp_association *asoc,
  	       void *event_arg,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
1053
  	       gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  {
  	sctp_cmd_seq_t commands;
  	const sctp_sm_table_entry_t *state_fn;
  	sctp_disposition_t status;
  	int error = 0;
  	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.
  	 */
  	state_fn = sctp_sm_lookup_event(event_type, state, subtype);
  
  	sctp_init_cmd_seq(&commands);
  
  	DEBUG_PRE;
  	status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
  	DEBUG_POST;
  
  	error = sctp_side_effects(event_type, subtype, state,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1078
  				  ep, asoc, event_arg, status,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
  				  &commands, gfp);
  	DEBUG_POST_SFX;
  
  	return error;
  }
  
  #undef DEBUG_PRE
  #undef DEBUG_POST
  
  /*****************************************************************
   * This the master state function side effect processing function.
   *****************************************************************/
  static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
  			     sctp_state_t state,
  			     struct sctp_endpoint *ep,
  			     struct sctp_association *asoc,
  			     void *event_arg,
  			     sctp_disposition_t status,
  			     sctp_cmd_seq_t *commands,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
1098
  			     gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  {
  	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,
  					       ep, asoc,
  					       event_arg, status,
  					       commands, gfp)))
  		goto bail;
  
  	switch (status) {
  	case SCTP_DISPOSITION_DISCARD:
  		SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
  				  "event_type %d, event_id %d
  ",
  				  state, event_type, subtype.chunk);
  		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;
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1131
  	case SCTP_DISPOSITION_DELETE_TCB:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  		/* This should now be a command. */
  		break;
  
  	case SCTP_DISPOSITION_CONSUME:
  	case SCTP_DISPOSITION_ABORT:
  		/*
  		 * 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:
d99fa4296   Wei Yongjun   SCTP: Use net_rat...
1144
  		if (net_ratelimit())
145ce502e   Joe Perches   net/sctp: Use pr_...
1145
1146
1147
  			pr_err("protocol violation state %d chunkid %d
  ",
  			       state, subtype.chunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1148
1149
1150
  		break;
  
  	case SCTP_DISPOSITION_NOT_IMPL:
145ce502e   Joe Perches   net/sctp: Use pr_...
1151
1152
1153
  		pr_warn("unimplemented feature in state %d, event_type %d, event_id %d
  ",
  			state, event_type, subtype.chunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
1155
1156
  		break;
  
  	case SCTP_DISPOSITION_BUG:
145ce502e   Joe Perches   net/sctp: Use pr_...
1157
1158
  		pr_err("bug in state %d, event_type %d, event_id %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
1160
1161
1162
1163
  		       state, event_type, subtype.chunk);
  		BUG();
  		break;
  
  	default:
145ce502e   Joe Perches   net/sctp: Use pr_...
1164
1165
  		pr_err("impossible disposition %d in state %d, event_type %d, event_id %d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
1168
  		       status, state, event_type, subtype.chunk);
  		BUG();
  		break;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
1169
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
  
  bail:
  	return error;
  }
  
  /********************************************************************
   * 2nd Level Abstractions
   ********************************************************************/
  
  /* This is the side-effect interpreter.  */
  static int sctp_cmd_interpreter(sctp_event_t event_type,
  				sctp_subtype_t subtype,
  				sctp_state_t state,
  				struct sctp_endpoint *ep,
  				struct sctp_association *asoc,
  				void *event_arg,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1186
  				sctp_disposition_t status,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187
  				sctp_cmd_seq_t *commands,
dd0fc66fb   Al Viro   [PATCH] gfp flags...
1188
  				gfp_t gfp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189
1190
1191
1192
1193
1194
1195
  {
  	int error = 0;
  	int force;
  	sctp_cmd_t *cmd;
  	struct sctp_chunk *new_obj;
  	struct sctp_chunk *chunk = NULL;
  	struct sctp_packet *packet;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
1197
1198
1199
1200
1201
1202
  	struct timer_list *timer;
  	unsigned long timeout;
  	struct sctp_transport *t;
  	struct sctp_sackhdr sackh;
  	int local_cork = 0;
  
  	if (SCTP_EVENT_T_TIMEOUT != event_type)
ea1107338   Joe Perches   net: Remove casts...
1203
  		chunk = event_arg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
  
  	/* 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:
  	 *     while (cmds)
  	 *         cmd->handle(x, y, z)
  	 * --jgrimm
  	 */
  	while (NULL != (cmd = sctp_next_cmd(commands))) {
  		switch (cmd->verb) {
  		case SCTP_CMD_NOP:
  			/* Do nothing. */
  			break;
  
  		case SCTP_CMD_NEW_ASOC:
  			/* Register a new association.  */
  			if (local_cork) {
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1221
  				sctp_outq_uncork(&asoc->outqueue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  				local_cork = 0;
  			}
  			asoc = cmd->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, cmd->obj.ptr);
  		       break;
  
  		case SCTP_CMD_PURGE_OUTQUEUE:
  		       sctp_outq_teardown(&asoc->outqueue);
  		       break;
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1237
  		case SCTP_CMD_DELETE_TCB:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
  			if (local_cork) {
  				sctp_outq_uncork(&asoc->outqueue);
  				local_cork = 0;
  			}
  			/* Delete the current association.  */
  			sctp_cmd_delete_tcb(commands, asoc);
  			asoc = NULL;
  			break;
  
  		case SCTP_CMD_NEW_STATE:
  			/* Enter a new state.  */
  			sctp_cmd_new_state(commands, asoc, cmd->obj.state);
  			break;
  
  		case SCTP_CMD_REPORT_TSN:
  			/* Record the arrival of a TSN.  */
8e1ee18c3   Vlad Yasevich   sctp: Rework the ...
1254
1255
  			error = sctp_tsnmap_mark(&asoc->peer.tsn_map,
  						 cmd->obj.u32);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1256
1257
1258
1259
1260
  			break;
  
  		case SCTP_CMD_REPORT_FWDTSN:
  			/* Move the Cumulattive TSN Ack ahead. */
  			sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
ea2dfb373   Vlad Yasevich   SCTP: properly cl...
1261
1262
  			/* purge the fragmentation queue */
  			sctp_ulpq_reasm_flushtsn(&asoc->ulpq, cmd->obj.u32);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1263
1264
1265
1266
1267
1268
  			/* Abort any in progress partial delivery. */
  			sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  			break;
  
  		case SCTP_CMD_PROCESS_FWDTSN:
  			sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1269
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  
  		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 = cmd->obj.i32;
  			error = sctp_gen_sack(asoc, force, commands);
  			break;
  
  		case SCTP_CMD_PROCESS_SACK:
  			/* Process an inbound SACK.  */
  			error = sctp_cmd_process_sack(commands, asoc,
  						      cmd->obj.ptr);
  			break;
  
  		case SCTP_CMD_GEN_INIT_ACK:
  			/* Generate an INIT ACK chunk.  */
  			new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
  						     0);
  			if (!new_obj)
  				goto nomem;
  
  			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  					SCTP_CHUNK(new_obj));
  			break;
  
  		case SCTP_CMD_PEER_INIT:
  			/* 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,
  						      cmd->obj.ptr, gfp);
  			break;
  
  		case SCTP_CMD_GEN_COOKIE_ECHO:
  			/* Generate a COOKIE ECHO chunk.  */
  			new_obj = sctp_make_cookie_echo(asoc, chunk);
  			if (!new_obj) {
  				if (cmd->obj.ptr)
  					sctp_chunk_free(cmd->obj.ptr);
  				goto nomem;
  			}
  			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  					SCTP_CHUNK(new_obj));
  
  			/* If there is an ERROR chunk to be sent along with
  			 * the COOKIE_ECHO, send it, too.
  			 */
  			if (cmd->obj.ptr)
  				sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  						SCTP_CHUNK(cmd->obj.ptr));
96cd0d3d7   Vlad Yasevich   sctp: enable cook...
1325
1326
1327
1328
  			if (new_obj->transport) {
  				new_obj->transport->init_sent_count++;
  				asoc->init_last_sent_to = new_obj->transport;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329
  			/* FIXME - Eventually come up with a cleaner way to
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1330
1331
1332
1333
1334
1335
  			 * enabling COOKIE-ECHO + DATA bundling during
  			 * multihoming stale cookie scenarios, the following
  			 * command plays with asoc->peer.retran_path to
  			 * avoid the problem of sending the COOKIE-ECHO and
  			 * DATA in different paths, which could result
  			 * in the association being ABORTed if the DATA chunk
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336
1337
1338
1339
1340
  			 * is processed first by the server.  Checking the
  			 * init error counter simply causes this command
  			 * to be executed only during failed attempts of
  			 * association establishment.
  			 */
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1341
1342
1343
1344
  			if ((asoc->peer.retran_path !=
  			     asoc->peer.primary_path) &&
  			    (asoc->init_err_counter > 0)) {
  				sctp_add_cmd_sf(commands,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1345
  						SCTP_CMD_FORCE_PRIM_RETRAN,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
  						SCTP_NULL());
  			}
  
  			break;
  
  		case SCTP_CMD_GEN_SHUTDOWN:
  			/* Generate SHUTDOWN when in SHUTDOWN_SENT state.
  			 * Reset error counts.
  			 */
  			asoc->overall_error_count = 0;
  
  			/* Generate a SHUTDOWN chunk.  */
  			new_obj = sctp_make_shutdown(asoc, chunk);
  			if (!new_obj)
  				goto nomem;
  			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  					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.
  ",
  					  "chunk_up:", cmd->obj.ptr,
  					  "ulpq:", &asoc->ulpq);
  			sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
  					    GFP_ATOMIC);
  			break;
  
  		case SCTP_CMD_EVENT_ULP:
  			/* Send a notification to the sockets layer.  */
  			SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.
  ",
  					  "event_up:",cmd->obj.ptr,
  					  "ulpq:",&asoc->ulpq);
  			sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
  			break;
  
  		case SCTP_CMD_REPLY:
  			/* If an caller has not already corked, do cork. */
  			if (!asoc->outqueue.cork) {
  				sctp_outq_cork(&asoc->outqueue);
  				local_cork = 1;
  			}
  			/* Send a chunk to our peer.  */
  			error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
  			break;
  
  		case SCTP_CMD_SEND_PKT:
  			/* Send a full packet to our peer.  */
  			packet = cmd->obj.ptr;
  			sctp_packet_transmit(packet);
  			sctp_ootb_pkt_free(packet);
  			break;
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
1400
1401
1402
1403
1404
  		case SCTP_CMD_T1_RETRAN:
  			/* Mark a transport for retransmission.  */
  			sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  					SCTP_RTXR_T1_RTX);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1405
1406
1407
1408
1409
  		case SCTP_CMD_RETRAN:
  			/* Mark a transport for retransmission.  */
  			sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  					SCTP_RTXR_T3_RTX);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
  		case SCTP_CMD_ECN_CE:
  			/* Do delayed CE processing.   */
  			sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
  			break;
  
  		case SCTP_CMD_ECN_ECNE:
  			/* Do delayed ECNE processing. */
  			new_obj = sctp_do_ecn_ecne_work(asoc, cmd->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, cmd->obj.u32);
  			break;
  
  		case SCTP_CMD_SETUP_T2:
  			sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
  			break;
f8d960524   Thomas Graf   sctp: Enforce ret...
1432
1433
1434
1435
1436
1437
  		case SCTP_CMD_TIMER_START_ONCE:
  			timer = &asoc->timers[cmd->obj.to];
  
  			if (timer_pending(timer))
  				break;
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1438
1439
1440
  		case SCTP_CMD_TIMER_START:
  			timer = &asoc->timers[cmd->obj.to];
  			timeout = asoc->timeouts[cmd->obj.to];
09a626600   Kris Katterjohn   [NET]: Change som...
1441
  			BUG_ON(!timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
  
  			timer->expires = jiffies + timeout;
  			sctp_association_hold(asoc);
  			add_timer(timer);
  			break;
  
  		case SCTP_CMD_TIMER_RESTART:
  			timer = &asoc->timers[cmd->obj.to];
  			timeout = asoc->timeouts[cmd->obj.to];
  			if (!mod_timer(timer, jiffies + timeout))
  				sctp_association_hold(asoc);
  			break;
  
  		case SCTP_CMD_TIMER_STOP:
  			timer = &asoc->timers[cmd->obj.to];
  			if (timer_pending(timer) && del_timer(timer))
  				sctp_association_put(asoc);
  			break;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1460
1461
  		case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
  			chunk = cmd->obj.ptr;
9919b455f   Wei Yongjun   sctp: fix to choo...
1462
1463
  			t = sctp_assoc_choose_alter_transport(asoc,
  						asoc->init_last_sent_to);
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1464
1465
1466
  			asoc->init_last_sent_to = t;
  			chunk->transport = t;
  			t->init_sent_count++;
e0e9db178   Vlad Yasevich   sctp: Select a wo...
1467
1468
  			/* Set the new transport as primary */
  			sctp_assoc_set_primary(asoc, t);
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1469
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1470
1471
1472
  		case SCTP_CMD_INIT_RESTART:
  			/* Do the needed accounting and updates
  			 * associated with restarting an initialization
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1473
1474
1475
1476
  			 * timer. Only multiply the timeout by two if
  			 * all transports have been tried at the current
  			 * timeout.
  			 */
96cd0d3d7   Vlad Yasevich   sctp: enable cook...
1477
1478
1479
  			sctp_cmd_t1_timer_update(asoc,
  						SCTP_EVENT_TIMEOUT_T1_INIT,
  						"INIT");
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
  
  			sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  					SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  			break;
  
  		case SCTP_CMD_COOKIEECHO_RESTART:
  			/* Do the needed accounting and updates
  			 * associated with restarting an initialization
  			 * timer. Only multiply the timeout by two if
  			 * all transports have been tried at the current
  			 * timeout.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1491
  			 */
96cd0d3d7   Vlad Yasevich   sctp: enable cook...
1492
1493
1494
  			sctp_cmd_t1_timer_update(asoc,
  						SCTP_EVENT_TIMEOUT_T1_COOKIE,
  						"COOKIE");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1495
1496
1497
1498
  
  			/* If we've sent any data bundled with
  			 * COOKIE-ECHO we need to resend.
  			 */
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
1499
1500
  			list_for_each_entry(t, &asoc->peer.transport_addr_list,
  					transports) {
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
1501
1502
  				sctp_retransmit_mark(&asoc->outqueue, t,
  					    SCTP_RTXR_T1_RTX);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
1504
1505
1506
  			}
  
  			sctp_add_cmd_sf(commands,
  					SCTP_CMD_TIMER_RESTART,
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1507
  					SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1508
1509
1510
  			break;
  
  		case SCTP_CMD_INIT_FAILED:
dc251b2b1   Al Viro   [SCTP]: SCTP_CMD_...
1511
  			sctp_cmd_init_failed(commands, asoc, cmd->obj.err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1512
1513
1514
1515
  			break;
  
  		case SCTP_CMD_ASSOC_FAILED:
  			sctp_cmd_assoc_failed(commands, asoc, event_type,
5be291fe2   Al Viro   [SCTP]: SCTP_CMD_...
1516
  					      subtype, chunk, cmd->obj.err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1517
  			break;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1518
1519
  		case SCTP_CMD_INIT_COUNTER_INC:
  			asoc->init_err_counter++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1520
  			break;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
1521
1522
1523
  		case SCTP_CMD_INIT_COUNTER_RESET:
  			asoc->init_err_counter = 0;
  			asoc->init_cycle = 0;
96cd0d3d7   Vlad Yasevich   sctp: enable cook...
1524
1525
1526
1527
  			list_for_each_entry(t, &asoc->peer.transport_addr_list,
  					    transports) {
  				t->init_sent_count = 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  			break;
  
  		case SCTP_CMD_REPORT_DUP:
  			sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
  					     cmd->obj.u32);
  			break;
  
  		case SCTP_CMD_REPORT_BAD_TAG:
  			SCTP_DEBUG_PRINTK("vtag mismatch!
  ");
  			break;
  
  		case SCTP_CMD_STRIKE:
  			/* Mark one strike against a transport.  */
7e99013a5   Vlad Yasevich   sctp: Fix broken ...
1542
1543
1544
1545
1546
1547
1548
  			sctp_do_8_2_transport_strike(asoc, cmd->obj.transport,
  						    0);
  			break;
  
  		case SCTP_CMD_TRANSPORT_IDLE:
  			t = cmd->obj.transport;
  			sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1549
  			break;
7e99013a5   Vlad Yasevich   sctp: Fix broken ...
1550
  		case SCTP_CMD_TRANSPORT_HB_SENT:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1551
  			t = cmd->obj.transport;
7e99013a5   Vlad Yasevich   sctp: Fix broken ...
1552
1553
  			sctp_do_8_2_transport_strike(asoc, t, 1);
  			t->hb_sent = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
  			break;
  
  		case SCTP_CMD_TRANSPORT_ON:
  			t = cmd->obj.transport;
  			sctp_cmd_transport_on(commands, asoc, t, chunk);
  			break;
  
  		case SCTP_CMD_HB_TIMERS_START:
  			sctp_cmd_hb_timers_start(commands, asoc);
  			break;
  
  		case SCTP_CMD_HB_TIMER_UPDATE:
  			t = cmd->obj.transport;
765ff02e8   Gui Jianfeng   [SCTP]: Remove an...
1567
  			sctp_cmd_hb_timer_update(commands, t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
  			break;
  
  		case SCTP_CMD_HB_TIMERS_STOP:
  			sctp_cmd_hb_timers_stop(commands, asoc);
  			break;
  
  		case SCTP_CMD_REPORT_ERROR:
  			error = cmd->obj.error;
  			break;
  
  		case SCTP_CMD_PROCESS_CTSN:
  			/* Dummy up a SACK for processing. */
2178eda82   Al Viro   [SCTP]: SCTP_CMD_...
1580
  			sackh.cum_tsn_ack = cmd->obj.be32;
d4d6fb578   Vlad Yasevich   sctp: Try not to ...
1581
1582
  			sackh.a_rwnd = asoc->peer.rwnd +
  					asoc->outqueue.outstanding_bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1583
1584
1585
1586
1587
1588
1589
  			sackh.num_gap_ack_blocks = 0;
  			sackh.num_dup_tsns = 0;
  			sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
  					SCTP_SACKH(&sackh));
  			break;
  
  		case SCTP_CMD_DISCARD_PACKET:
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
1590
1591
1592
1593
  			/* We need to discard the whole packet.
  			 * Uncork the queue since there might be
  			 * responses pending
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1594
  			chunk->pdiscard = 1;
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
1595
1596
1597
1598
  			if (asoc) {
  				sctp_outq_uncork(&asoc->outqueue);
  				local_cork = 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
  			break;
  
  		case SCTP_CMD_RTO_PENDING:
  			t = cmd->obj.transport;
  			t->rto_pending = 1;
  			break;
  
  		case SCTP_CMD_PART_DELIVER:
  			sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
  						   GFP_ATOMIC);
  			break;
  
  		case SCTP_CMD_RENEGE:
  			sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
  					 GFP_ATOMIC);
  			break;
  
  		case SCTP_CMD_SETUP_T4:
  			sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
  			break;
  
  		case SCTP_CMD_PROCESS_OPERR:
  			sctp_cmd_process_operr(commands, asoc, chunk);
  			break;
  		case SCTP_CMD_CLEAR_INIT_TAG:
  			asoc->peer.i.init_tag = 0;
  			break;
  		case SCTP_CMD_DEL_NON_PRIMARY:
  			sctp_cmd_del_non_primary(asoc);
  			break;
  		case SCTP_CMD_T3_RTX_TIMERS_STOP:
  			sctp_cmd_t3_rtx_timers_stop(commands, asoc);
  			break;
  		case SCTP_CMD_FORCE_PRIM_RETRAN:
  			t = asoc->peer.retran_path;
  			asoc->peer.retran_path = asoc->peer.primary_path;
  			error = sctp_outq_uncork(&asoc->outqueue);
  			local_cork = 0;
  			asoc->peer.retran_path = t;
  			break;
8de8c8738   Sridhar Samudrala   [SCTP]: Set sk_er...
1639
1640
1641
  		case SCTP_CMD_SET_SK_ERR:
  			sctp_cmd_set_sk_err(asoc, cmd->obj.error);
  			break;
07d939677   Vlad Yasevich   [SCTP]: Set assoc...
1642
1643
1644
1645
1646
1647
1648
  		case SCTP_CMD_ASSOC_CHANGE:
  			sctp_cmd_assoc_change(commands, asoc,
  					      cmd->obj.u8);
  			break;
  		case SCTP_CMD_ADAPTATION_IND:
  			sctp_cmd_adaptation_ind(commands, asoc);
  			break;
730fc3d05   Vlad Yasevich   [SCTP]: Implete S...
1649
1650
1651
1652
  		case SCTP_CMD_ASSOC_SHKEY:
  			error = sctp_auth_asoc_init_active_key(asoc,
  						GFP_ATOMIC);
  			break;
f4ad85ca3   Gui Jianfeng   [SCTP]: Fix proto...
1653
1654
1655
  		case SCTP_CMD_UPDATE_INITTAG:
  			asoc->peer.i.init_tag = cmd->obj.u32;
  			break;
9c5c62be2   Vlad Yasevich   sctp: Send user m...
1656
1657
1658
1659
1660
1661
1662
  		case SCTP_CMD_SEND_MSG:
  			if (!asoc->outqueue.cork) {
  				sctp_outq_cork(&asoc->outqueue);
  				local_cork = 1;
  			}
  			error = sctp_cmd_send_msg(asoc, cmd->obj.msg);
  			break;
c07866934   Vlad Yasevich   sctp: Fix oops wh...
1663
1664
1665
  		case SCTP_CMD_SEND_NEXT_ASCONF:
  			sctp_cmd_send_asconf(asoc);
  			break;
a000c01e6   Wei Yongjun   sctp: stop pendin...
1666
1667
1668
  		case SCTP_CMD_PURGE_ASCONF_QUEUE:
  			sctp_asconf_queue_teardown(asoc);
  			break;
d5ccd4966   Max Matveev   sctp: deal with m...
1669
1670
1671
1672
  
  		case SCTP_CMD_SET_ASOC:
  			asoc = cmd->obj.asoc;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
  		default:
145ce502e   Joe Perches   net/sctp: Use pr_...
1674
1675
1676
  			pr_warn("Impossible command: %u, %p
  ",
  				cmd->verb, cmd->obj.ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1677
  			break;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
1678
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1679
1680
1681
1682
1683
  		if (error)
  			break;
  	}
  
  out:
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
1684
1685
1686
1687
1688
1689
  	/* If this is in response to a received chunk, wait until
  	 * we are done with the packet to open the queue so that we don't
  	 * send multiple packets in response to a single request.
  	 */
  	if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) {
  		if (chunk->end_of_packet || chunk->singleton)
d4d6fb578   Vlad Yasevich   sctp: Try not to ...
1690
  			error = sctp_outq_uncork(&asoc->outqueue);
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
1691
  	} else if (local_cork)
d4d6fb578   Vlad Yasevich   sctp: Try not to ...
1692
  		error = sctp_outq_uncork(&asoc->outqueue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1693
1694
1695
1696
1697
  	return error;
  nomem:
  	error = -ENOMEM;
  	goto out;
  }