5437b33fd33e6386143a6e5c7c1f2ea8d3653155
[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 int sctp_cmd_send_msg(struct sctp_association *asoc,
999                                 struct sctp_datamsg *msg)
1000 {
1001         struct sctp_chunk *chunk;
1002         int error = 0;
1003
1004         list_for_each_entry(chunk, &msg->chunks, frag_list) {
1005                 error = sctp_outq_tail(&asoc->outqueue, chunk);
1006                 if (error)
1007                         break;
1008         }
1009
1010         return error;
1011 }
1012
1013
1014 /* Sent the next ASCONF packet currently stored in the association.
1015  * This happens after the ASCONF_ACK was succeffully processed.
1016  */
1017 static void sctp_cmd_send_asconf(struct sctp_association *asoc)
1018 {
1019         /* Send the next asconf chunk from the addip chunk
1020          * queue.
1021          */
1022         if (!list_empty(&asoc->addip_chunk_list)) {
1023                 struct list_head *entry = asoc->addip_chunk_list.next;
1024                 struct sctp_chunk *asconf = list_entry(entry,
1025                                                 struct sctp_chunk, list);
1026                 list_del_init(entry);
1027
1028                 /* Hold the chunk until an ASCONF_ACK is received. */
1029                 sctp_chunk_hold(asconf);
1030                 if (sctp_primitive_ASCONF(asoc, asconf))
1031                         sctp_chunk_free(asconf);
1032                 else
1033                         asoc->addip_last_asconf = asconf;
1034         }
1035 }
1036
1037
1038 /* These three macros allow us to pull the debugging code out of the
1039  * main flow of sctp_do_sm() to keep attention focused on the real
1040  * functionality there.
1041  */
1042 #define DEBUG_PRE \
1043         SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
1044                           "ep %p, %s, %s, asoc %p[%s], %s\n", \
1045                           ep, sctp_evttype_tbl[event_type], \
1046                           (*debug_fn)(subtype), asoc, \
1047                           sctp_state_tbl[state], state_fn->name)
1048
1049 #define DEBUG_POST \
1050         SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
1051                           "asoc %p, status: %s\n", \
1052                           asoc, sctp_status_tbl[status])
1053
1054 #define DEBUG_POST_SFX \
1055         SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
1056                           error, asoc, \
1057                           sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
1058                           sctp_assoc2id(asoc)))?asoc->state:SCTP_STATE_CLOSED])
1059
1060 /*
1061  * This is the master state machine processing function.
1062  *
1063  * If you want to understand all of lksctp, this is a
1064  * good place to start.
1065  */
1066 int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
1067                sctp_state_t state,
1068                struct sctp_endpoint *ep,
1069                struct sctp_association *asoc,
1070                void *event_arg,
1071                gfp_t gfp)
1072 {
1073         sctp_cmd_seq_t commands;
1074         const sctp_sm_table_entry_t *state_fn;
1075         sctp_disposition_t status;
1076         int error = 0;
1077         typedef const char *(printfn_t)(sctp_subtype_t);
1078
1079         static printfn_t *table[] = {
1080                 NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
1081         };
1082         printfn_t *debug_fn  __attribute__ ((unused)) = table[event_type];
1083
1084         /* Look up the state function, run it, and then process the
1085          * side effects.  These three steps are the heart of lksctp.
1086          */
1087         state_fn = sctp_sm_lookup_event(event_type, state, subtype);
1088
1089         sctp_init_cmd_seq(&commands);
1090
1091         DEBUG_PRE;
1092         status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
1093         DEBUG_POST;
1094
1095         error = sctp_side_effects(event_type, subtype, state,
1096                                   ep, asoc, event_arg, status,
1097                                   &commands, gfp);
1098         DEBUG_POST_SFX;
1099
1100         return error;
1101 }
1102
1103 #undef DEBUG_PRE
1104 #undef DEBUG_POST
1105
1106 /*****************************************************************
1107  * This the master state function side effect processing function.
1108  *****************************************************************/
1109 static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
1110                              sctp_state_t state,
1111                              struct sctp_endpoint *ep,
1112                              struct sctp_association *asoc,
1113                              void *event_arg,
1114                              sctp_disposition_t status,
1115                              sctp_cmd_seq_t *commands,
1116                              gfp_t gfp)
1117 {
1118         int error;
1119
1120         /* FIXME - Most of the dispositions left today would be categorized
1121          * as "exceptional" dispositions.  For those dispositions, it
1122          * may not be proper to run through any of the commands at all.
1123          * For example, the command interpreter might be run only with
1124          * disposition SCTP_DISPOSITION_CONSUME.
1125          */
1126         if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
1127                                                ep, asoc,
1128                                                event_arg, status,
1129                                                commands, gfp)))
1130                 goto bail;
1131
1132         switch (status) {
1133         case SCTP_DISPOSITION_DISCARD:
1134                 SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
1135                                   "event_type %d, event_id %d\n",
1136                                   state, event_type, subtype.chunk);
1137                 break;
1138
1139         case SCTP_DISPOSITION_NOMEM:
1140                 /* We ran out of memory, so we need to discard this
1141                  * packet.
1142                  */
1143                 /* BUG--we should now recover some memory, probably by
1144                  * reneging...
1145                  */
1146                 error = -ENOMEM;
1147                 break;
1148
1149         case SCTP_DISPOSITION_DELETE_TCB:
1150                 /* This should now be a command. */
1151                 break;
1152
1153         case SCTP_DISPOSITION_CONSUME:
1154         case SCTP_DISPOSITION_ABORT:
1155                 /*
1156                  * We should no longer have much work to do here as the
1157                  * real work has been done as explicit commands above.
1158                  */
1159                 break;
1160
1161         case SCTP_DISPOSITION_VIOLATION:
1162                 if (net_ratelimit())
1163                         pr_err("protocol violation state %d chunkid %d\n",
1164                                state, subtype.chunk);
1165                 break;
1166
1167         case SCTP_DISPOSITION_NOT_IMPL:
1168                 pr_warn("unimplemented feature in state %d, event_type %d, event_id %d\n",
1169                         state, event_type, subtype.chunk);
1170                 break;
1171
1172         case SCTP_DISPOSITION_BUG:
1173                 pr_err("bug in state %d, event_type %d, event_id %d\n",
1174                        state, event_type, subtype.chunk);
1175                 BUG();
1176                 break;
1177
1178         default:
1179                 pr_err("impossible disposition %d in state %d, event_type %d, event_id %d\n",
1180                        status, state, event_type, subtype.chunk);
1181                 BUG();
1182                 break;
1183         }
1184
1185 bail:
1186         return error;
1187 }
1188
1189 /********************************************************************
1190  * 2nd Level Abstractions
1191  ********************************************************************/
1192
1193 /* This is the side-effect interpreter.  */
1194 static int sctp_cmd_interpreter(sctp_event_t event_type,
1195                                 sctp_subtype_t subtype,
1196                                 sctp_state_t state,
1197                                 struct sctp_endpoint *ep,
1198                                 struct sctp_association *asoc,
1199                                 void *event_arg,
1200                                 sctp_disposition_t status,
1201                                 sctp_cmd_seq_t *commands,
1202                                 gfp_t gfp)
1203 {
1204         int error = 0;
1205         int force;
1206         sctp_cmd_t *cmd;
1207         struct sctp_chunk *new_obj;
1208         struct sctp_chunk *chunk = NULL;
1209         struct sctp_packet *packet;
1210         struct timer_list *timer;
1211         unsigned long timeout;
1212         struct sctp_transport *t;
1213         struct sctp_sackhdr sackh;
1214         int local_cork = 0;
1215
1216         if (SCTP_EVENT_T_TIMEOUT != event_type)
1217                 chunk = event_arg;
1218
1219         /* Note:  This whole file is a huge candidate for rework.
1220          * For example, each command could either have its own handler, so
1221          * the loop would look like:
1222          *     while (cmds)
1223          *         cmd->handle(x, y, z)
1224          * --jgrimm
1225          */
1226         while (NULL != (cmd = sctp_next_cmd(commands))) {
1227                 switch (cmd->verb) {
1228                 case SCTP_CMD_NOP:
1229                         /* Do nothing. */
1230                         break;
1231
1232                 case SCTP_CMD_NEW_ASOC:
1233                         /* Register a new association.  */
1234                         if (local_cork) {
1235                                 sctp_outq_uncork(&asoc->outqueue);
1236                                 local_cork = 0;
1237                         }
1238                         asoc = cmd->obj.ptr;
1239                         /* Register with the endpoint.  */
1240                         sctp_endpoint_add_asoc(ep, asoc);
1241                         sctp_hash_established(asoc);
1242                         break;
1243
1244                 case SCTP_CMD_UPDATE_ASSOC:
1245                        sctp_assoc_update(asoc, cmd->obj.ptr);
1246                        break;
1247
1248                 case SCTP_CMD_PURGE_OUTQUEUE:
1249                        sctp_outq_teardown(&asoc->outqueue);
1250                        break;
1251
1252                 case SCTP_CMD_DELETE_TCB:
1253                         if (local_cork) {
1254                                 sctp_outq_uncork(&asoc->outqueue);
1255                                 local_cork = 0;
1256                         }
1257                         /* Delete the current association.  */
1258                         sctp_cmd_delete_tcb(commands, asoc);
1259                         asoc = NULL;
1260                         break;
1261
1262                 case SCTP_CMD_NEW_STATE:
1263                         /* Enter a new state.  */
1264                         sctp_cmd_new_state(commands, asoc, cmd->obj.state);
1265                         break;
1266
1267                 case SCTP_CMD_REPORT_TSN:
1268                         /* Record the arrival of a TSN.  */
1269                         error = sctp_tsnmap_mark(&asoc->peer.tsn_map,
1270                                                  cmd->obj.u32);
1271                         break;
1272
1273                 case SCTP_CMD_REPORT_FWDTSN:
1274                         /* Move the Cumulattive TSN Ack ahead. */
1275                         sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
1276
1277                         /* purge the fragmentation queue */
1278                         sctp_ulpq_reasm_flushtsn(&asoc->ulpq, cmd->obj.u32);
1279
1280                         /* Abort any in progress partial delivery. */
1281                         sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
1282                         break;
1283
1284                 case SCTP_CMD_PROCESS_FWDTSN:
1285                         sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
1286                         break;
1287
1288                 case SCTP_CMD_GEN_SACK:
1289                         /* Generate a Selective ACK.
1290                          * The argument tells us whether to just count
1291                          * the packet and MAYBE generate a SACK, or
1292                          * force a SACK out.
1293                          */
1294                         force = cmd->obj.i32;
1295                         error = sctp_gen_sack(asoc, force, commands);
1296                         break;
1297
1298                 case SCTP_CMD_PROCESS_SACK:
1299                         /* Process an inbound SACK.  */
1300                         error = sctp_cmd_process_sack(commands, asoc,
1301                                                       cmd->obj.ptr);
1302                         break;
1303
1304                 case SCTP_CMD_GEN_INIT_ACK:
1305                         /* Generate an INIT ACK chunk.  */
1306                         new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
1307                                                      0);
1308                         if (!new_obj)
1309                                 goto nomem;
1310
1311                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1312                                         SCTP_CHUNK(new_obj));
1313                         break;
1314
1315                 case SCTP_CMD_PEER_INIT:
1316                         /* Process a unified INIT from the peer.
1317                          * Note: Only used during INIT-ACK processing.  If
1318                          * there is an error just return to the outter
1319                          * layer which will bail.
1320                          */
1321                         error = sctp_cmd_process_init(commands, asoc, chunk,
1322                                                       cmd->obj.ptr, gfp);
1323                         break;
1324
1325                 case SCTP_CMD_GEN_COOKIE_ECHO:
1326                         /* Generate a COOKIE ECHO chunk.  */
1327                         new_obj = sctp_make_cookie_echo(asoc, chunk);
1328                         if (!new_obj) {
1329                                 if (cmd->obj.ptr)
1330                                         sctp_chunk_free(cmd->obj.ptr);
1331                                 goto nomem;
1332                         }
1333                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1334                                         SCTP_CHUNK(new_obj));
1335
1336                         /* If there is an ERROR chunk to be sent along with
1337                          * the COOKIE_ECHO, send it, too.
1338                          */
1339                         if (cmd->obj.ptr)
1340                                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1341                                                 SCTP_CHUNK(cmd->obj.ptr));
1342
1343                         if (new_obj->transport) {
1344                                 new_obj->transport->init_sent_count++;
1345                                 asoc->init_last_sent_to = new_obj->transport;
1346                         }
1347
1348                         /* FIXME - Eventually come up with a cleaner way to
1349                          * enabling COOKIE-ECHO + DATA bundling during
1350                          * multihoming stale cookie scenarios, the following
1351                          * command plays with asoc->peer.retran_path to
1352                          * avoid the problem of sending the COOKIE-ECHO and
1353                          * DATA in different paths, which could result
1354                          * in the association being ABORTed if the DATA chunk
1355                          * is processed first by the server.  Checking the
1356                          * init error counter simply causes this command
1357                          * to be executed only during failed attempts of
1358                          * association establishment.
1359                          */
1360                         if ((asoc->peer.retran_path !=
1361                              asoc->peer.primary_path) &&
1362                             (asoc->init_err_counter > 0)) {
1363                                 sctp_add_cmd_sf(commands,
1364                                                 SCTP_CMD_FORCE_PRIM_RETRAN,
1365                                                 SCTP_NULL());
1366                         }
1367
1368                         break;
1369
1370                 case SCTP_CMD_GEN_SHUTDOWN:
1371                         /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
1372                          * Reset error counts.
1373                          */
1374                         asoc->overall_error_count = 0;
1375
1376                         /* Generate a SHUTDOWN chunk.  */
1377                         new_obj = sctp_make_shutdown(asoc, chunk);
1378                         if (!new_obj)
1379                                 goto nomem;
1380                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1381                                         SCTP_CHUNK(new_obj));
1382                         break;
1383
1384                 case SCTP_CMD_CHUNK_ULP:
1385                         /* Send a chunk to the sockets layer.  */
1386                         SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1387                                           "chunk_up:", cmd->obj.ptr,
1388                                           "ulpq:", &asoc->ulpq);
1389                         sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
1390                                             GFP_ATOMIC);
1391                         break;
1392
1393                 case SCTP_CMD_EVENT_ULP:
1394                         /* Send a notification to the sockets layer.  */
1395                         SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1396                                           "event_up:",cmd->obj.ptr,
1397                                           "ulpq:",&asoc->ulpq);
1398                         sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
1399                         break;
1400
1401                 case SCTP_CMD_REPLY:
1402                         /* If an caller has not already corked, do cork. */
1403                         if (!asoc->outqueue.cork) {
1404                                 sctp_outq_cork(&asoc->outqueue);
1405                                 local_cork = 1;
1406                         }
1407                         /* Send a chunk to our peer.  */
1408                         error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
1409                         break;
1410
1411                 case SCTP_CMD_SEND_PKT:
1412                         /* Send a full packet to our peer.  */
1413                         packet = cmd->obj.ptr;
1414                         sctp_packet_transmit(packet);
1415                         sctp_ootb_pkt_free(packet);
1416                         break;
1417
1418                 case SCTP_CMD_T1_RETRAN:
1419                         /* Mark a transport for retransmission.  */
1420                         sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1421                                         SCTP_RTXR_T1_RTX);
1422                         break;
1423
1424                 case SCTP_CMD_RETRAN:
1425                         /* Mark a transport for retransmission.  */
1426                         sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1427                                         SCTP_RTXR_T3_RTX);
1428                         break;
1429
1430                 case SCTP_CMD_ECN_CE:
1431                         /* Do delayed CE processing.   */
1432                         sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
1433                         break;
1434
1435                 case SCTP_CMD_ECN_ECNE:
1436                         /* Do delayed ECNE processing. */
1437                         new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
1438                                                         chunk);
1439                         if (new_obj)
1440                                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1441                                                 SCTP_CHUNK(new_obj));
1442                         break;
1443
1444                 case SCTP_CMD_ECN_CWR:
1445                         /* Do delayed CWR processing.  */
1446                         sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
1447                         break;
1448
1449                 case SCTP_CMD_SETUP_T2:
1450                         sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
1451                         break;
1452
1453                 case SCTP_CMD_TIMER_START_ONCE:
1454                         timer = &asoc->timers[cmd->obj.to];
1455
1456                         if (timer_pending(timer))
1457                                 break;
1458                         /* fall through */
1459
1460                 case SCTP_CMD_TIMER_START:
1461                         timer = &asoc->timers[cmd->obj.to];
1462                         timeout = asoc->timeouts[cmd->obj.to];
1463                         BUG_ON(!timeout);
1464
1465                         timer->expires = jiffies + timeout;
1466                         sctp_association_hold(asoc);
1467                         add_timer(timer);
1468                         break;
1469
1470                 case SCTP_CMD_TIMER_RESTART:
1471                         timer = &asoc->timers[cmd->obj.to];
1472                         timeout = asoc->timeouts[cmd->obj.to];
1473                         if (!mod_timer(timer, jiffies + timeout))
1474                                 sctp_association_hold(asoc);
1475                         break;
1476
1477                 case SCTP_CMD_TIMER_STOP:
1478                         timer = &asoc->timers[cmd->obj.to];
1479                         if (timer_pending(timer) && del_timer(timer))
1480                                 sctp_association_put(asoc);
1481                         break;
1482
1483                 case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
1484                         chunk = cmd->obj.ptr;
1485                         t = sctp_assoc_choose_alter_transport(asoc,
1486                                                 asoc->init_last_sent_to);
1487                         asoc->init_last_sent_to = t;
1488                         chunk->transport = t;
1489                         t->init_sent_count++;
1490                         /* Set the new transport as primary */
1491                         sctp_assoc_set_primary(asoc, t);
1492                         break;
1493
1494                 case SCTP_CMD_INIT_RESTART:
1495                         /* Do the needed accounting and updates
1496                          * associated with restarting an initialization
1497                          * timer. Only multiply the timeout by two if
1498                          * all transports have been tried at the current
1499                          * timeout.
1500                          */
1501                         sctp_cmd_t1_timer_update(asoc,
1502                                                 SCTP_EVENT_TIMEOUT_T1_INIT,
1503                                                 "INIT");
1504
1505                         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
1506                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
1507                         break;
1508
1509                 case SCTP_CMD_COOKIEECHO_RESTART:
1510                         /* Do the needed accounting and updates
1511                          * associated with restarting an initialization
1512                          * timer. Only multiply the timeout by two if
1513                          * all transports have been tried at the current
1514                          * timeout.
1515                          */
1516                         sctp_cmd_t1_timer_update(asoc,
1517                                                 SCTP_EVENT_TIMEOUT_T1_COOKIE,
1518                                                 "COOKIE");
1519
1520                         /* If we've sent any data bundled with
1521                          * COOKIE-ECHO we need to resend.
1522                          */
1523                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
1524                                         transports) {
1525                                 sctp_retransmit_mark(&asoc->outqueue, t,
1526                                             SCTP_RTXR_T1_RTX);
1527                         }
1528
1529                         sctp_add_cmd_sf(commands,
1530                                         SCTP_CMD_TIMER_RESTART,
1531                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1532                         break;
1533
1534                 case SCTP_CMD_INIT_FAILED:
1535                         sctp_cmd_init_failed(commands, asoc, cmd->obj.err);
1536                         break;
1537
1538                 case SCTP_CMD_ASSOC_FAILED:
1539                         sctp_cmd_assoc_failed(commands, asoc, event_type,
1540                                               subtype, chunk, cmd->obj.err);
1541                         break;
1542
1543                 case SCTP_CMD_INIT_COUNTER_INC:
1544                         asoc->init_err_counter++;
1545                         break;
1546
1547                 case SCTP_CMD_INIT_COUNTER_RESET:
1548                         asoc->init_err_counter = 0;
1549                         asoc->init_cycle = 0;
1550                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
1551                                             transports) {
1552                                 t->init_sent_count = 0;
1553                         }
1554                         break;
1555
1556                 case SCTP_CMD_REPORT_DUP:
1557                         sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
1558                                              cmd->obj.u32);
1559                         break;
1560
1561                 case SCTP_CMD_REPORT_BAD_TAG:
1562                         SCTP_DEBUG_PRINTK("vtag mismatch!\n");
1563                         break;
1564
1565                 case SCTP_CMD_STRIKE:
1566                         /* Mark one strike against a transport.  */
1567                         sctp_do_8_2_transport_strike(asoc, cmd->obj.transport,
1568                                                     0);
1569                         break;
1570
1571                 case SCTP_CMD_TRANSPORT_IDLE:
1572                         t = cmd->obj.transport;
1573                         sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
1574                         break;
1575
1576                 case SCTP_CMD_TRANSPORT_HB_SENT:
1577                         t = cmd->obj.transport;
1578                         sctp_do_8_2_transport_strike(asoc, t, 1);
1579                         t->hb_sent = 1;
1580                         break;
1581
1582                 case SCTP_CMD_TRANSPORT_ON:
1583                         t = cmd->obj.transport;
1584                         sctp_cmd_transport_on(commands, asoc, t, chunk);
1585                         break;
1586
1587                 case SCTP_CMD_HB_TIMERS_START:
1588                         sctp_cmd_hb_timers_start(commands, asoc);
1589                         break;
1590
1591                 case SCTP_CMD_HB_TIMER_UPDATE:
1592                         t = cmd->obj.transport;
1593                         sctp_cmd_hb_timer_update(commands, t);
1594                         break;
1595
1596                 case SCTP_CMD_HB_TIMERS_STOP:
1597                         sctp_cmd_hb_timers_stop(commands, asoc);
1598                         break;
1599
1600                 case SCTP_CMD_REPORT_ERROR:
1601                         error = cmd->obj.error;
1602                         break;
1603
1604                 case SCTP_CMD_PROCESS_CTSN:
1605                         /* Dummy up a SACK for processing. */
1606                         sackh.cum_tsn_ack = cmd->obj.be32;
1607                         sackh.a_rwnd = asoc->peer.rwnd +
1608                                         asoc->outqueue.outstanding_bytes;
1609                         sackh.num_gap_ack_blocks = 0;
1610                         sackh.num_dup_tsns = 0;
1611                         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
1612                                         SCTP_SACKH(&sackh));
1613                         break;
1614
1615                 case SCTP_CMD_DISCARD_PACKET:
1616                         /* We need to discard the whole packet.
1617                          * Uncork the queue since there might be
1618                          * responses pending
1619                          */
1620                         chunk->pdiscard = 1;
1621                         if (asoc) {
1622                                 sctp_outq_uncork(&asoc->outqueue);
1623                                 local_cork = 0;
1624                         }
1625                         break;
1626
1627                 case SCTP_CMD_RTO_PENDING:
1628                         t = cmd->obj.transport;
1629                         t->rto_pending = 1;
1630                         break;
1631
1632                 case SCTP_CMD_PART_DELIVER:
1633                         sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
1634                                                    GFP_ATOMIC);
1635                         break;
1636
1637                 case SCTP_CMD_RENEGE:
1638                         sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
1639                                          GFP_ATOMIC);
1640                         break;
1641
1642                 case SCTP_CMD_SETUP_T4:
1643                         sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
1644                         break;
1645
1646                 case SCTP_CMD_PROCESS_OPERR:
1647                         sctp_cmd_process_operr(commands, asoc, chunk);
1648                         break;
1649                 case SCTP_CMD_CLEAR_INIT_TAG:
1650                         asoc->peer.i.init_tag = 0;
1651                         break;
1652                 case SCTP_CMD_DEL_NON_PRIMARY:
1653                         sctp_cmd_del_non_primary(asoc);
1654                         break;
1655                 case SCTP_CMD_T3_RTX_TIMERS_STOP:
1656                         sctp_cmd_t3_rtx_timers_stop(commands, asoc);
1657                         break;
1658                 case SCTP_CMD_FORCE_PRIM_RETRAN:
1659                         t = asoc->peer.retran_path;
1660                         asoc->peer.retran_path = asoc->peer.primary_path;
1661                         error = sctp_outq_uncork(&asoc->outqueue);
1662                         local_cork = 0;
1663                         asoc->peer.retran_path = t;
1664                         break;
1665                 case SCTP_CMD_SET_SK_ERR:
1666                         sctp_cmd_set_sk_err(asoc, cmd->obj.error);
1667                         break;
1668                 case SCTP_CMD_ASSOC_CHANGE:
1669                         sctp_cmd_assoc_change(commands, asoc,
1670                                               cmd->obj.u8);
1671                         break;
1672                 case SCTP_CMD_ADAPTATION_IND:
1673                         sctp_cmd_adaptation_ind(commands, asoc);
1674                         break;
1675
1676                 case SCTP_CMD_ASSOC_SHKEY:
1677                         error = sctp_auth_asoc_init_active_key(asoc,
1678                                                 GFP_ATOMIC);
1679                         break;
1680                 case SCTP_CMD_UPDATE_INITTAG:
1681                         asoc->peer.i.init_tag = cmd->obj.u32;
1682                         break;
1683                 case SCTP_CMD_SEND_MSG:
1684                         if (!asoc->outqueue.cork) {
1685                                 sctp_outq_cork(&asoc->outqueue);
1686                                 local_cork = 1;
1687                         }
1688                         error = sctp_cmd_send_msg(asoc, cmd->obj.msg);
1689                         break;
1690                 case SCTP_CMD_SEND_NEXT_ASCONF:
1691                         sctp_cmd_send_asconf(asoc);
1692                         break;
1693                 case SCTP_CMD_PURGE_ASCONF_QUEUE:
1694                         sctp_asconf_queue_teardown(asoc);
1695                         break;
1696
1697                 case SCTP_CMD_SET_ASOC:
1698                         asoc = cmd->obj.asoc;
1699                         break;
1700
1701                 default:
1702                         pr_warn("Impossible command: %u, %p\n",
1703                                 cmd->verb, cmd->obj.ptr);
1704                         break;
1705                 }
1706
1707                 if (error)
1708                         break;
1709         }
1710
1711 out:
1712         /* If this is in response to a received chunk, wait until
1713          * we are done with the packet to open the queue so that we don't
1714          * send multiple packets in response to a single request.
1715          */
1716         if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) {
1717                 if (chunk->end_of_packet || chunk->singleton)
1718                         error = sctp_outq_uncork(&asoc->outqueue);
1719         } else if (local_cork)
1720                 error = sctp_outq_uncork(&asoc->outqueue);
1721         return error;
1722 nomem:
1723         error = -ENOMEM;
1724         goto out;
1725 }
1726