sctp: fix a type cast warnings that causes a_rwnd gets the wrong value
[pandora-kernel.git] / net / sctp / sm_sideeffect.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  *
6  * This file is part of the SCTP kernel implementation
7  *
8  * These functions work with the state functions in sctp_sm_statefuns.c
9  * to implement that state operations.  These functions implement the
10  * steps which require modifying existing data structures.
11  *
12  * This SCTP implementation is free software;
13  * you can redistribute it and/or modify it under the terms of
14  * the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This SCTP implementation is distributed in the hope that it
19  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20  *                 ************************
21  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22  * See the GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with GNU CC; see the file COPYING.  If not, write to
26  * the Free Software Foundation, 59 Temple Place - Suite 330,
27  * Boston, MA 02111-1307, USA.
28  *
29  * Please send any bug reports or fixes you make to the
30  * email address(es):
31  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
32  *
33  * Or submit a bug report through the following website:
34  *    http://www.sf.net/projects/lksctp
35  *
36  * Written or modified by:
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Karl Knutson          <karl@athena.chicago.il.us>
39  *    Jon Grimm             <jgrimm@austin.ibm.com>
40  *    Hui Huang             <hui.huang@nokia.com>
41  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
42  *    Daisy Chang           <daisyc@us.ibm.com>
43  *    Sridhar Samudrala     <sri@us.ibm.com>
44  *    Ardelle Fan           <ardelle.fan@intel.com>
45  *
46  * Any bugs reported given to us we will try to fix... any fixes shared will
47  * be incorporated into the next SCTP release.
48  */
49
50 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
52 #include <linux/skbuff.h>
53 #include <linux/types.h>
54 #include <linux/socket.h>
55 #include <linux/ip.h>
56 #include <linux/gfp.h>
57 #include <net/sock.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
60
61 static int sctp_cmd_interpreter(sctp_event_t event_type,
62                                 sctp_subtype_t subtype,
63                                 sctp_state_t state,
64                                 struct sctp_endpoint *ep,
65                                 struct sctp_association *asoc,
66                                 void *event_arg,
67                                 sctp_disposition_t status,
68                                 sctp_cmd_seq_t *commands,
69                                 gfp_t gfp);
70 static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
71                              sctp_state_t state,
72                              struct sctp_endpoint *ep,
73                              struct sctp_association *asoc,
74                              void *event_arg,
75                              sctp_disposition_t status,
76                              sctp_cmd_seq_t *commands,
77                              gfp_t gfp);
78
79 /********************************************************************
80  * Helper functions
81  ********************************************************************/
82
83 /* A helper function for delayed processing of INET ECN CE bit. */
84 static void sctp_do_ecn_ce_work(struct sctp_association *asoc,
85                                 __u32 lowest_tsn)
86 {
87         /* Save the TSN away for comparison when we receive CWR */
88
89         asoc->last_ecne_tsn = lowest_tsn;
90         asoc->need_ecne = 1;
91 }
92
93 /* Helper function for delayed processing of SCTP ECNE chunk.  */
94 /* RFC 2960 Appendix A
95  *
96  * RFC 2481 details a specific bit for a sender to send in
97  * the header of its next outbound TCP segment to indicate to
98  * its peer that it has reduced its congestion window.  This
99  * is termed the CWR bit.  For SCTP the same indication is made
100  * by including the CWR chunk.  This chunk contains one data
101  * element, i.e. the TSN number that was sent in the ECNE chunk.
102  * This element represents the lowest TSN number in the datagram
103  * that was originally marked with the CE bit.
104  */
105 static struct sctp_chunk *sctp_do_ecn_ecne_work(struct sctp_association *asoc,
106                                            __u32 lowest_tsn,
107                                            struct sctp_chunk *chunk)
108 {
109         struct sctp_chunk *repl;
110
111         /* Our previously transmitted packet ran into some congestion
112          * so we should take action by reducing cwnd and ssthresh
113          * and then ACK our peer that we we've done so by
114          * sending a CWR.
115          */
116
117         /* First, try to determine if we want to actually lower
118          * our cwnd variables.  Only lower them if the ECNE looks more
119          * recent than the last response.
120          */
121         if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) {
122                 struct sctp_transport *transport;
123
124                 /* Find which transport's congestion variables
125                  * need to be adjusted.
126                  */
127                 transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn);
128
129                 /* Update the congestion variables. */
130                 if (transport)
131                         sctp_transport_lower_cwnd(transport,
132                                                   SCTP_LOWER_CWND_ECNE);
133                 asoc->last_cwr_tsn = lowest_tsn;
134         }
135
136         /* Always try to quiet the other end.  In case of lost CWR,
137          * resend last_cwr_tsn.
138          */
139         repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk);
140
141         /* If we run out of memory, it will look like a lost CWR.  We'll
142          * get back in sync eventually.
143          */
144         return repl;
145 }
146
147 /* Helper function to do delayed processing of ECN CWR chunk.  */
148 static void sctp_do_ecn_cwr_work(struct sctp_association *asoc,
149                                  __u32 lowest_tsn)
150 {
151         /* Turn off ECNE getting auto-prepended to every outgoing
152          * packet
153          */
154         asoc->need_ecne = 0;
155 }
156
157 /* Generate SACK if necessary.  We call this at the end of a packet.  */
158 static int sctp_gen_sack(struct sctp_association *asoc, int force,
159                          sctp_cmd_seq_t *commands)
160 {
161         __u32 ctsn, max_tsn_seen;
162         struct sctp_chunk *sack;
163         struct sctp_transport *trans = asoc->peer.last_data_from;
164         int error = 0;
165
166         if (force ||
167             (!trans && (asoc->param_flags & SPP_SACKDELAY_DISABLE)) ||
168             (trans && (trans->param_flags & SPP_SACKDELAY_DISABLE)))
169                 asoc->peer.sack_needed = 1;
170
171         ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
172         max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
173
174         /* From 12.2 Parameters necessary per association (i.e. the TCB):
175          *
176          * Ack State : This flag indicates if the next received packet
177          *           : is to be responded to with a SACK. ...
178          *           : When DATA chunks are out of order, SACK's
179          *           : are not delayed (see Section 6).
180          *
181          * [This is actually not mentioned in Section 6, but we
182          * implement it here anyway. --piggy]
183          */
184         if (max_tsn_seen != ctsn)
185                 asoc->peer.sack_needed = 1;
186
187         /* From 6.2  Acknowledgement on Reception of DATA Chunks:
188          *
189          * Section 4.2 of [RFC2581] SHOULD be followed. Specifically,
190          * an acknowledgement SHOULD be generated for at least every
191          * second packet (not every second DATA chunk) received, and
192          * SHOULD be generated within 200 ms of the arrival of any
193          * unacknowledged DATA chunk. ...
194          */
195         if (!asoc->peer.sack_needed) {
196                 asoc->peer.sack_cnt++;
197
198                 /* Set the SACK delay timeout based on the
199                  * SACK delay for the last transport
200                  * data was received from, or the default
201                  * for the association.
202                  */
203                 if (trans) {
204                         /* We will need a SACK for the next packet.  */
205                         if (asoc->peer.sack_cnt >= trans->sackfreq - 1)
206                                 asoc->peer.sack_needed = 1;
207
208                         asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
209                                 trans->sackdelay;
210                 } else {
211                         /* We will need a SACK for the next packet.  */
212                         if (asoc->peer.sack_cnt >= asoc->sackfreq - 1)
213                                 asoc->peer.sack_needed = 1;
214
215                         asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
216                                 asoc->sackdelay;
217                 }
218
219                 /* Restart the SACK timer. */
220                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
221                                 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
222         } else {
223                 asoc->a_rwnd = asoc->rwnd;
224                 sack = sctp_make_sack(asoc);
225                 if (!sack)
226                         goto nomem;
227
228                 asoc->peer.sack_needed = 0;
229                 asoc->peer.sack_cnt = 0;
230
231                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(sack));
232
233                 /* Stop the SACK timer.  */
234                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
235                                 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
236         }
237
238         return error;
239 nomem:
240         error = -ENOMEM;
241         return error;
242 }
243
244 /* When the T3-RTX timer expires, it calls this function to create the
245  * relevant state machine event.
246  */
247 void sctp_generate_t3_rtx_event(unsigned long peer)
248 {
249         int error;
250         struct sctp_transport *transport = (struct sctp_transport *) peer;
251         struct sctp_association *asoc = transport->asoc;
252         struct sock *sk = asoc->base.sk;
253
254         /* Check whether a task is in the sock.  */
255
256         sctp_bh_lock_sock(sk);
257         if (sock_owned_by_user(sk)) {
258                 SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __func__);
259
260                 /* Try again later.  */
261                 if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
262                         sctp_transport_hold(transport);
263                 goto out_unlock;
264         }
265
266         /* Is this transport really dead and just waiting around for
267          * the timer to let go of the reference?
268          */
269         if (transport->dead)
270                 goto out_unlock;
271
272         /* Run through the state machine.  */
273         error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
274                            SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
275                            asoc->state,
276                            asoc->ep, asoc,
277                            transport, GFP_ATOMIC);
278
279         if (error)
280                 sk->sk_err = -error;
281
282 out_unlock:
283         sctp_bh_unlock_sock(sk);
284         sctp_transport_put(transport);
285 }
286
287 /* This is a sa interface for producing timeout events.  It works
288  * for timeouts which use the association as their parameter.
289  */
290 static void sctp_generate_timeout_event(struct sctp_association *asoc,
291                                         sctp_event_timeout_t timeout_type)
292 {
293         struct sock *sk = asoc->base.sk;
294         int error = 0;
295
296         sctp_bh_lock_sock(sk);
297         if (sock_owned_by_user(sk)) {
298                 SCTP_DEBUG_PRINTK("%s:Sock is busy: timer %d\n",
299                                   __func__,
300                                   timeout_type);
301
302                 /* Try again later.  */
303                 if (!mod_timer(&asoc->timers[timeout_type], jiffies + (HZ/20)))
304                         sctp_association_hold(asoc);
305                 goto out_unlock;
306         }
307
308         /* Is this association really dead and just waiting around for
309          * the timer to let go of the reference?
310          */
311         if (asoc->base.dead)
312                 goto out_unlock;
313
314         /* Run through the state machine.  */
315         error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
316                            SCTP_ST_TIMEOUT(timeout_type),
317                            asoc->state, asoc->ep, asoc,
318                            (void *)timeout_type, GFP_ATOMIC);
319
320         if (error)
321                 sk->sk_err = -error;
322
323 out_unlock:
324         sctp_bh_unlock_sock(sk);
325         sctp_association_put(asoc);
326 }
327
328 static void sctp_generate_t1_cookie_event(unsigned long data)
329 {
330         struct sctp_association *asoc = (struct sctp_association *) data;
331         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE);
332 }
333
334 static void sctp_generate_t1_init_event(unsigned long data)
335 {
336         struct sctp_association *asoc = (struct sctp_association *) data;
337         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT);
338 }
339
340 static void sctp_generate_t2_shutdown_event(unsigned long data)
341 {
342         struct sctp_association *asoc = (struct sctp_association *) data;
343         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN);
344 }
345
346 static void sctp_generate_t4_rto_event(unsigned long data)
347 {
348         struct sctp_association *asoc = (struct sctp_association *) data;
349         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T4_RTO);
350 }
351
352 static void sctp_generate_t5_shutdown_guard_event(unsigned long data)
353 {
354         struct sctp_association *asoc = (struct sctp_association *)data;
355         sctp_generate_timeout_event(asoc,
356                                     SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD);
357
358 } /* sctp_generate_t5_shutdown_guard_event() */
359
360 static void sctp_generate_autoclose_event(unsigned long data)
361 {
362         struct sctp_association *asoc = (struct sctp_association *) data;
363         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE);
364 }
365
366 /* Generate a heart beat event.  If the sock is busy, reschedule.   Make
367  * sure that the transport is still valid.
368  */
369 void sctp_generate_heartbeat_event(unsigned long data)
370 {
371         int error = 0;
372         struct sctp_transport *transport = (struct sctp_transport *) data;
373         struct sctp_association *asoc = transport->asoc;
374         struct sock *sk = asoc->base.sk;
375
376         sctp_bh_lock_sock(sk);
377         if (sock_owned_by_user(sk)) {
378                 SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __func__);
379
380                 /* Try again later.  */
381                 if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
382                         sctp_transport_hold(transport);
383                 goto out_unlock;
384         }
385
386         /* Is this structure just waiting around for us to actually
387          * get destroyed?
388          */
389         if (transport->dead)
390                 goto out_unlock;
391
392         error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
393                            SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
394                            asoc->state, asoc->ep, asoc,
395                            transport, GFP_ATOMIC);
396
397          if (error)
398                 sk->sk_err = -error;
399
400 out_unlock:
401         sctp_bh_unlock_sock(sk);
402         sctp_transport_put(transport);
403 }
404
405 /* Handle the timeout of the ICMP protocol unreachable timer.  Trigger
406  * the correct state machine transition that will close the association.
407  */
408 void sctp_generate_proto_unreach_event(unsigned long data)
409 {
410         struct sctp_transport *transport = (struct sctp_transport *) data;
411         struct sctp_association *asoc = transport->asoc;
412         struct sock *sk = asoc->base.sk;
413         
414         sctp_bh_lock_sock(sk);
415         if (sock_owned_by_user(sk)) {
416                 SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __func__);
417
418                 /* Try again later.  */
419                 if (!mod_timer(&transport->proto_unreach_timer,
420                                 jiffies + (HZ/20)))
421                         sctp_association_hold(asoc);
422                 goto out_unlock;
423         }
424
425         /* Is this structure just waiting around for us to actually
426          * get destroyed?
427          */
428         if (asoc->base.dead)
429                 goto out_unlock;
430
431         sctp_do_sm(SCTP_EVENT_T_OTHER,
432                    SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
433                    asoc->state, asoc->ep, asoc, transport, GFP_ATOMIC);
434
435 out_unlock:
436         sctp_bh_unlock_sock(sk);
437         sctp_association_put(asoc);
438 }
439
440
441 /* Inject a SACK Timeout event into the state machine.  */
442 static void sctp_generate_sack_event(unsigned long data)
443 {
444         struct sctp_association *asoc = (struct sctp_association *) data;
445         sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK);
446 }
447
448 sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES] = {
449         NULL,
450         sctp_generate_t1_cookie_event,
451         sctp_generate_t1_init_event,
452         sctp_generate_t2_shutdown_event,
453         NULL,
454         sctp_generate_t4_rto_event,
455         sctp_generate_t5_shutdown_guard_event,
456         NULL,
457         sctp_generate_sack_event,
458         sctp_generate_autoclose_event,
459 };
460
461
462 /* RFC 2960 8.2 Path Failure Detection
463  *
464  * When its peer endpoint is multi-homed, an endpoint should keep a
465  * error counter for each of the destination transport addresses of the
466  * peer endpoint.
467  *
468  * Each time the T3-rtx timer expires on any address, or when a
469  * HEARTBEAT sent to an idle address is not acknowledged within a RTO,
470  * the error counter of that destination address will be incremented.
471  * When the value in the error counter exceeds the protocol parameter
472  * 'Path.Max.Retrans' of that destination address, the endpoint should
473  * mark the destination transport address as inactive, and a
474  * notification SHOULD be sent to the upper layer.
475  *
476  */
477 static void sctp_do_8_2_transport_strike(struct sctp_association *asoc,
478                                          struct sctp_transport *transport,
479                                          int is_hb)
480 {
481         /* The check for association's overall error counter exceeding the
482          * threshold is done in the state function.
483          */
484         /* We are here due to a timer expiration.  If the timer was
485          * not a HEARTBEAT, then normal error tracking is done.
486          * If the timer was a heartbeat, we only increment error counts
487          * when we already have an outstanding HEARTBEAT that has not
488          * been acknowledged.
489          * Additionally, some tranport states inhibit error increments.
490          */
491         if (!is_hb) {
492                 asoc->overall_error_count++;
493                 if (transport->state != SCTP_INACTIVE)
494                         transport->error_count++;
495          } else if (transport->hb_sent) {
496                 if (transport->state != SCTP_UNCONFIRMED)
497                         asoc->overall_error_count++;
498                 if (transport->state != SCTP_INACTIVE)
499                         transport->error_count++;
500         }
501
502         if (transport->state != SCTP_INACTIVE &&
503             (transport->error_count > transport->pathmaxrxt)) {
504                 SCTP_DEBUG_PRINTK_IPADDR("transport_strike:association %p",
505                                          " transport IP: port:%d failed.\n",
506                                          asoc,
507                                          (&transport->ipaddr),
508                                          ntohs(transport->ipaddr.v4.sin_port));
509                 sctp_assoc_control_transport(asoc, transport,
510                                              SCTP_TRANSPORT_DOWN,
511                                              SCTP_FAILED_THRESHOLD);
512         }
513
514         /* E2) For the destination address for which the timer
515          * expires, set RTO <- RTO * 2 ("back off the timer").  The
516          * maximum value discussed in rule C7 above (RTO.max) may be
517          * used to provide an upper bound to this doubling operation.
518          *
519          * Special Case:  the first HB doesn't trigger exponential backoff.
520          * The first unacknowledged HB triggers it.  We do this with a flag
521          * that indicates that we have an outstanding HB.
522          */
523         if (!is_hb || transport->hb_sent) {
524                 transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
525         }
526 }
527
528 /* Worker routine to handle INIT command failure.  */
529 static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
530                                  struct sctp_association *asoc,
531                                  unsigned error)
532 {
533         struct sctp_ulpevent *event;
534
535         event = sctp_ulpevent_make_assoc_change(asoc,0, SCTP_CANT_STR_ASSOC,
536                                                 (__u16)error, 0, 0, NULL,
537                                                 GFP_ATOMIC);
538
539         if (event)
540                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
541                                 SCTP_ULPEVENT(event));
542
543         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
544                         SCTP_STATE(SCTP_STATE_CLOSED));
545
546         /* SEND_FAILED sent later when cleaning up the association. */
547         asoc->outqueue.error = error;
548         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
549 }
550
551 /* Worker routine to handle SCTP_CMD_ASSOC_FAILED.  */
552 static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
553                                   struct sctp_association *asoc,
554                                   sctp_event_t event_type,
555                                   sctp_subtype_t subtype,
556                                   struct sctp_chunk *chunk,
557                                   unsigned error)
558 {
559         struct sctp_ulpevent *event;
560
561         /* Cancel any partial delivery in progress. */
562         sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
563
564         if (event_type == SCTP_EVENT_T_CHUNK && subtype.chunk == SCTP_CID_ABORT)
565                 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
566                                                 (__u16)error, 0, 0, chunk,
567                                                 GFP_ATOMIC);
568         else
569                 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
570                                                 (__u16)error, 0, 0, NULL,
571                                                 GFP_ATOMIC);
572         if (event)
573                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
574                                 SCTP_ULPEVENT(event));
575
576         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
577                         SCTP_STATE(SCTP_STATE_CLOSED));
578
579         /* SEND_FAILED sent later when cleaning up the association. */
580         asoc->outqueue.error = error;
581         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
582 }
583
584 /* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
585  * inside the cookie.  In reality, this is only used for INIT-ACK processing
586  * since all other cases use "temporary" associations and can do all
587  * their work in statefuns directly.
588  */
589 static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
590                                  struct sctp_association *asoc,
591                                  struct sctp_chunk *chunk,
592                                  sctp_init_chunk_t *peer_init,
593                                  gfp_t gfp)
594 {
595         int error;
596
597         /* We only process the init as a sideeffect in a single
598          * case.   This is when we process the INIT-ACK.   If we
599          * fail during INIT processing (due to malloc problems),
600          * just return the error and stop processing the stack.
601          */
602         if (!sctp_process_init(asoc, chunk, sctp_source(chunk), peer_init, gfp))
603                 error = -ENOMEM;
604         else
605                 error = 0;
606
607         return error;
608 }
609
610 /* Helper function to break out starting up of heartbeat timers.  */
611 static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
612                                      struct sctp_association *asoc)
613 {
614         struct sctp_transport *t;
615
616         /* Start a heartbeat timer for each transport on the association.
617          * hold a reference on the transport to make sure none of
618          * the needed data structures go away.
619          */
620         list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) {
621
622                 if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
623                         sctp_transport_hold(t);
624         }
625 }
626
627 static void sctp_cmd_hb_timers_stop(sctp_cmd_seq_t *cmds,
628                                     struct sctp_association *asoc)
629 {
630         struct sctp_transport *t;
631
632         /* Stop all heartbeat timers. */
633
634         list_for_each_entry(t, &asoc->peer.transport_addr_list,
635                         transports) {
636                 if (del_timer(&t->hb_timer))
637                         sctp_transport_put(t);
638         }
639 }
640
641 /* Helper function to stop any pending T3-RTX timers */
642 static void sctp_cmd_t3_rtx_timers_stop(sctp_cmd_seq_t *cmds,
643                                         struct sctp_association *asoc)
644 {
645         struct sctp_transport *t;
646
647         list_for_each_entry(t, &asoc->peer.transport_addr_list,
648                         transports) {
649                 if (timer_pending(&t->T3_rtx_timer) &&
650                     del_timer(&t->T3_rtx_timer)) {
651                         sctp_transport_put(t);
652                 }
653         }
654 }
655
656
657 /* Helper function to update the heartbeat timer. */
658 static void sctp_cmd_hb_timer_update(sctp_cmd_seq_t *cmds,
659                                      struct sctp_transport *t)
660 {
661         /* Update the heartbeat timer.  */
662         if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
663                 sctp_transport_hold(t);
664 }
665
666 /* Helper function to handle the reception of an HEARTBEAT ACK.  */
667 static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
668                                   struct sctp_association *asoc,
669                                   struct sctp_transport *t,
670                                   struct sctp_chunk *chunk)
671 {
672         sctp_sender_hb_info_t *hbinfo;
673
674         /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the
675          * HEARTBEAT should clear the error counter of the destination
676          * transport address to which the HEARTBEAT was sent.
677          */
678         t->error_count = 0;
679
680         /*
681          * Although RFC4960 specifies that the overall error count must
682          * be cleared when a HEARTBEAT ACK is received, we make an
683          * exception while in SHUTDOWN PENDING. If the peer keeps its
684          * window shut forever, we may never be able to transmit our
685          * outstanding data and rely on the retransmission limit be reached
686          * to shutdown the association.
687          */
688         if (t->asoc->state < SCTP_STATE_SHUTDOWN_PENDING)
689                 t->asoc->overall_error_count = 0;
690
691         /* Clear the hb_sent flag to signal that we had a good
692          * acknowledgement.
693          */
694         t->hb_sent = 0;
695
696         /* Mark the destination transport address as active if it is not so
697          * marked.
698          */
699         if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED))
700                 sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
701                                              SCTP_HEARTBEAT_SUCCESS);
702
703         /* The receiver of the HEARTBEAT ACK should also perform an
704          * RTT measurement for that destination transport address
705          * using the time value carried in the HEARTBEAT ACK chunk.
706          * If the transport's rto_pending variable has been cleared,
707          * it was most likely due to a retransmit.  However, we want
708          * to re-enable it to properly update the rto.
709          */
710         if (t->rto_pending == 0)
711                 t->rto_pending = 1;
712
713         hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
714         sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
715
716         /* Update the heartbeat timer.  */
717         if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
718                 sctp_transport_hold(t);
719 }
720
721
722 /* Helper function to process the process SACK command.  */
723 static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
724                                  struct sctp_association *asoc,
725                                  struct sctp_sackhdr *sackh)
726 {
727         int err = 0;
728
729         if (sctp_outq_sack(&asoc->outqueue, sackh)) {
730                 /* There are no more TSNs awaiting SACK.  */
731                 err = sctp_do_sm(SCTP_EVENT_T_OTHER,
732                                  SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
733                                  asoc->state, asoc->ep, asoc, NULL,
734                                  GFP_ATOMIC);
735         }
736
737         return err;
738 }
739
740 /* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
741  * the transport for a shutdown chunk.
742  */
743 static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds,
744                               struct sctp_association *asoc,
745                               struct sctp_chunk *chunk)
746 {
747         struct sctp_transport *t;
748
749         if (chunk->transport)
750                 t = chunk->transport;
751         else {
752                 t = sctp_assoc_choose_alter_transport(asoc,
753                                               asoc->shutdown_last_sent_to);
754                 chunk->transport = t;
755         }
756         asoc->shutdown_last_sent_to = t;
757         asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
758 }
759
760 /* Helper function to change the state of an association. */
761 static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds,
762                                struct sctp_association *asoc,
763                                sctp_state_t state)
764 {
765         struct sock *sk = asoc->base.sk;
766
767         asoc->state = state;
768
769         SCTP_DEBUG_PRINTK("sctp_cmd_new_state: asoc %p[%s]\n",
770                           asoc, sctp_state_tbl[state]);
771
772         if (sctp_style(sk, TCP)) {
773                 /* Change the sk->sk_state of a TCP-style socket that has
774                  * successfully completed a connect() call.
775                  */
776                 if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED))
777                         sk->sk_state = SCTP_SS_ESTABLISHED;
778
779                 /* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */
780                 if (sctp_state(asoc, SHUTDOWN_RECEIVED) &&
781                     sctp_sstate(sk, ESTABLISHED))
782                         sk->sk_shutdown |= RCV_SHUTDOWN;
783         }
784
785         if (sctp_state(asoc, COOKIE_WAIT)) {
786                 /* Reset init timeouts since they may have been
787                  * increased due to timer expirations.
788                  */
789                 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
790                                                 asoc->rto_initial;
791                 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
792                                                 asoc->rto_initial;
793         }
794
795         if (sctp_state(asoc, ESTABLISHED) ||
796             sctp_state(asoc, CLOSED) ||
797             sctp_state(asoc, SHUTDOWN_RECEIVED)) {
798                 /* Wake up any processes waiting in the asoc's wait queue in
799                  * sctp_wait_for_connect() or sctp_wait_for_sndbuf().
800                  */
801                 if (waitqueue_active(&asoc->wait))
802                         wake_up_interruptible(&asoc->wait);
803
804                 /* Wake up any processes waiting in the sk's sleep queue of
805                  * a TCP-style or UDP-style peeled-off socket in
806                  * sctp_wait_for_accept() or sctp_wait_for_packet().
807                  * For a UDP-style socket, the waiters are woken up by the
808                  * notifications.
809                  */
810                 if (!sctp_style(sk, UDP))
811                         sk->sk_state_change(sk);
812         }
813 }
814
815 /* Helper function to delete an association. */
816 static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
817                                 struct sctp_association *asoc)
818 {
819         struct sock *sk = asoc->base.sk;
820
821         /* If it is a non-temporary association belonging to a TCP-style
822          * listening socket that is not closed, do not free it so that accept()
823          * can pick it up later.
824          */
825         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
826             (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
827                 return;
828
829         sctp_unhash_established(asoc);
830         sctp_association_free(asoc);
831 }
832
833 /*
834  * ADDIP Section 4.1 ASCONF Chunk Procedures
835  * A4) Start a T-4 RTO timer, using the RTO value of the selected
836  * destination address (we use active path instead of primary path just
837  * because primary path may be inactive.
838  */
839 static void sctp_cmd_setup_t4(sctp_cmd_seq_t *cmds,
840                                 struct sctp_association *asoc,
841                                 struct sctp_chunk *chunk)
842 {
843         struct sctp_transport *t;
844
845         t = sctp_assoc_choose_alter_transport(asoc, chunk->transport);
846         asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
847         chunk->transport = t;
848 }
849
850 /* Process an incoming Operation Error Chunk. */
851 static void sctp_cmd_process_operr(sctp_cmd_seq_t *cmds,
852                                    struct sctp_association *asoc,
853                                    struct sctp_chunk *chunk)
854 {
855         struct sctp_errhdr *err_hdr;
856         struct sctp_ulpevent *ev;
857
858         while (chunk->chunk_end > chunk->skb->data) {
859                 err_hdr = (struct sctp_errhdr *)(chunk->skb->data);
860
861                 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
862                                                      GFP_ATOMIC);
863                 if (!ev)
864                         return;
865
866                 sctp_ulpq_tail_event(&asoc->ulpq, ev);
867
868                 switch (err_hdr->cause) {
869                 case SCTP_ERROR_UNKNOWN_CHUNK:
870                 {
871                         sctp_chunkhdr_t *unk_chunk_hdr;
872
873                         unk_chunk_hdr = (sctp_chunkhdr_t *)err_hdr->variable;
874                         switch (unk_chunk_hdr->type) {
875                         /* ADDIP 4.1 A9) If the peer responds to an ASCONF with
876                          * an ERROR chunk reporting that it did not recognized
877                          * the ASCONF chunk type, the sender of the ASCONF MUST
878                          * NOT send any further ASCONF chunks and MUST stop its
879                          * T-4 timer.
880                          */
881                         case SCTP_CID_ASCONF:
882                                 if (asoc->peer.asconf_capable == 0)
883                                         break;
884
885                                 asoc->peer.asconf_capable = 0;
886                                 sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
887                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
888                                 break;
889                         default:
890                                 break;
891                         }
892                         break;
893                 }
894                 default:
895                         break;
896                 }
897         }
898 }
899
900 /* Process variable FWDTSN chunk information. */
901 static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
902                                     struct sctp_chunk *chunk)
903 {
904         struct sctp_fwdtsn_skip *skip;
905         /* Walk through all the skipped SSNs */
906         sctp_walk_fwdtsn(skip, chunk) {
907                 sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
908         }
909 }
910
911 /* Helper function to remove the association non-primary peer
912  * transports.
913  */
914 static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
915 {
916         struct sctp_transport *t;
917         struct list_head *pos;
918         struct list_head *temp;
919
920         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
921                 t = list_entry(pos, struct sctp_transport, transports);
922                 if (!sctp_cmp_addr_exact(&t->ipaddr,
923                                          &asoc->peer.primary_addr)) {
924                         sctp_assoc_del_peer(asoc, &t->ipaddr);
925                 }
926         }
927 }
928
929 /* Helper function to set sk_err on a 1-1 style socket. */
930 static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
931 {
932         struct sock *sk = asoc->base.sk;
933
934         if (!sctp_style(sk, UDP))
935                 sk->sk_err = error;
936 }
937
938 /* Helper function to generate an association change event */
939 static void sctp_cmd_assoc_change(sctp_cmd_seq_t *commands,
940                                  struct sctp_association *asoc,
941                                  u8 state)
942 {
943         struct sctp_ulpevent *ev;
944
945         ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
946                                             asoc->c.sinit_num_ostreams,
947                                             asoc->c.sinit_max_instreams,
948                                             NULL, GFP_ATOMIC);
949         if (ev)
950                 sctp_ulpq_tail_event(&asoc->ulpq, ev);
951 }
952
953 /* Helper function to generate an adaptation indication event */
954 static void sctp_cmd_adaptation_ind(sctp_cmd_seq_t *commands,
955                                     struct sctp_association *asoc)
956 {
957         struct sctp_ulpevent *ev;
958
959         ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
960
961         if (ev)
962                 sctp_ulpq_tail_event(&asoc->ulpq, ev);
963 }
964
965
966 static void sctp_cmd_t1_timer_update(struct sctp_association *asoc,
967                                     sctp_event_timeout_t timer,
968                                     char *name)
969 {
970         struct sctp_transport *t;
971
972         t = asoc->init_last_sent_to;
973         asoc->init_err_counter++;
974
975         if (t->init_sent_count > (asoc->init_cycle + 1)) {
976                 asoc->timeouts[timer] *= 2;
977                 if (asoc->timeouts[timer] > asoc->max_init_timeo) {
978                         asoc->timeouts[timer] = asoc->max_init_timeo;
979                 }
980                 asoc->init_cycle++;
981                 SCTP_DEBUG_PRINTK(
982                         "T1 %s Timeout adjustment"
983                         " init_err_counter: %d"
984                         " cycle: %d"
985                         " timeout: %ld\n",
986                         name,
987                         asoc->init_err_counter,
988                         asoc->init_cycle,
989                         asoc->timeouts[timer]);
990         }
991
992 }
993
994 /* Send the whole message, chunk by chunk, to the outqueue.
995  * This way the whole message is queued up and bundling if
996  * encouraged for small fragments.
997  */
998 static void sctp_cmd_send_msg(struct sctp_association *asoc,
999                               struct sctp_datamsg *msg)
1000 {
1001         struct sctp_chunk *chunk;
1002
1003         list_for_each_entry(chunk, &msg->chunks, frag_list)
1004                 sctp_outq_tail(&asoc->outqueue, chunk);
1005 }
1006
1007
1008 /* Sent the next ASCONF packet currently stored in the association.
1009  * This happens after the ASCONF_ACK was succeffully processed.
1010  */
1011 static void sctp_cmd_send_asconf(struct sctp_association *asoc)
1012 {
1013         /* Send the next asconf chunk from the addip chunk
1014          * queue.
1015          */
1016         if (!list_empty(&asoc->addip_chunk_list)) {
1017                 struct list_head *entry = asoc->addip_chunk_list.next;
1018                 struct sctp_chunk *asconf = list_entry(entry,
1019                                                 struct sctp_chunk, list);
1020                 list_del_init(entry);
1021
1022                 /* Hold the chunk until an ASCONF_ACK is received. */
1023                 sctp_chunk_hold(asconf);
1024                 if (sctp_primitive_ASCONF(asoc, asconf))
1025                         sctp_chunk_free(asconf);
1026                 else
1027                         asoc->addip_last_asconf = asconf;
1028         }
1029 }
1030
1031
1032 /* These three macros allow us to pull the debugging code out of the
1033  * main flow of sctp_do_sm() to keep attention focused on the real
1034  * functionality there.
1035  */
1036 #define DEBUG_PRE \
1037         SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
1038                           "ep %p, %s, %s, asoc %p[%s], %s\n", \
1039                           ep, sctp_evttype_tbl[event_type], \
1040                           (*debug_fn)(subtype), asoc, \
1041                           sctp_state_tbl[state], state_fn->name)
1042
1043 #define DEBUG_POST \
1044         SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
1045                           "asoc %p, status: %s\n", \
1046                           asoc, sctp_status_tbl[status])
1047
1048 #define DEBUG_POST_SFX \
1049         SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
1050                           error, asoc, \
1051                           sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
1052                           sctp_assoc2id(asoc)))?asoc->state:SCTP_STATE_CLOSED])
1053
1054 /*
1055  * This is the master state machine processing function.
1056  *
1057  * If you want to understand all of lksctp, this is a
1058  * good place to start.
1059  */
1060 int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
1061                sctp_state_t state,
1062                struct sctp_endpoint *ep,
1063                struct sctp_association *asoc,
1064                void *event_arg,
1065                gfp_t gfp)
1066 {
1067         sctp_cmd_seq_t commands;
1068         const sctp_sm_table_entry_t *state_fn;
1069         sctp_disposition_t status;
1070         int error = 0;
1071         typedef const char *(printfn_t)(sctp_subtype_t);
1072
1073         static printfn_t *table[] = {
1074                 NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
1075         };
1076         printfn_t *debug_fn  __attribute__ ((unused)) = table[event_type];
1077
1078         /* Look up the state function, run it, and then process the
1079          * side effects.  These three steps are the heart of lksctp.
1080          */
1081         state_fn = sctp_sm_lookup_event(event_type, state, subtype);
1082
1083         sctp_init_cmd_seq(&commands);
1084
1085         DEBUG_PRE;
1086         status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
1087         DEBUG_POST;
1088
1089         error = sctp_side_effects(event_type, subtype, state,
1090                                   ep, asoc, event_arg, status,
1091                                   &commands, gfp);
1092         DEBUG_POST_SFX;
1093
1094         return error;
1095 }
1096
1097 #undef DEBUG_PRE
1098 #undef DEBUG_POST
1099
1100 /*****************************************************************
1101  * This the master state function side effect processing function.
1102  *****************************************************************/
1103 static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
1104                              sctp_state_t state,
1105                              struct sctp_endpoint *ep,
1106                              struct sctp_association *asoc,
1107                              void *event_arg,
1108                              sctp_disposition_t status,
1109                              sctp_cmd_seq_t *commands,
1110                              gfp_t gfp)
1111 {
1112         int error;
1113
1114         /* FIXME - Most of the dispositions left today would be categorized
1115          * as "exceptional" dispositions.  For those dispositions, it
1116          * may not be proper to run through any of the commands at all.
1117          * For example, the command interpreter might be run only with
1118          * disposition SCTP_DISPOSITION_CONSUME.
1119          */
1120         if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
1121                                                ep, asoc,
1122                                                event_arg, status,
1123                                                commands, gfp)))
1124                 goto bail;
1125
1126         switch (status) {
1127         case SCTP_DISPOSITION_DISCARD:
1128                 SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
1129                                   "event_type %d, event_id %d\n",
1130                                   state, event_type, subtype.chunk);
1131                 break;
1132
1133         case SCTP_DISPOSITION_NOMEM:
1134                 /* We ran out of memory, so we need to discard this
1135                  * packet.
1136                  */
1137                 /* BUG--we should now recover some memory, probably by
1138                  * reneging...
1139                  */
1140                 error = -ENOMEM;
1141                 break;
1142
1143         case SCTP_DISPOSITION_DELETE_TCB:
1144                 /* This should now be a command. */
1145                 break;
1146
1147         case SCTP_DISPOSITION_CONSUME:
1148         case SCTP_DISPOSITION_ABORT:
1149                 /*
1150                  * We should no longer have much work to do here as the
1151                  * real work has been done as explicit commands above.
1152                  */
1153                 break;
1154
1155         case SCTP_DISPOSITION_VIOLATION:
1156                 if (net_ratelimit())
1157                         pr_err("protocol violation state %d chunkid %d\n",
1158                                state, subtype.chunk);
1159                 break;
1160
1161         case SCTP_DISPOSITION_NOT_IMPL:
1162                 pr_warn("unimplemented feature in state %d, event_type %d, event_id %d\n",
1163                         state, event_type, subtype.chunk);
1164                 break;
1165
1166         case SCTP_DISPOSITION_BUG:
1167                 pr_err("bug in state %d, event_type %d, event_id %d\n",
1168                        state, event_type, subtype.chunk);
1169                 BUG();
1170                 break;
1171
1172         default:
1173                 pr_err("impossible disposition %d in state %d, event_type %d, event_id %d\n",
1174                        status, state, event_type, subtype.chunk);
1175                 BUG();
1176                 break;
1177         }
1178
1179 bail:
1180         return error;
1181 }
1182
1183 /********************************************************************
1184  * 2nd Level Abstractions
1185  ********************************************************************/
1186
1187 /* This is the side-effect interpreter.  */
1188 static int sctp_cmd_interpreter(sctp_event_t event_type,
1189                                 sctp_subtype_t subtype,
1190                                 sctp_state_t state,
1191                                 struct sctp_endpoint *ep,
1192                                 struct sctp_association *asoc,
1193                                 void *event_arg,
1194                                 sctp_disposition_t status,
1195                                 sctp_cmd_seq_t *commands,
1196                                 gfp_t gfp)
1197 {
1198         int error = 0;
1199         int force;
1200         sctp_cmd_t *cmd;
1201         struct sctp_chunk *new_obj;
1202         struct sctp_chunk *chunk = NULL;
1203         struct sctp_packet *packet;
1204         struct timer_list *timer;
1205         unsigned long timeout;
1206         struct sctp_transport *t;
1207         struct sctp_sackhdr sackh;
1208         int local_cork = 0;
1209
1210         if (SCTP_EVENT_T_TIMEOUT != event_type)
1211                 chunk = event_arg;
1212
1213         /* Note:  This whole file is a huge candidate for rework.
1214          * For example, each command could either have its own handler, so
1215          * the loop would look like:
1216          *     while (cmds)
1217          *         cmd->handle(x, y, z)
1218          * --jgrimm
1219          */
1220         while (NULL != (cmd = sctp_next_cmd(commands))) {
1221                 switch (cmd->verb) {
1222                 case SCTP_CMD_NOP:
1223                         /* Do nothing. */
1224                         break;
1225
1226                 case SCTP_CMD_NEW_ASOC:
1227                         /* Register a new association.  */
1228                         if (local_cork) {
1229                                 sctp_outq_uncork(&asoc->outqueue);
1230                                 local_cork = 0;
1231                         }
1232                         asoc = cmd->obj.ptr;
1233                         /* Register with the endpoint.  */
1234                         sctp_endpoint_add_asoc(ep, asoc);
1235                         sctp_hash_established(asoc);
1236                         break;
1237
1238                 case SCTP_CMD_UPDATE_ASSOC:
1239                        sctp_assoc_update(asoc, cmd->obj.ptr);
1240                        break;
1241
1242                 case SCTP_CMD_PURGE_OUTQUEUE:
1243                        sctp_outq_teardown(&asoc->outqueue);
1244                        break;
1245
1246                 case SCTP_CMD_DELETE_TCB:
1247                         if (local_cork) {
1248                                 sctp_outq_uncork(&asoc->outqueue);
1249                                 local_cork = 0;
1250                         }
1251                         /* Delete the current association.  */
1252                         sctp_cmd_delete_tcb(commands, asoc);
1253                         asoc = NULL;
1254                         break;
1255
1256                 case SCTP_CMD_NEW_STATE:
1257                         /* Enter a new state.  */
1258                         sctp_cmd_new_state(commands, asoc, cmd->obj.state);
1259                         break;
1260
1261                 case SCTP_CMD_REPORT_TSN:
1262                         /* Record the arrival of a TSN.  */
1263                         error = sctp_tsnmap_mark(&asoc->peer.tsn_map,
1264                                                  cmd->obj.u32);
1265                         break;
1266
1267                 case SCTP_CMD_REPORT_FWDTSN:
1268                         /* Move the Cumulattive TSN Ack ahead. */
1269                         sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
1270
1271                         /* purge the fragmentation queue */
1272                         sctp_ulpq_reasm_flushtsn(&asoc->ulpq, cmd->obj.u32);
1273
1274                         /* Abort any in progress partial delivery. */
1275                         sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
1276                         break;
1277
1278                 case SCTP_CMD_PROCESS_FWDTSN:
1279                         sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
1280                         break;
1281
1282                 case SCTP_CMD_GEN_SACK:
1283                         /* Generate a Selective ACK.
1284                          * The argument tells us whether to just count
1285                          * the packet and MAYBE generate a SACK, or
1286                          * force a SACK out.
1287                          */
1288                         force = cmd->obj.i32;
1289                         error = sctp_gen_sack(asoc, force, commands);
1290                         break;
1291
1292                 case SCTP_CMD_PROCESS_SACK:
1293                         /* Process an inbound SACK.  */
1294                         error = sctp_cmd_process_sack(commands, asoc,
1295                                                       cmd->obj.ptr);
1296                         break;
1297
1298                 case SCTP_CMD_GEN_INIT_ACK:
1299                         /* Generate an INIT ACK chunk.  */
1300                         new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
1301                                                      0);
1302                         if (!new_obj)
1303                                 goto nomem;
1304
1305                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1306                                         SCTP_CHUNK(new_obj));
1307                         break;
1308
1309                 case SCTP_CMD_PEER_INIT:
1310                         /* Process a unified INIT from the peer.
1311                          * Note: Only used during INIT-ACK processing.  If
1312                          * there is an error just return to the outter
1313                          * layer which will bail.
1314                          */
1315                         error = sctp_cmd_process_init(commands, asoc, chunk,
1316                                                       cmd->obj.ptr, gfp);
1317                         break;
1318
1319                 case SCTP_CMD_GEN_COOKIE_ECHO:
1320                         /* Generate a COOKIE ECHO chunk.  */
1321                         new_obj = sctp_make_cookie_echo(asoc, chunk);
1322                         if (!new_obj) {
1323                                 if (cmd->obj.ptr)
1324                                         sctp_chunk_free(cmd->obj.ptr);
1325                                 goto nomem;
1326                         }
1327                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1328                                         SCTP_CHUNK(new_obj));
1329
1330                         /* If there is an ERROR chunk to be sent along with
1331                          * the COOKIE_ECHO, send it, too.
1332                          */
1333                         if (cmd->obj.ptr)
1334                                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1335                                                 SCTP_CHUNK(cmd->obj.ptr));
1336
1337                         if (new_obj->transport) {
1338                                 new_obj->transport->init_sent_count++;
1339                                 asoc->init_last_sent_to = new_obj->transport;
1340                         }
1341
1342                         /* FIXME - Eventually come up with a cleaner way to
1343                          * enabling COOKIE-ECHO + DATA bundling during
1344                          * multihoming stale cookie scenarios, the following
1345                          * command plays with asoc->peer.retran_path to
1346                          * avoid the problem of sending the COOKIE-ECHO and
1347                          * DATA in different paths, which could result
1348                          * in the association being ABORTed if the DATA chunk
1349                          * is processed first by the server.  Checking the
1350                          * init error counter simply causes this command
1351                          * to be executed only during failed attempts of
1352                          * association establishment.
1353                          */
1354                         if ((asoc->peer.retran_path !=
1355                              asoc->peer.primary_path) &&
1356                             (asoc->init_err_counter > 0)) {
1357                                 sctp_add_cmd_sf(commands,
1358                                                 SCTP_CMD_FORCE_PRIM_RETRAN,
1359                                                 SCTP_NULL());
1360                         }
1361
1362                         break;
1363
1364                 case SCTP_CMD_GEN_SHUTDOWN:
1365                         /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
1366                          * Reset error counts.
1367                          */
1368                         asoc->overall_error_count = 0;
1369
1370                         /* Generate a SHUTDOWN chunk.  */
1371                         new_obj = sctp_make_shutdown(asoc, chunk);
1372                         if (!new_obj)
1373                                 goto nomem;
1374                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1375                                         SCTP_CHUNK(new_obj));
1376                         break;
1377
1378                 case SCTP_CMD_CHUNK_ULP:
1379                         /* Send a chunk to the sockets layer.  */
1380                         SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1381                                           "chunk_up:", cmd->obj.ptr,
1382                                           "ulpq:", &asoc->ulpq);
1383                         sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
1384                                             GFP_ATOMIC);
1385                         break;
1386
1387                 case SCTP_CMD_EVENT_ULP:
1388                         /* Send a notification to the sockets layer.  */
1389                         SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1390                                           "event_up:",cmd->obj.ptr,
1391                                           "ulpq:",&asoc->ulpq);
1392                         sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
1393                         break;
1394
1395                 case SCTP_CMD_REPLY:
1396                         /* If an caller has not already corked, do cork. */
1397                         if (!asoc->outqueue.cork) {
1398                                 sctp_outq_cork(&asoc->outqueue);
1399                                 local_cork = 1;
1400                         }
1401                         /* Send a chunk to our peer.  */
1402                         error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
1403                         break;
1404
1405                 case SCTP_CMD_SEND_PKT:
1406                         /* Send a full packet to our peer.  */
1407                         packet = cmd->obj.ptr;
1408                         sctp_packet_transmit(packet);
1409                         sctp_ootb_pkt_free(packet);
1410                         break;
1411
1412                 case SCTP_CMD_T1_RETRAN:
1413                         /* Mark a transport for retransmission.  */
1414                         sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1415                                         SCTP_RTXR_T1_RTX);
1416                         break;
1417
1418                 case SCTP_CMD_RETRAN:
1419                         /* Mark a transport for retransmission.  */
1420                         sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1421                                         SCTP_RTXR_T3_RTX);
1422                         break;
1423
1424                 case SCTP_CMD_ECN_CE:
1425                         /* Do delayed CE processing.   */
1426                         sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
1427                         break;
1428
1429                 case SCTP_CMD_ECN_ECNE:
1430                         /* Do delayed ECNE processing. */
1431                         new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
1432                                                         chunk);
1433                         if (new_obj)
1434                                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1435                                                 SCTP_CHUNK(new_obj));
1436                         break;
1437
1438                 case SCTP_CMD_ECN_CWR:
1439                         /* Do delayed CWR processing.  */
1440                         sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
1441                         break;
1442
1443                 case SCTP_CMD_SETUP_T2:
1444                         sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
1445                         break;
1446
1447                 case SCTP_CMD_TIMER_START_ONCE:
1448                         timer = &asoc->timers[cmd->obj.to];
1449
1450                         if (timer_pending(timer))
1451                                 break;
1452                         /* fall through */
1453
1454                 case SCTP_CMD_TIMER_START:
1455                         timer = &asoc->timers[cmd->obj.to];
1456                         timeout = asoc->timeouts[cmd->obj.to];
1457                         BUG_ON(!timeout);
1458
1459                         timer->expires = jiffies + timeout;
1460                         sctp_association_hold(asoc);
1461                         add_timer(timer);
1462                         break;
1463
1464                 case SCTP_CMD_TIMER_RESTART:
1465                         timer = &asoc->timers[cmd->obj.to];
1466                         timeout = asoc->timeouts[cmd->obj.to];
1467                         if (!mod_timer(timer, jiffies + timeout))
1468                                 sctp_association_hold(asoc);
1469                         break;
1470
1471                 case SCTP_CMD_TIMER_STOP:
1472                         timer = &asoc->timers[cmd->obj.to];
1473                         if (timer_pending(timer) && del_timer(timer))
1474                                 sctp_association_put(asoc);
1475                         break;
1476
1477                 case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
1478                         chunk = cmd->obj.ptr;
1479                         t = sctp_assoc_choose_alter_transport(asoc,
1480                                                 asoc->init_last_sent_to);
1481                         asoc->init_last_sent_to = t;
1482                         chunk->transport = t;
1483                         t->init_sent_count++;
1484                         /* Set the new transport as primary */
1485                         sctp_assoc_set_primary(asoc, t);
1486                         break;
1487
1488                 case SCTP_CMD_INIT_RESTART:
1489                         /* Do the needed accounting and updates
1490                          * associated with restarting an initialization
1491                          * timer. Only multiply the timeout by two if
1492                          * all transports have been tried at the current
1493                          * timeout.
1494                          */
1495                         sctp_cmd_t1_timer_update(asoc,
1496                                                 SCTP_EVENT_TIMEOUT_T1_INIT,
1497                                                 "INIT");
1498
1499                         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
1500                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
1501                         break;
1502
1503                 case SCTP_CMD_COOKIEECHO_RESTART:
1504                         /* Do the needed accounting and updates
1505                          * associated with restarting an initialization
1506                          * timer. Only multiply the timeout by two if
1507                          * all transports have been tried at the current
1508                          * timeout.
1509                          */
1510                         sctp_cmd_t1_timer_update(asoc,
1511                                                 SCTP_EVENT_TIMEOUT_T1_COOKIE,
1512                                                 "COOKIE");
1513
1514                         /* If we've sent any data bundled with
1515                          * COOKIE-ECHO we need to resend.
1516                          */
1517                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
1518                                         transports) {
1519                                 sctp_retransmit_mark(&asoc->outqueue, t,
1520                                             SCTP_RTXR_T1_RTX);
1521                         }
1522
1523                         sctp_add_cmd_sf(commands,
1524                                         SCTP_CMD_TIMER_RESTART,
1525                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1526                         break;
1527
1528                 case SCTP_CMD_INIT_FAILED:
1529                         sctp_cmd_init_failed(commands, asoc, cmd->obj.err);
1530                         break;
1531
1532                 case SCTP_CMD_ASSOC_FAILED:
1533                         sctp_cmd_assoc_failed(commands, asoc, event_type,
1534                                               subtype, chunk, cmd->obj.err);
1535                         break;
1536
1537                 case SCTP_CMD_INIT_COUNTER_INC:
1538                         asoc->init_err_counter++;
1539                         break;
1540
1541                 case SCTP_CMD_INIT_COUNTER_RESET:
1542                         asoc->init_err_counter = 0;
1543                         asoc->init_cycle = 0;
1544                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
1545                                             transports) {
1546                                 t->init_sent_count = 0;
1547                         }
1548                         break;
1549
1550                 case SCTP_CMD_REPORT_DUP:
1551                         sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
1552                                              cmd->obj.u32);
1553                         break;
1554
1555                 case SCTP_CMD_REPORT_BAD_TAG:
1556                         SCTP_DEBUG_PRINTK("vtag mismatch!\n");
1557                         break;
1558
1559                 case SCTP_CMD_STRIKE:
1560                         /* Mark one strike against a transport.  */
1561                         sctp_do_8_2_transport_strike(asoc, cmd->obj.transport,
1562                                                     0);
1563                         break;
1564
1565                 case SCTP_CMD_TRANSPORT_IDLE:
1566                         t = cmd->obj.transport;
1567                         sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
1568                         break;
1569
1570                 case SCTP_CMD_TRANSPORT_HB_SENT:
1571                         t = cmd->obj.transport;
1572                         sctp_do_8_2_transport_strike(asoc, t, 1);
1573                         t->hb_sent = 1;
1574                         break;
1575
1576                 case SCTP_CMD_TRANSPORT_ON:
1577                         t = cmd->obj.transport;
1578                         sctp_cmd_transport_on(commands, asoc, t, chunk);
1579                         break;
1580
1581                 case SCTP_CMD_HB_TIMERS_START:
1582                         sctp_cmd_hb_timers_start(commands, asoc);
1583                         break;
1584
1585                 case SCTP_CMD_HB_TIMER_UPDATE:
1586                         t = cmd->obj.transport;
1587                         sctp_cmd_hb_timer_update(commands, t);
1588                         break;
1589
1590                 case SCTP_CMD_HB_TIMERS_STOP:
1591                         sctp_cmd_hb_timers_stop(commands, asoc);
1592                         break;
1593
1594                 case SCTP_CMD_REPORT_ERROR:
1595                         error = cmd->obj.error;
1596                         break;
1597
1598                 case SCTP_CMD_PROCESS_CTSN:
1599                         /* Dummy up a SACK for processing. */
1600                         sackh.cum_tsn_ack = cmd->obj.be32;
1601                         sackh.a_rwnd = htonl(asoc->peer.rwnd +
1602                                              asoc->outqueue.outstanding_bytes);
1603                         sackh.num_gap_ack_blocks = 0;
1604                         sackh.num_dup_tsns = 0;
1605                         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
1606                                         SCTP_SACKH(&sackh));
1607                         break;
1608
1609                 case SCTP_CMD_DISCARD_PACKET:
1610                         /* We need to discard the whole packet.
1611                          * Uncork the queue since there might be
1612                          * responses pending
1613                          */
1614                         chunk->pdiscard = 1;
1615                         if (asoc) {
1616                                 sctp_outq_uncork(&asoc->outqueue);
1617                                 local_cork = 0;
1618                         }
1619                         break;
1620
1621                 case SCTP_CMD_RTO_PENDING:
1622                         t = cmd->obj.transport;
1623                         t->rto_pending = 1;
1624                         break;
1625
1626                 case SCTP_CMD_PART_DELIVER:
1627                         sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
1628                                                    GFP_ATOMIC);
1629                         break;
1630
1631                 case SCTP_CMD_RENEGE:
1632                         sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
1633                                          GFP_ATOMIC);
1634                         break;
1635
1636                 case SCTP_CMD_SETUP_T4:
1637                         sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
1638                         break;
1639
1640                 case SCTP_CMD_PROCESS_OPERR:
1641                         sctp_cmd_process_operr(commands, asoc, chunk);
1642                         break;
1643                 case SCTP_CMD_CLEAR_INIT_TAG:
1644                         asoc->peer.i.init_tag = 0;
1645                         break;
1646                 case SCTP_CMD_DEL_NON_PRIMARY:
1647                         sctp_cmd_del_non_primary(asoc);
1648                         break;
1649                 case SCTP_CMD_T3_RTX_TIMERS_STOP:
1650                         sctp_cmd_t3_rtx_timers_stop(commands, asoc);
1651                         break;
1652                 case SCTP_CMD_FORCE_PRIM_RETRAN:
1653                         t = asoc->peer.retran_path;
1654                         asoc->peer.retran_path = asoc->peer.primary_path;
1655                         error = sctp_outq_uncork(&asoc->outqueue);
1656                         local_cork = 0;
1657                         asoc->peer.retran_path = t;
1658                         break;
1659                 case SCTP_CMD_SET_SK_ERR:
1660                         sctp_cmd_set_sk_err(asoc, cmd->obj.error);
1661                         break;
1662                 case SCTP_CMD_ASSOC_CHANGE:
1663                         sctp_cmd_assoc_change(commands, asoc,
1664                                               cmd->obj.u8);
1665                         break;
1666                 case SCTP_CMD_ADAPTATION_IND:
1667                         sctp_cmd_adaptation_ind(commands, asoc);
1668                         break;
1669
1670                 case SCTP_CMD_ASSOC_SHKEY:
1671                         error = sctp_auth_asoc_init_active_key(asoc,
1672                                                 GFP_ATOMIC);
1673                         break;
1674                 case SCTP_CMD_UPDATE_INITTAG:
1675                         asoc->peer.i.init_tag = cmd->obj.u32;
1676                         break;
1677                 case SCTP_CMD_SEND_MSG:
1678                         if (!asoc->outqueue.cork) {
1679                                 sctp_outq_cork(&asoc->outqueue);
1680                                 local_cork = 1;
1681                         }
1682                         sctp_cmd_send_msg(asoc, cmd->obj.msg);
1683                         break;
1684                 case SCTP_CMD_SEND_NEXT_ASCONF:
1685                         sctp_cmd_send_asconf(asoc);
1686                         break;
1687                 case SCTP_CMD_PURGE_ASCONF_QUEUE:
1688                         sctp_asconf_queue_teardown(asoc);
1689                         break;
1690
1691                 case SCTP_CMD_SET_ASOC:
1692                         asoc = cmd->obj.asoc;
1693                         break;
1694
1695                 default:
1696                         pr_warn("Impossible command: %u, %p\n",
1697                                 cmd->verb, cmd->obj.ptr);
1698                         break;
1699                 }
1700
1701                 if (error)
1702                         break;
1703         }
1704
1705 out:
1706         /* If this is in response to a received chunk, wait until
1707          * we are done with the packet to open the queue so that we don't
1708          * send multiple packets in response to a single request.
1709          */
1710         if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) {
1711                 if (chunk->end_of_packet || chunk->singleton)
1712                         error = sctp_outq_uncork(&asoc->outqueue);
1713         } else if (local_cork)
1714                 error = sctp_outq_uncork(&asoc->outqueue);
1715         return error;
1716 nomem:
1717         error = -ENOMEM;
1718         goto out;
1719 }
1720