blob: 49fb9acece63c561a82a8bd3f2135edf9f14e511 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05006 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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#include <linux/skbuff.h>
51#include <linux/types.h>
52#include <linux/socket.h>
53#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/sctp/sctp.h>
57#include <net/sctp/sm.h>
58
59static int sctp_cmd_interpreter(sctp_event_t event_type,
60 sctp_subtype_t subtype,
61 sctp_state_t state,
62 struct sctp_endpoint *ep,
63 struct sctp_association *asoc,
64 void *event_arg,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090065 sctp_disposition_t status,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 sctp_cmd_seq_t *commands,
Al Virodd0fc662005-10-07 07:46:04 +010067 gfp_t gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
69 sctp_state_t state,
70 struct sctp_endpoint *ep,
71 struct sctp_association *asoc,
72 void *event_arg,
73 sctp_disposition_t status,
74 sctp_cmd_seq_t *commands,
Al Virodd0fc662005-10-07 07:46:04 +010075 gfp_t gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/********************************************************************
78 * Helper functions
79 ********************************************************************/
80
81/* A helper function for delayed processing of INET ECN CE bit. */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090082static void sctp_do_ecn_ce_work(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 __u32 lowest_tsn)
84{
85 /* Save the TSN away for comparison when we receive CWR */
86
87 asoc->last_ecne_tsn = lowest_tsn;
88 asoc->need_ecne = 1;
89}
90
91/* Helper function for delayed processing of SCTP ECNE chunk. */
92/* RFC 2960 Appendix A
93 *
94 * RFC 2481 details a specific bit for a sender to send in
95 * the header of its next outbound TCP segment to indicate to
96 * its peer that it has reduced its congestion window. This
97 * is termed the CWR bit. For SCTP the same indication is made
98 * by including the CWR chunk. This chunk contains one data
99 * element, i.e. the TSN number that was sent in the ECNE chunk.
100 * This element represents the lowest TSN number in the datagram
101 * that was originally marked with the CE bit.
102 */
103static struct sctp_chunk *sctp_do_ecn_ecne_work(struct sctp_association *asoc,
104 __u32 lowest_tsn,
105 struct sctp_chunk *chunk)
106{
107 struct sctp_chunk *repl;
108
109 /* Our previously transmitted packet ran into some congestion
110 * so we should take action by reducing cwnd and ssthresh
111 * and then ACK our peer that we we've done so by
112 * sending a CWR.
113 */
114
115 /* First, try to determine if we want to actually lower
116 * our cwnd variables. Only lower them if the ECNE looks more
117 * recent than the last response.
118 */
119 if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) {
120 struct sctp_transport *transport;
121
122 /* Find which transport's congestion variables
123 * need to be adjusted.
124 */
125 transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn);
126
127 /* Update the congestion variables. */
128 if (transport)
129 sctp_transport_lower_cwnd(transport,
130 SCTP_LOWER_CWND_ECNE);
131 asoc->last_cwr_tsn = lowest_tsn;
132 }
133
134 /* Always try to quiet the other end. In case of lost CWR,
135 * resend last_cwr_tsn.
136 */
137 repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk);
138
139 /* If we run out of memory, it will look like a lost CWR. We'll
140 * get back in sync eventually.
141 */
142 return repl;
143}
144
145/* Helper function to do delayed processing of ECN CWR chunk. */
146static void sctp_do_ecn_cwr_work(struct sctp_association *asoc,
147 __u32 lowest_tsn)
148{
149 /* Turn off ECNE getting auto-prepended to every outgoing
150 * packet
151 */
152 asoc->need_ecne = 0;
153}
154
155/* Generate SACK if necessary. We call this at the end of a packet. */
156static int sctp_gen_sack(struct sctp_association *asoc, int force,
157 sctp_cmd_seq_t *commands)
158{
159 __u32 ctsn, max_tsn_seen;
160 struct sctp_chunk *sack;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800161 struct sctp_transport *trans = asoc->peer.last_data_from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int error = 0;
163
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900164 if (force ||
Frank Filz52ccb8e2005-12-22 11:36:46 -0800165 (!trans && (asoc->param_flags & SPP_SACKDELAY_DISABLE)) ||
166 (trans && (trans->param_flags & SPP_SACKDELAY_DISABLE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 asoc->peer.sack_needed = 1;
168
169 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
170 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
171
172 /* From 12.2 Parameters necessary per association (i.e. the TCB):
173 *
174 * Ack State : This flag indicates if the next received packet
175 * : is to be responded to with a SACK. ...
176 * : When DATA chunks are out of order, SACK's
177 * : are not delayed (see Section 6).
178 *
179 * [This is actually not mentioned in Section 6, but we
180 * implement it here anyway. --piggy]
181 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900182 if (max_tsn_seen != ctsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 asoc->peer.sack_needed = 1;
184
185 /* From 6.2 Acknowledgement on Reception of DATA Chunks:
186 *
187 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically,
188 * an acknowledgement SHOULD be generated for at least every
189 * second packet (not every second DATA chunk) received, and
190 * SHOULD be generated within 200 ms of the arrival of any
191 * unacknowledged DATA chunk. ...
192 */
193 if (!asoc->peer.sack_needed) {
Wei Yongjund364d922008-05-09 15:13:26 -0700194 asoc->peer.sack_cnt++;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800195
196 /* Set the SACK delay timeout based on the
197 * SACK delay for the last transport
198 * data was received from, or the default
199 * for the association.
200 */
Wei Yongjund364d922008-05-09 15:13:26 -0700201 if (trans) {
202 /* We will need a SACK for the next packet. */
203 if (asoc->peer.sack_cnt >= trans->sackfreq - 1)
204 asoc->peer.sack_needed = 1;
205
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900206 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
Frank Filz52ccb8e2005-12-22 11:36:46 -0800207 trans->sackdelay;
Wei Yongjund364d922008-05-09 15:13:26 -0700208 } else {
209 /* We will need a SACK for the next packet. */
210 if (asoc->peer.sack_cnt >= asoc->sackfreq - 1)
211 asoc->peer.sack_needed = 1;
212
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900213 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
Frank Filz52ccb8e2005-12-22 11:36:46 -0800214 asoc->sackdelay;
Wei Yongjund364d922008-05-09 15:13:26 -0700215 }
Frank Filz52ccb8e2005-12-22 11:36:46 -0800216
217 /* Restart the SACK timer. */
218 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
219 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
Vlad Yasevich90f2f532009-11-23 15:53:57 -0500221 asoc->a_rwnd = asoc->rwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sack = sctp_make_sack(asoc);
223 if (!sack)
224 goto nomem;
225
226 asoc->peer.sack_needed = 0;
Wei Yongjund364d922008-05-09 15:13:26 -0700227 asoc->peer.sack_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Vlad Yasevich732ba352007-01-15 19:20:21 -0800229 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(sack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Stop the SACK timer. */
232 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
233 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
234 }
Frank Filz52ccb8e2005-12-22 11:36:46 -0800235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return error;
237nomem:
238 error = -ENOMEM;
239 return error;
240}
241
242/* When the T3-RTX timer expires, it calls this function to create the
243 * relevant state machine event.
244 */
245void sctp_generate_t3_rtx_event(unsigned long peer)
246{
247 int error;
248 struct sctp_transport *transport = (struct sctp_transport *) peer;
249 struct sctp_association *asoc = transport->asoc;
250
251 /* Check whether a task is in the sock. */
252
253 sctp_bh_lock_sock(asoc->base.sk);
254 if (sock_owned_by_user(asoc->base.sk)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800255 SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Try again later. */
258 if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
259 sctp_transport_hold(transport);
260 goto out_unlock;
261 }
262
263 /* Is this transport really dead and just waiting around for
264 * the timer to let go of the reference?
265 */
266 if (transport->dead)
267 goto out_unlock;
268
269 /* Run through the state machine. */
270 error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
271 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
272 asoc->state,
273 asoc->ep, asoc,
274 transport, GFP_ATOMIC);
275
276 if (error)
277 asoc->base.sk->sk_err = -error;
278
279out_unlock:
280 sctp_bh_unlock_sock(asoc->base.sk);
281 sctp_transport_put(transport);
282}
283
284/* This is a sa interface for producing timeout events. It works
285 * for timeouts which use the association as their parameter.
286 */
287static void sctp_generate_timeout_event(struct sctp_association *asoc,
288 sctp_event_timeout_t timeout_type)
289{
290 int error = 0;
291
292 sctp_bh_lock_sock(asoc->base.sk);
293 if (sock_owned_by_user(asoc->base.sk)) {
294 SCTP_DEBUG_PRINTK("%s:Sock is busy: timer %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800295 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 timeout_type);
297
298 /* Try again later. */
299 if (!mod_timer(&asoc->timers[timeout_type], jiffies + (HZ/20)))
300 sctp_association_hold(asoc);
301 goto out_unlock;
302 }
303
304 /* Is this association really dead and just waiting around for
305 * the timer to let go of the reference?
306 */
307 if (asoc->base.dead)
308 goto out_unlock;
309
310 /* Run through the state machine. */
311 error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
312 SCTP_ST_TIMEOUT(timeout_type),
313 asoc->state, asoc->ep, asoc,
314 (void *)timeout_type, GFP_ATOMIC);
315
316 if (error)
317 asoc->base.sk->sk_err = -error;
318
319out_unlock:
320 sctp_bh_unlock_sock(asoc->base.sk);
321 sctp_association_put(asoc);
322}
323
324static void sctp_generate_t1_cookie_event(unsigned long data)
325{
326 struct sctp_association *asoc = (struct sctp_association *) data;
327 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE);
328}
329
330static void sctp_generate_t1_init_event(unsigned long data)
331{
332 struct sctp_association *asoc = (struct sctp_association *) data;
333 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT);
334}
335
336static void sctp_generate_t2_shutdown_event(unsigned long data)
337{
338 struct sctp_association *asoc = (struct sctp_association *) data;
339 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN);
340}
341
342static void sctp_generate_t4_rto_event(unsigned long data)
343{
344 struct sctp_association *asoc = (struct sctp_association *) data;
345 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T4_RTO);
346}
347
348static void sctp_generate_t5_shutdown_guard_event(unsigned long data)
349{
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900350 struct sctp_association *asoc = (struct sctp_association *)data;
351 sctp_generate_timeout_event(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD);
353
354} /* sctp_generate_t5_shutdown_guard_event() */
355
356static void sctp_generate_autoclose_event(unsigned long data)
357{
358 struct sctp_association *asoc = (struct sctp_association *) data;
359 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE);
360}
361
362/* Generate a heart beat event. If the sock is busy, reschedule. Make
363 * sure that the transport is still valid.
364 */
365void sctp_generate_heartbeat_event(unsigned long data)
366{
367 int error = 0;
368 struct sctp_transport *transport = (struct sctp_transport *) data;
369 struct sctp_association *asoc = transport->asoc;
370
371 sctp_bh_lock_sock(asoc->base.sk);
372 if (sock_owned_by_user(asoc->base.sk)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800373 SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* Try again later. */
376 if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
377 sctp_transport_hold(transport);
378 goto out_unlock;
379 }
380
381 /* Is this structure just waiting around for us to actually
382 * get destroyed?
383 */
384 if (transport->dead)
385 goto out_unlock;
386
387 error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
388 SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
389 asoc->state, asoc->ep, asoc,
390 transport, GFP_ATOMIC);
391
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900392 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 asoc->base.sk->sk_err = -error;
394
395out_unlock:
396 sctp_bh_unlock_sock(asoc->base.sk);
397 sctp_transport_put(transport);
398}
399
400/* Inject a SACK Timeout event into the state machine. */
401static void sctp_generate_sack_event(unsigned long data)
402{
403 struct sctp_association *asoc = (struct sctp_association *) data;
404 sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK);
405}
406
407sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES] = {
408 NULL,
409 sctp_generate_t1_cookie_event,
410 sctp_generate_t1_init_event,
411 sctp_generate_t2_shutdown_event,
412 NULL,
413 sctp_generate_t4_rto_event,
414 sctp_generate_t5_shutdown_guard_event,
Vladislav Yasevich1e7d3d92005-11-11 16:06:16 -0800415 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sctp_generate_sack_event,
417 sctp_generate_autoclose_event,
418};
419
420
421/* RFC 2960 8.2 Path Failure Detection
422 *
423 * When its peer endpoint is multi-homed, an endpoint should keep a
424 * error counter for each of the destination transport addresses of the
425 * peer endpoint.
426 *
427 * Each time the T3-rtx timer expires on any address, or when a
428 * HEARTBEAT sent to an idle address is not acknowledged within a RTO,
429 * the error counter of that destination address will be incremented.
430 * When the value in the error counter exceeds the protocol parameter
431 * 'Path.Max.Retrans' of that destination address, the endpoint should
432 * mark the destination transport address as inactive, and a
433 * notification SHOULD be sent to the upper layer.
434 *
435 */
436static void sctp_do_8_2_transport_strike(struct sctp_association *asoc,
Vlad Yasevich7e990132009-03-02 09:46:14 +0000437 struct sctp_transport *transport,
438 int is_hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 /* The check for association's overall error counter exceeding the
441 * threshold is done in the state function.
442 */
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400443 /* We are here due to a timer expiration. If the timer was
444 * not a HEARTBEAT, then normal error tracking is done.
445 * If the timer was a heartbeat, we only increment error counts
446 * when we already have an outstanding HEARTBEAT that has not
447 * been acknowledged.
448 * Additionaly, some tranport states inhibit error increments.
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700449 */
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400450 if (!is_hb) {
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700451 asoc->overall_error_count++;
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400452 if (transport->state != SCTP_INACTIVE)
453 transport->error_count++;
454 } else if (transport->hb_sent) {
455 if (transport->state != SCTP_UNCONFIRMED)
456 asoc->overall_error_count++;
457 if (transport->state != SCTP_INACTIVE)
458 transport->error_count++;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Frank Filz3f7a87d2005-06-20 13:14:57 -0700461 if (transport->state != SCTP_INACTIVE &&
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400462 (transport->error_count > transport->pathmaxrxt)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -0700463 SCTP_DEBUG_PRINTK_IPADDR("transport_strike:association %p",
464 " transport IP: port:%d failed.\n",
465 asoc,
Al Virob3f5b3b2006-11-20 17:22:43 -0800466 (&transport->ipaddr),
467 ntohs(transport->ipaddr.v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 sctp_assoc_control_transport(asoc, transport,
469 SCTP_TRANSPORT_DOWN,
470 SCTP_FAILED_THRESHOLD);
471 }
472
473 /* E2) For the destination address for which the timer
474 * expires, set RTO <- RTO * 2 ("back off the timer"). The
475 * maximum value discussed in rule C7 above (RTO.max) may be
476 * used to provide an upper bound to this doubling operation.
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000477 *
478 * Special Case: the first HB doesn't trigger exponential backoff.
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800479 * The first unacknowledged HB triggers it. We do this with a flag
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000480 * that indicates that we have an outstanding HB.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Vlad Yasevich7e990132009-03-02 09:46:14 +0000482 if (!is_hb || transport->hb_sent) {
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000483 transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487/* Worker routine to handle INIT command failure. */
488static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
489 struct sctp_association *asoc,
490 unsigned error)
491{
492 struct sctp_ulpevent *event;
493
494 event = sctp_ulpevent_make_assoc_change(asoc,0, SCTP_CANT_STR_ASSOC,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700495 (__u16)error, 0, 0, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 GFP_ATOMIC);
497
498 if (event)
499 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
500 SCTP_ULPEVENT(event));
501
502 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
503 SCTP_STATE(SCTP_STATE_CLOSED));
504
505 /* SEND_FAILED sent later when cleaning up the association. */
506 asoc->outqueue.error = error;
507 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
508}
509
510/* Worker routine to handle SCTP_CMD_ASSOC_FAILED. */
511static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
512 struct sctp_association *asoc,
513 sctp_event_t event_type,
514 sctp_subtype_t subtype,
515 struct sctp_chunk *chunk,
516 unsigned error)
517{
518 struct sctp_ulpevent *event;
519
520 /* Cancel any partial delivery in progress. */
521 sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
522
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700523 if (event_type == SCTP_EVENT_T_CHUNK && subtype.chunk == SCTP_CID_ABORT)
524 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
525 (__u16)error, 0, 0, chunk,
526 GFP_ATOMIC);
527 else
528 event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
529 (__u16)error, 0, 0, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 GFP_ATOMIC);
531 if (event)
532 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
533 SCTP_ULPEVENT(event));
534
535 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
536 SCTP_STATE(SCTP_STATE_CLOSED));
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* SEND_FAILED sent later when cleaning up the association. */
539 asoc->outqueue.error = error;
540 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
541}
542
543/* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
544 * inside the cookie. In reality, this is only used for INIT-ACK processing
545 * since all other cases use "temporary" associations and can do all
546 * their work in statefuns directly.
547 */
548static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
549 struct sctp_association *asoc,
550 struct sctp_chunk *chunk,
Alexey Dobriyan3182cd82005-07-11 20:57:47 -0700551 sctp_init_chunk_t *peer_init,
Al Virodd0fc662005-10-07 07:46:04 +0100552 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int error;
555
556 /* We only process the init as a sideeffect in a single
557 * case. This is when we process the INIT-ACK. If we
558 * fail during INIT processing (due to malloc problems),
559 * just return the error and stop processing the stack.
560 */
561 if (!sctp_process_init(asoc, chunk->chunk_hdr->type,
562 sctp_source(chunk), peer_init, gfp))
563 error = -ENOMEM;
564 else
565 error = 0;
566
567 return error;
568}
569
570/* Helper function to break out starting up of heartbeat timers. */
571static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
572 struct sctp_association *asoc)
573{
574 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Start a heartbeat timer for each transport on the association.
577 * hold a reference on the transport to make sure none of
578 * the needed data structures go away.
579 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700580 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
583 sctp_transport_hold(t);
584 }
585}
586
587static void sctp_cmd_hb_timers_stop(sctp_cmd_seq_t *cmds,
588 struct sctp_association *asoc)
589{
590 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Stop all heartbeat timers. */
593
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700594 list_for_each_entry(t, &asoc->peer.transport_addr_list,
595 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (del_timer(&t->hb_timer))
597 sctp_transport_put(t);
598 }
599}
600
601/* Helper function to stop any pending T3-RTX timers */
602static void sctp_cmd_t3_rtx_timers_stop(sctp_cmd_seq_t *cmds,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900603 struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700607 list_for_each_entry(t, &asoc->peer.transport_addr_list,
608 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (timer_pending(&t->T3_rtx_timer) &&
610 del_timer(&t->T3_rtx_timer)) {
611 sctp_transport_put(t);
612 }
613 }
614}
615
616
617/* Helper function to update the heartbeat timer. */
618static void sctp_cmd_hb_timer_update(sctp_cmd_seq_t *cmds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct sctp_transport *t)
620{
621 /* Update the heartbeat timer. */
622 if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
623 sctp_transport_hold(t);
624}
625
626/* Helper function to handle the reception of an HEARTBEAT ACK. */
627static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
628 struct sctp_association *asoc,
629 struct sctp_transport *t,
630 struct sctp_chunk *chunk)
631{
632 sctp_sender_hb_info_t *hbinfo;
633
634 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the
635 * HEARTBEAT should clear the error counter of the destination
636 * transport address to which the HEARTBEAT was sent.
637 * The association's overall error count is also cleared.
638 */
639 t->error_count = 0;
640 t->asoc->overall_error_count = 0;
641
Vlad Yasevichfaee47c2009-02-13 08:33:43 +0000642 /* Clear the hb_sent flag to signal that we had a good
643 * acknowledgement.
644 */
645 t->hb_sent = 0;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Mark the destination transport address as active if it is not so
648 * marked.
649 */
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700650 if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
652 SCTP_HEARTBEAT_SUCCESS);
653
654 /* The receiver of the HEARTBEAT ACK should also perform an
655 * RTT measurement for that destination transport address
656 * using the time value carried in the HEARTBEAT ACK chunk.
Vlad Yaseviche533ca12007-01-30 14:36:14 -0800657 * If the transport's rto_pending variable has been cleared,
658 * it was most likely due to a retransmit. However, we want
659 * to re-enable it to properly update the rto.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Vlad Yaseviche533ca12007-01-30 14:36:14 -0800661 if (t->rto_pending == 0)
662 t->rto_pending = 1;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
665 sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700666
667 /* Update the heartbeat timer. */
668 if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
669 sctp_transport_hold(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/* Helper function to process the process SACK command. */
674static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
675 struct sctp_association *asoc,
676 struct sctp_sackhdr *sackh)
677{
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700678 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (sctp_outq_sack(&asoc->outqueue, sackh)) {
681 /* There are no more TSNs awaiting SACK. */
682 err = sctp_do_sm(SCTP_EVENT_T_OTHER,
683 SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
684 asoc->state, asoc->ep, asoc, NULL,
685 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 return err;
689}
690
691/* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
692 * the transport for a shutdown chunk.
693 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900694static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct sctp_association *asoc,
696 struct sctp_chunk *chunk)
697{
698 struct sctp_transport *t;
699
Vlad Yasevichc17b02b2010-04-30 21:42:43 -0400700 if (chunk->transport)
701 t = chunk->transport;
702 else {
703 t = sctp_assoc_choose_alter_transport(asoc,
Wei Yongjun9919b452009-05-12 21:52:51 +0800704 asoc->shutdown_last_sent_to);
Vlad Yasevichc17b02b2010-04-30 21:42:43 -0400705 chunk->transport = t;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 asoc->shutdown_last_sent_to = t;
708 asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/* Helper function to change the state of an association. */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900712static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct sctp_association *asoc,
714 sctp_state_t state)
715{
716 struct sock *sk = asoc->base.sk;
717
718 asoc->state = state;
719
Frank Filz3f7a87d2005-06-20 13:14:57 -0700720 SCTP_DEBUG_PRINTK("sctp_cmd_new_state: asoc %p[%s]\n",
721 asoc, sctp_state_tbl[state]);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (sctp_style(sk, TCP)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -0700724 /* Change the sk->sk_state of a TCP-style socket that has
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200725 * successfully completed a connect() call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727 if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED))
728 sk->sk_state = SCTP_SS_ESTABLISHED;
729
730 /* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */
731 if (sctp_state(asoc, SHUTDOWN_RECEIVED) &&
732 sctp_sstate(sk, ESTABLISHED))
733 sk->sk_shutdown |= RCV_SHUTDOWN;
734 }
735
Frank Filz3f7a87d2005-06-20 13:14:57 -0700736 if (sctp_state(asoc, COOKIE_WAIT)) {
737 /* Reset init timeouts since they may have been
738 * increased due to timer expirations.
739 */
740 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
Vladislav Yasevich1e7d3d92005-11-11 16:06:16 -0800741 asoc->rto_initial;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700742 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
Vladislav Yasevich1e7d3d92005-11-11 16:06:16 -0800743 asoc->rto_initial;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700744 }
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (sctp_state(asoc, ESTABLISHED) ||
747 sctp_state(asoc, CLOSED) ||
748 sctp_state(asoc, SHUTDOWN_RECEIVED)) {
749 /* Wake up any processes waiting in the asoc's wait queue in
750 * sctp_wait_for_connect() or sctp_wait_for_sndbuf().
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (waitqueue_active(&asoc->wait))
753 wake_up_interruptible(&asoc->wait);
754
755 /* Wake up any processes waiting in the sk's sleep queue of
756 * a TCP-style or UDP-style peeled-off socket in
757 * sctp_wait_for_accept() or sctp_wait_for_packet().
758 * For a UDP-style socket, the waiters are woken up by the
759 * notifications.
760 */
761 if (!sctp_style(sk, UDP))
762 sk->sk_state_change(sk);
763 }
764}
765
766/* Helper function to delete an association. */
767static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
768 struct sctp_association *asoc)
769{
770 struct sock *sk = asoc->base.sk;
771
772 /* If it is a non-temporary association belonging to a TCP-style
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900773 * listening socket that is not closed, do not free it so that accept()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * can pick it up later.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
777 (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
778 return;
779
780 sctp_unhash_established(asoc);
781 sctp_association_free(asoc);
782}
783
784/*
785 * ADDIP Section 4.1 ASCONF Chunk Procedures
786 * A4) Start a T-4 RTO timer, using the RTO value of the selected
787 * destination address (we use active path instead of primary path just
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900788 * because primary path may be inactive.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
790static void sctp_cmd_setup_t4(sctp_cmd_seq_t *cmds,
791 struct sctp_association *asoc,
792 struct sctp_chunk *chunk)
793{
794 struct sctp_transport *t;
795
Wei Yongjun9919b452009-05-12 21:52:51 +0800796 t = sctp_assoc_choose_alter_transport(asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
798 chunk->transport = t;
799}
800
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900801/* Process an incoming Operation Error Chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static void sctp_cmd_process_operr(sctp_cmd_seq_t *cmds,
803 struct sctp_association *asoc,
804 struct sctp_chunk *chunk)
805{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct sctp_errhdr *err_hdr;
Wei Yongjun3df26782009-03-02 06:46:51 +0000807 struct sctp_ulpevent *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Wei Yongjun3df26782009-03-02 06:46:51 +0000809 while (chunk->chunk_end > chunk->skb->data) {
810 err_hdr = (struct sctp_errhdr *)(chunk->skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Wei Yongjun3df26782009-03-02 06:46:51 +0000812 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
813 GFP_ATOMIC);
814 if (!ev)
815 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Wei Yongjun3df26782009-03-02 06:46:51 +0000817 sctp_ulpq_tail_event(&asoc->ulpq, ev);
818
819 switch (err_hdr->cause) {
820 case SCTP_ERROR_UNKNOWN_CHUNK:
821 {
822 sctp_chunkhdr_t *unk_chunk_hdr;
823
824 unk_chunk_hdr = (sctp_chunkhdr_t *)err_hdr->variable;
825 switch (unk_chunk_hdr->type) {
826 /* ADDIP 4.1 A9) If the peer responds to an ASCONF with
827 * an ERROR chunk reporting that it did not recognized
828 * the ASCONF chunk type, the sender of the ASCONF MUST
829 * NOT send any further ASCONF chunks and MUST stop its
830 * T-4 timer.
831 */
832 case SCTP_CID_ASCONF:
833 if (asoc->peer.asconf_capable == 0)
834 break;
835
836 asoc->peer.asconf_capable = 0;
837 sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Wei Yongjun3df26782009-03-02 06:46:51 +0000839 break;
840 default:
841 break;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
Wei Yongjun3df26782009-03-02 06:46:51 +0000844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 default:
846 break;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849}
850
851/* Process variable FWDTSN chunk information. */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900852static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct sctp_chunk *chunk)
854{
855 struct sctp_fwdtsn_skip *skip;
856 /* Walk through all the skipped SSNs */
857 sctp_walk_fwdtsn(skip, chunk) {
858 sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
859 }
860
861 return;
862}
863
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900864/* Helper function to remove the association non-primary peer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * transports.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
868{
869 struct sctp_transport *t;
870 struct list_head *pos;
871 struct list_head *temp;
872
873 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
874 t = list_entry(pos, struct sctp_transport, transports);
Al Viro5f242a12006-11-20 17:05:23 -0800875 if (!sctp_cmp_addr_exact(&t->ipaddr,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900876 &asoc->peer.primary_addr)) {
Al Viro38a03142006-11-20 17:06:45 -0800877 sctp_assoc_del_peer(asoc, &t->ipaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 }
880
881 return;
882}
883
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700884/* Helper function to set sk_err on a 1-1 style socket. */
885static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
886{
887 struct sock *sk = asoc->base.sk;
888
889 if (!sctp_style(sk, UDP))
890 sk->sk_err = error;
891}
892
Vlad Yasevich07d93962007-05-04 13:55:27 -0700893/* Helper function to generate an association change event */
894static void sctp_cmd_assoc_change(sctp_cmd_seq_t *commands,
895 struct sctp_association *asoc,
896 u8 state)
897{
898 struct sctp_ulpevent *ev;
899
900 ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
901 asoc->c.sinit_num_ostreams,
902 asoc->c.sinit_max_instreams,
903 NULL, GFP_ATOMIC);
904 if (ev)
905 sctp_ulpq_tail_event(&asoc->ulpq, ev);
906}
907
908/* Helper function to generate an adaptation indication event */
909static void sctp_cmd_adaptation_ind(sctp_cmd_seq_t *commands,
910 struct sctp_association *asoc)
911{
912 struct sctp_ulpevent *ev;
913
914 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
915
916 if (ev)
917 sctp_ulpq_tail_event(&asoc->ulpq, ev);
918}
919
Vlad Yasevich96cd0d32008-09-08 14:00:26 -0400920
921static void sctp_cmd_t1_timer_update(struct sctp_association *asoc,
922 sctp_event_timeout_t timer,
923 char *name)
924{
925 struct sctp_transport *t;
926
927 t = asoc->init_last_sent_to;
928 asoc->init_err_counter++;
929
930 if (t->init_sent_count > (asoc->init_cycle + 1)) {
931 asoc->timeouts[timer] *= 2;
932 if (asoc->timeouts[timer] > asoc->max_init_timeo) {
933 asoc->timeouts[timer] = asoc->max_init_timeo;
934 }
935 asoc->init_cycle++;
936 SCTP_DEBUG_PRINTK(
937 "T1 %s Timeout adjustment"
938 " init_err_counter: %d"
939 " cycle: %d"
940 " timeout: %ld\n",
941 name,
942 asoc->init_err_counter,
943 asoc->init_cycle,
944 asoc->timeouts[timer]);
945 }
946
947}
948
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -0400949/* Send the whole message, chunk by chunk, to the outqueue.
950 * This way the whole message is queued up and bundling if
951 * encouraged for small fragments.
952 */
953static int sctp_cmd_send_msg(struct sctp_association *asoc,
954 struct sctp_datamsg *msg)
955{
956 struct sctp_chunk *chunk;
957 int error = 0;
958
959 list_for_each_entry(chunk, &msg->chunks, frag_list) {
960 error = sctp_outq_tail(&asoc->outqueue, chunk);
961 if (error)
962 break;
963 }
964
965 return error;
966}
967
968
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/* These three macros allow us to pull the debugging code out of the
971 * main flow of sctp_do_sm() to keep attention focused on the real
972 * functionality there.
973 */
974#define DEBUG_PRE \
975 SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
976 "ep %p, %s, %s, asoc %p[%s], %s\n", \
977 ep, sctp_evttype_tbl[event_type], \
978 (*debug_fn)(subtype), asoc, \
979 sctp_state_tbl[state], state_fn->name)
980
981#define DEBUG_POST \
982 SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
983 "asoc %p, status: %s\n", \
984 asoc, sctp_status_tbl[status])
985
986#define DEBUG_POST_SFX \
987 SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
988 error, asoc, \
989 sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
990 sctp_assoc2id(asoc)))?asoc->state:SCTP_STATE_CLOSED])
991
992/*
993 * This is the master state machine processing function.
994 *
995 * If you want to understand all of lksctp, this is a
996 * good place to start.
997 */
998int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
999 sctp_state_t state,
1000 struct sctp_endpoint *ep,
1001 struct sctp_association *asoc,
1002 void *event_arg,
Al Virodd0fc662005-10-07 07:46:04 +01001003 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 sctp_cmd_seq_t commands;
1006 const sctp_sm_table_entry_t *state_fn;
1007 sctp_disposition_t status;
1008 int error = 0;
1009 typedef const char *(printfn_t)(sctp_subtype_t);
1010
1011 static printfn_t *table[] = {
1012 NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
1013 };
1014 printfn_t *debug_fn __attribute__ ((unused)) = table[event_type];
1015
1016 /* Look up the state function, run it, and then process the
1017 * side effects. These three steps are the heart of lksctp.
1018 */
1019 state_fn = sctp_sm_lookup_event(event_type, state, subtype);
1020
1021 sctp_init_cmd_seq(&commands);
1022
1023 DEBUG_PRE;
1024 status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
1025 DEBUG_POST;
1026
1027 error = sctp_side_effects(event_type, subtype, state,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001028 ep, asoc, event_arg, status,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 &commands, gfp);
1030 DEBUG_POST_SFX;
1031
1032 return error;
1033}
1034
1035#undef DEBUG_PRE
1036#undef DEBUG_POST
1037
1038/*****************************************************************
1039 * This the master state function side effect processing function.
1040 *****************************************************************/
1041static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
1042 sctp_state_t state,
1043 struct sctp_endpoint *ep,
1044 struct sctp_association *asoc,
1045 void *event_arg,
1046 sctp_disposition_t status,
1047 sctp_cmd_seq_t *commands,
Al Virodd0fc662005-10-07 07:46:04 +01001048 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 int error;
1051
1052 /* FIXME - Most of the dispositions left today would be categorized
1053 * as "exceptional" dispositions. For those dispositions, it
1054 * may not be proper to run through any of the commands at all.
1055 * For example, the command interpreter might be run only with
1056 * disposition SCTP_DISPOSITION_CONSUME.
1057 */
1058 if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
1059 ep, asoc,
1060 event_arg, status,
1061 commands, gfp)))
1062 goto bail;
1063
1064 switch (status) {
1065 case SCTP_DISPOSITION_DISCARD:
1066 SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
1067 "event_type %d, event_id %d\n",
1068 state, event_type, subtype.chunk);
1069 break;
1070
1071 case SCTP_DISPOSITION_NOMEM:
1072 /* We ran out of memory, so we need to discard this
1073 * packet.
1074 */
1075 /* BUG--we should now recover some memory, probably by
1076 * reneging...
1077 */
1078 error = -ENOMEM;
1079 break;
1080
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001081 case SCTP_DISPOSITION_DELETE_TCB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* This should now be a command. */
1083 break;
1084
1085 case SCTP_DISPOSITION_CONSUME:
1086 case SCTP_DISPOSITION_ABORT:
1087 /*
1088 * We should no longer have much work to do here as the
1089 * real work has been done as explicit commands above.
1090 */
1091 break;
1092
1093 case SCTP_DISPOSITION_VIOLATION:
Wei Yongjund99fa422007-08-27 11:19:24 +08001094 if (net_ratelimit())
1095 printk(KERN_ERR "sctp protocol violation state %d "
1096 "chunkid %d\n", state, subtype.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098
1099 case SCTP_DISPOSITION_NOT_IMPL:
1100 printk(KERN_WARNING "sctp unimplemented feature in state %d, "
1101 "event_type %d, event_id %d\n",
1102 state, event_type, subtype.chunk);
1103 break;
1104
1105 case SCTP_DISPOSITION_BUG:
1106 printk(KERN_ERR "sctp bug in state %d, "
1107 "event_type %d, event_id %d\n",
1108 state, event_type, subtype.chunk);
1109 BUG();
1110 break;
1111
1112 default:
1113 printk(KERN_ERR "sctp impossible disposition %d "
1114 "in state %d, event_type %d, event_id %d\n",
1115 status, state, event_type, subtype.chunk);
1116 BUG();
1117 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120bail:
1121 return error;
1122}
1123
1124/********************************************************************
1125 * 2nd Level Abstractions
1126 ********************************************************************/
1127
1128/* This is the side-effect interpreter. */
1129static int sctp_cmd_interpreter(sctp_event_t event_type,
1130 sctp_subtype_t subtype,
1131 sctp_state_t state,
1132 struct sctp_endpoint *ep,
1133 struct sctp_association *asoc,
1134 void *event_arg,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001135 sctp_disposition_t status,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 sctp_cmd_seq_t *commands,
Al Virodd0fc662005-10-07 07:46:04 +01001137 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 int error = 0;
1140 int force;
1141 sctp_cmd_t *cmd;
1142 struct sctp_chunk *new_obj;
1143 struct sctp_chunk *chunk = NULL;
1144 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 struct timer_list *timer;
1146 unsigned long timeout;
1147 struct sctp_transport *t;
1148 struct sctp_sackhdr sackh;
1149 int local_cork = 0;
1150
1151 if (SCTP_EVENT_T_TIMEOUT != event_type)
1152 chunk = (struct sctp_chunk *) event_arg;
1153
1154 /* Note: This whole file is a huge candidate for rework.
1155 * For example, each command could either have its own handler, so
1156 * the loop would look like:
1157 * while (cmds)
1158 * cmd->handle(x, y, z)
1159 * --jgrimm
1160 */
1161 while (NULL != (cmd = sctp_next_cmd(commands))) {
1162 switch (cmd->verb) {
1163 case SCTP_CMD_NOP:
1164 /* Do nothing. */
1165 break;
1166
1167 case SCTP_CMD_NEW_ASOC:
1168 /* Register a new association. */
1169 if (local_cork) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001170 sctp_outq_uncork(&asoc->outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 local_cork = 0;
1172 }
1173 asoc = cmd->obj.ptr;
1174 /* Register with the endpoint. */
1175 sctp_endpoint_add_asoc(ep, asoc);
1176 sctp_hash_established(asoc);
1177 break;
1178
1179 case SCTP_CMD_UPDATE_ASSOC:
1180 sctp_assoc_update(asoc, cmd->obj.ptr);
1181 break;
1182
1183 case SCTP_CMD_PURGE_OUTQUEUE:
1184 sctp_outq_teardown(&asoc->outqueue);
1185 break;
1186
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001187 case SCTP_CMD_DELETE_TCB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (local_cork) {
1189 sctp_outq_uncork(&asoc->outqueue);
1190 local_cork = 0;
1191 }
1192 /* Delete the current association. */
1193 sctp_cmd_delete_tcb(commands, asoc);
1194 asoc = NULL;
1195 break;
1196
1197 case SCTP_CMD_NEW_STATE:
1198 /* Enter a new state. */
1199 sctp_cmd_new_state(commands, asoc, cmd->obj.state);
1200 break;
1201
1202 case SCTP_CMD_REPORT_TSN:
1203 /* Record the arrival of a TSN. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -07001204 error = sctp_tsnmap_mark(&asoc->peer.tsn_map,
1205 cmd->obj.u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207
1208 case SCTP_CMD_REPORT_FWDTSN:
1209 /* Move the Cumulattive TSN Ack ahead. */
1210 sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
1211
Vlad Yasevichea2dfb32007-07-13 17:01:19 -04001212 /* purge the fragmentation queue */
1213 sctp_ulpq_reasm_flushtsn(&asoc->ulpq, cmd->obj.u32);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Abort any in progress partial delivery. */
1216 sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
1217 break;
1218
1219 case SCTP_CMD_PROCESS_FWDTSN:
1220 sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 case SCTP_CMD_GEN_SACK:
1224 /* Generate a Selective ACK.
1225 * The argument tells us whether to just count
1226 * the packet and MAYBE generate a SACK, or
1227 * force a SACK out.
1228 */
1229 force = cmd->obj.i32;
1230 error = sctp_gen_sack(asoc, force, commands);
1231 break;
1232
1233 case SCTP_CMD_PROCESS_SACK:
1234 /* Process an inbound SACK. */
1235 error = sctp_cmd_process_sack(commands, asoc,
1236 cmd->obj.ptr);
1237 break;
1238
1239 case SCTP_CMD_GEN_INIT_ACK:
1240 /* Generate an INIT ACK chunk. */
1241 new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
1242 0);
1243 if (!new_obj)
1244 goto nomem;
1245
1246 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1247 SCTP_CHUNK(new_obj));
1248 break;
1249
1250 case SCTP_CMD_PEER_INIT:
1251 /* Process a unified INIT from the peer.
1252 * Note: Only used during INIT-ACK processing. If
1253 * there is an error just return to the outter
1254 * layer which will bail.
1255 */
1256 error = sctp_cmd_process_init(commands, asoc, chunk,
1257 cmd->obj.ptr, gfp);
1258 break;
1259
1260 case SCTP_CMD_GEN_COOKIE_ECHO:
1261 /* Generate a COOKIE ECHO chunk. */
1262 new_obj = sctp_make_cookie_echo(asoc, chunk);
1263 if (!new_obj) {
1264 if (cmd->obj.ptr)
1265 sctp_chunk_free(cmd->obj.ptr);
1266 goto nomem;
1267 }
1268 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1269 SCTP_CHUNK(new_obj));
1270
1271 /* If there is an ERROR chunk to be sent along with
1272 * the COOKIE_ECHO, send it, too.
1273 */
1274 if (cmd->obj.ptr)
1275 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1276 SCTP_CHUNK(cmd->obj.ptr));
1277
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04001278 if (new_obj->transport) {
1279 new_obj->transport->init_sent_count++;
1280 asoc->init_last_sent_to = new_obj->transport;
1281 }
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* FIXME - Eventually come up with a cleaner way to
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001284 * enabling COOKIE-ECHO + DATA bundling during
1285 * multihoming stale cookie scenarios, the following
1286 * command plays with asoc->peer.retran_path to
1287 * avoid the problem of sending the COOKIE-ECHO and
1288 * DATA in different paths, which could result
1289 * in the association being ABORTed if the DATA chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * is processed first by the server. Checking the
1291 * init error counter simply causes this command
1292 * to be executed only during failed attempts of
1293 * association establishment.
1294 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001295 if ((asoc->peer.retran_path !=
1296 asoc->peer.primary_path) &&
1297 (asoc->init_err_counter > 0)) {
1298 sctp_add_cmd_sf(commands,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001299 SCTP_CMD_FORCE_PRIM_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 SCTP_NULL());
1301 }
1302
1303 break;
1304
1305 case SCTP_CMD_GEN_SHUTDOWN:
1306 /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
1307 * Reset error counts.
1308 */
1309 asoc->overall_error_count = 0;
1310
1311 /* Generate a SHUTDOWN chunk. */
1312 new_obj = sctp_make_shutdown(asoc, chunk);
1313 if (!new_obj)
1314 goto nomem;
1315 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1316 SCTP_CHUNK(new_obj));
1317 break;
1318
1319 case SCTP_CMD_CHUNK_ULP:
1320 /* Send a chunk to the sockets layer. */
1321 SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1322 "chunk_up:", cmd->obj.ptr,
1323 "ulpq:", &asoc->ulpq);
1324 sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
1325 GFP_ATOMIC);
1326 break;
1327
1328 case SCTP_CMD_EVENT_ULP:
1329 /* Send a notification to the sockets layer. */
1330 SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
1331 "event_up:",cmd->obj.ptr,
1332 "ulpq:",&asoc->ulpq);
1333 sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
1334 break;
1335
1336 case SCTP_CMD_REPLY:
1337 /* If an caller has not already corked, do cork. */
1338 if (!asoc->outqueue.cork) {
1339 sctp_outq_cork(&asoc->outqueue);
1340 local_cork = 1;
1341 }
1342 /* Send a chunk to our peer. */
1343 error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
1344 break;
1345
1346 case SCTP_CMD_SEND_PKT:
1347 /* Send a full packet to our peer. */
1348 packet = cmd->obj.ptr;
1349 sctp_packet_transmit(packet);
1350 sctp_ootb_pkt_free(packet);
1351 break;
1352
Vlad Yasevichb6157d82007-10-24 15:59:16 -04001353 case SCTP_CMD_T1_RETRAN:
1354 /* Mark a transport for retransmission. */
1355 sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1356 SCTP_RTXR_T1_RTX);
1357 break;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 case SCTP_CMD_RETRAN:
1360 /* Mark a transport for retransmission. */
1361 sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
1362 SCTP_RTXR_T3_RTX);
1363 break;
1364
1365 case SCTP_CMD_TRANSMIT:
1366 /* Kick start transmission. */
1367 error = sctp_outq_uncork(&asoc->outqueue);
1368 local_cork = 0;
1369 break;
1370
1371 case SCTP_CMD_ECN_CE:
1372 /* Do delayed CE processing. */
1373 sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
1374 break;
1375
1376 case SCTP_CMD_ECN_ECNE:
1377 /* Do delayed ECNE processing. */
1378 new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
1379 chunk);
1380 if (new_obj)
1381 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1382 SCTP_CHUNK(new_obj));
1383 break;
1384
1385 case SCTP_CMD_ECN_CWR:
1386 /* Do delayed CWR processing. */
1387 sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
1388 break;
1389
1390 case SCTP_CMD_SETUP_T2:
1391 sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
1392 break;
1393
1394 case SCTP_CMD_TIMER_START:
1395 timer = &asoc->timers[cmd->obj.to];
1396 timeout = asoc->timeouts[cmd->obj.to];
Kris Katterjohn09a62662006-01-08 22:24:28 -08001397 BUG_ON(!timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 timer->expires = jiffies + timeout;
1400 sctp_association_hold(asoc);
1401 add_timer(timer);
1402 break;
1403
1404 case SCTP_CMD_TIMER_RESTART:
1405 timer = &asoc->timers[cmd->obj.to];
1406 timeout = asoc->timeouts[cmd->obj.to];
1407 if (!mod_timer(timer, jiffies + timeout))
1408 sctp_association_hold(asoc);
1409 break;
1410
1411 case SCTP_CMD_TIMER_STOP:
1412 timer = &asoc->timers[cmd->obj.to];
1413 if (timer_pending(timer) && del_timer(timer))
1414 sctp_association_put(asoc);
1415 break;
1416
Frank Filz3f7a87d2005-06-20 13:14:57 -07001417 case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
1418 chunk = cmd->obj.ptr;
Wei Yongjun9919b452009-05-12 21:52:51 +08001419 t = sctp_assoc_choose_alter_transport(asoc,
1420 asoc->init_last_sent_to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001421 asoc->init_last_sent_to = t;
1422 chunk->transport = t;
1423 t->init_sent_count++;
Vlad Yaseviche0e9db12009-11-23 15:53:57 -05001424 /* Set the new transport as primary */
1425 sctp_assoc_set_primary(asoc, t);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001426 break;
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 case SCTP_CMD_INIT_RESTART:
1429 /* Do the needed accounting and updates
1430 * associated with restarting an initialization
Frank Filz3f7a87d2005-06-20 13:14:57 -07001431 * timer. Only multiply the timeout by two if
1432 * all transports have been tried at the current
1433 * timeout.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04001435 sctp_cmd_t1_timer_update(asoc,
1436 SCTP_EVENT_TIMEOUT_T1_INIT,
1437 "INIT");
Frank Filz3f7a87d2005-06-20 13:14:57 -07001438
1439 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
1440 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
1441 break;
1442
1443 case SCTP_CMD_COOKIEECHO_RESTART:
1444 /* Do the needed accounting and updates
1445 * associated with restarting an initialization
1446 * timer. Only multiply the timeout by two if
1447 * all transports have been tried at the current
1448 * timeout.
1449 */
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04001450 sctp_cmd_t1_timer_update(asoc,
1451 SCTP_EVENT_TIMEOUT_T1_COOKIE,
1452 "COOKIE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /* If we've sent any data bundled with
1455 * COOKIE-ECHO we need to resend.
1456 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001457 list_for_each_entry(t, &asoc->peer.transport_addr_list,
1458 transports) {
Vlad Yasevichb6157d82007-10-24 15:59:16 -04001459 sctp_retransmit_mark(&asoc->outqueue, t,
1460 SCTP_RTXR_T1_RTX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 sctp_add_cmd_sf(commands,
1464 SCTP_CMD_TIMER_RESTART,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001465 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467
1468 case SCTP_CMD_INIT_FAILED:
Al Virodc251b22006-11-20 17:00:44 -08001469 sctp_cmd_init_failed(commands, asoc, cmd->obj.err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471
1472 case SCTP_CMD_ASSOC_FAILED:
1473 sctp_cmd_assoc_failed(commands, asoc, event_type,
Al Viro5be291f2006-11-20 17:01:06 -08001474 subtype, chunk, cmd->obj.err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476
Frank Filz3f7a87d2005-06-20 13:14:57 -07001477 case SCTP_CMD_INIT_COUNTER_INC:
1478 asoc->init_err_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
1480
Frank Filz3f7a87d2005-06-20 13:14:57 -07001481 case SCTP_CMD_INIT_COUNTER_RESET:
1482 asoc->init_err_counter = 0;
1483 asoc->init_cycle = 0;
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04001484 list_for_each_entry(t, &asoc->peer.transport_addr_list,
1485 transports) {
1486 t->init_sent_count = 0;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489
1490 case SCTP_CMD_REPORT_DUP:
1491 sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
1492 cmd->obj.u32);
1493 break;
1494
1495 case SCTP_CMD_REPORT_BAD_TAG:
1496 SCTP_DEBUG_PRINTK("vtag mismatch!\n");
1497 break;
1498
1499 case SCTP_CMD_STRIKE:
1500 /* Mark one strike against a transport. */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001501 sctp_do_8_2_transport_strike(asoc, cmd->obj.transport,
1502 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
1504
Vlad Yasevich7e990132009-03-02 09:46:14 +00001505 case SCTP_CMD_TRANSPORT_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 t = cmd->obj.transport;
Vlad Yasevich7e990132009-03-02 09:46:14 +00001507 sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
1508 break;
1509
1510 case SCTP_CMD_TRANSPORT_HB_SENT:
1511 t = cmd->obj.transport;
1512 sctp_do_8_2_transport_strike(asoc, t, 1);
1513 t->hb_sent = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
1515
1516 case SCTP_CMD_TRANSPORT_ON:
1517 t = cmd->obj.transport;
1518 sctp_cmd_transport_on(commands, asoc, t, chunk);
1519 break;
1520
1521 case SCTP_CMD_HB_TIMERS_START:
1522 sctp_cmd_hb_timers_start(commands, asoc);
1523 break;
1524
1525 case SCTP_CMD_HB_TIMER_UPDATE:
1526 t = cmd->obj.transport;
Gui Jianfeng765ff022008-04-12 18:55:12 -07001527 sctp_cmd_hb_timer_update(commands, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529
1530 case SCTP_CMD_HB_TIMERS_STOP:
1531 sctp_cmd_hb_timers_stop(commands, asoc);
1532 break;
1533
1534 case SCTP_CMD_REPORT_ERROR:
1535 error = cmd->obj.error;
1536 break;
1537
1538 case SCTP_CMD_PROCESS_CTSN:
1539 /* Dummy up a SACK for processing. */
Al Viro2178eda2006-11-20 17:26:53 -08001540 sackh.cum_tsn_ack = cmd->obj.be32;
Vlad Yasevichd4d6fb52009-09-04 18:20:59 -04001541 sackh.a_rwnd = asoc->peer.rwnd +
1542 asoc->outqueue.outstanding_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 sackh.num_gap_ack_blocks = 0;
1544 sackh.num_dup_tsns = 0;
1545 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
1546 SCTP_SACKH(&sackh));
1547 break;
1548
1549 case SCTP_CMD_DISCARD_PACKET:
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001550 /* We need to discard the whole packet.
1551 * Uncork the queue since there might be
1552 * responses pending
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 chunk->pdiscard = 1;
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001555 if (asoc) {
1556 sctp_outq_uncork(&asoc->outqueue);
1557 local_cork = 0;
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
1560
1561 case SCTP_CMD_RTO_PENDING:
1562 t = cmd->obj.transport;
1563 t->rto_pending = 1;
1564 break;
1565
1566 case SCTP_CMD_PART_DELIVER:
1567 sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
1568 GFP_ATOMIC);
1569 break;
1570
1571 case SCTP_CMD_RENEGE:
1572 sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
1573 GFP_ATOMIC);
1574 break;
1575
1576 case SCTP_CMD_SETUP_T4:
1577 sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
1578 break;
1579
1580 case SCTP_CMD_PROCESS_OPERR:
1581 sctp_cmd_process_operr(commands, asoc, chunk);
1582 break;
1583 case SCTP_CMD_CLEAR_INIT_TAG:
1584 asoc->peer.i.init_tag = 0;
1585 break;
1586 case SCTP_CMD_DEL_NON_PRIMARY:
1587 sctp_cmd_del_non_primary(asoc);
1588 break;
1589 case SCTP_CMD_T3_RTX_TIMERS_STOP:
1590 sctp_cmd_t3_rtx_timers_stop(commands, asoc);
1591 break;
1592 case SCTP_CMD_FORCE_PRIM_RETRAN:
1593 t = asoc->peer.retran_path;
1594 asoc->peer.retran_path = asoc->peer.primary_path;
1595 error = sctp_outq_uncork(&asoc->outqueue);
1596 local_cork = 0;
1597 asoc->peer.retran_path = t;
1598 break;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001599 case SCTP_CMD_SET_SK_ERR:
1600 sctp_cmd_set_sk_err(asoc, cmd->obj.error);
1601 break;
Vlad Yasevich07d93962007-05-04 13:55:27 -07001602 case SCTP_CMD_ASSOC_CHANGE:
1603 sctp_cmd_assoc_change(commands, asoc,
1604 cmd->obj.u8);
1605 break;
1606 case SCTP_CMD_ADAPTATION_IND:
1607 sctp_cmd_adaptation_ind(commands, asoc);
1608 break;
1609
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07001610 case SCTP_CMD_ASSOC_SHKEY:
1611 error = sctp_auth_asoc_init_active_key(asoc,
1612 GFP_ATOMIC);
1613 break;
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07001614 case SCTP_CMD_UPDATE_INITTAG:
1615 asoc->peer.i.init_tag = cmd->obj.u32;
1616 break;
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001617 case SCTP_CMD_SEND_MSG:
1618 if (!asoc->outqueue.cork) {
1619 sctp_outq_cork(&asoc->outqueue);
1620 local_cork = 1;
1621 }
1622 error = sctp_cmd_send_msg(asoc, cmd->obj.msg);
1623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 default:
1625 printk(KERN_WARNING "Impossible command: %u, %p\n",
1626 cmd->verb, cmd->obj.ptr);
1627 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001628 }
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (error)
1631 break;
1632 }
1633
1634out:
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001635 /* If this is in response to a received chunk, wait until
1636 * we are done with the packet to open the queue so that we don't
1637 * send multiple packets in response to a single request.
1638 */
1639 if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) {
1640 if (chunk->end_of_packet || chunk->singleton)
Vlad Yasevichd4d6fb52009-09-04 18:20:59 -04001641 error = sctp_outq_uncork(&asoc->outqueue);
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001642 } else if (local_cork)
Vlad Yasevichd4d6fb52009-09-04 18:20:59 -04001643 error = sctp_outq_uncork(&asoc->outqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return error;
1645nomem:
1646 error = -ENOMEM;
1647 goto out;
1648}
1649