sm_sideeffect.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /* SCTP kernel reference 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 reference 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. * The SCTP reference 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. * The SCTP reference 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. #include <linux/skbuff.h>
  50. #include <linux/types.h>
  51. #include <linux/socket.h>
  52. #include <linux/ip.h>
  53. #include <net/sock.h>
  54. #include <net/sctp/sctp.h>
  55. #include <net/sctp/sm.h>
  56. static int sctp_cmd_interpreter(sctp_event_t event_type,
  57. sctp_subtype_t subtype,
  58. sctp_state_t state,
  59. struct sctp_endpoint *ep,
  60. struct sctp_association *asoc,
  61. void *event_arg,
  62. sctp_disposition_t status,
  63. sctp_cmd_seq_t *commands,
  64. unsigned int __nocast gfp);
  65. static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
  66. sctp_state_t state,
  67. struct sctp_endpoint *ep,
  68. struct sctp_association *asoc,
  69. void *event_arg,
  70. sctp_disposition_t status,
  71. sctp_cmd_seq_t *commands,
  72. unsigned int __nocast gfp);
  73. /********************************************************************
  74. * Helper functions
  75. ********************************************************************/
  76. /* A helper function for delayed processing of INET ECN CE bit. */
  77. static void sctp_do_ecn_ce_work(struct sctp_association *asoc,
  78. __u32 lowest_tsn)
  79. {
  80. /* Save the TSN away for comparison when we receive CWR */
  81. asoc->last_ecne_tsn = lowest_tsn;
  82. asoc->need_ecne = 1;
  83. }
  84. /* Helper function for delayed processing of SCTP ECNE chunk. */
  85. /* RFC 2960 Appendix A
  86. *
  87. * RFC 2481 details a specific bit for a sender to send in
  88. * the header of its next outbound TCP segment to indicate to
  89. * its peer that it has reduced its congestion window. This
  90. * is termed the CWR bit. For SCTP the same indication is made
  91. * by including the CWR chunk. This chunk contains one data
  92. * element, i.e. the TSN number that was sent in the ECNE chunk.
  93. * This element represents the lowest TSN number in the datagram
  94. * that was originally marked with the CE bit.
  95. */
  96. static struct sctp_chunk *sctp_do_ecn_ecne_work(struct sctp_association *asoc,
  97. __u32 lowest_tsn,
  98. struct sctp_chunk *chunk)
  99. {
  100. struct sctp_chunk *repl;
  101. /* Our previously transmitted packet ran into some congestion
  102. * so we should take action by reducing cwnd and ssthresh
  103. * and then ACK our peer that we we've done so by
  104. * sending a CWR.
  105. */
  106. /* First, try to determine if we want to actually lower
  107. * our cwnd variables. Only lower them if the ECNE looks more
  108. * recent than the last response.
  109. */
  110. if (TSN_lt(asoc->last_cwr_tsn, lowest_tsn)) {
  111. struct sctp_transport *transport;
  112. /* Find which transport's congestion variables
  113. * need to be adjusted.
  114. */
  115. transport = sctp_assoc_lookup_tsn(asoc, lowest_tsn);
  116. /* Update the congestion variables. */
  117. if (transport)
  118. sctp_transport_lower_cwnd(transport,
  119. SCTP_LOWER_CWND_ECNE);
  120. asoc->last_cwr_tsn = lowest_tsn;
  121. }
  122. /* Always try to quiet the other end. In case of lost CWR,
  123. * resend last_cwr_tsn.
  124. */
  125. repl = sctp_make_cwr(asoc, asoc->last_cwr_tsn, chunk);
  126. /* If we run out of memory, it will look like a lost CWR. We'll
  127. * get back in sync eventually.
  128. */
  129. return repl;
  130. }
  131. /* Helper function to do delayed processing of ECN CWR chunk. */
  132. static void sctp_do_ecn_cwr_work(struct sctp_association *asoc,
  133. __u32 lowest_tsn)
  134. {
  135. /* Turn off ECNE getting auto-prepended to every outgoing
  136. * packet
  137. */
  138. asoc->need_ecne = 0;
  139. }
  140. /* Generate SACK if necessary. We call this at the end of a packet. */
  141. static int sctp_gen_sack(struct sctp_association *asoc, int force,
  142. sctp_cmd_seq_t *commands)
  143. {
  144. __u32 ctsn, max_tsn_seen;
  145. struct sctp_chunk *sack;
  146. int error = 0;
  147. if (force)
  148. asoc->peer.sack_needed = 1;
  149. ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  150. max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
  151. /* From 12.2 Parameters necessary per association (i.e. the TCB):
  152. *
  153. * Ack State : This flag indicates if the next received packet
  154. * : is to be responded to with a SACK. ...
  155. * : When DATA chunks are out of order, SACK's
  156. * : are not delayed (see Section 6).
  157. *
  158. * [This is actually not mentioned in Section 6, but we
  159. * implement it here anyway. --piggy]
  160. */
  161. if (max_tsn_seen != ctsn)
  162. asoc->peer.sack_needed = 1;
  163. /* From 6.2 Acknowledgement on Reception of DATA Chunks:
  164. *
  165. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically,
  166. * an acknowledgement SHOULD be generated for at least every
  167. * second packet (not every second DATA chunk) received, and
  168. * SHOULD be generated within 200 ms of the arrival of any
  169. * unacknowledged DATA chunk. ...
  170. */
  171. if (!asoc->peer.sack_needed) {
  172. /* We will need a SACK for the next packet. */
  173. asoc->peer.sack_needed = 1;
  174. goto out;
  175. } else {
  176. if (asoc->a_rwnd > asoc->rwnd)
  177. asoc->a_rwnd = asoc->rwnd;
  178. sack = sctp_make_sack(asoc);
  179. if (!sack)
  180. goto nomem;
  181. asoc->peer.sack_needed = 0;
  182. error = sctp_outq_tail(&asoc->outqueue, sack);
  183. /* Stop the SACK timer. */
  184. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  185. SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
  186. }
  187. out:
  188. return error;
  189. nomem:
  190. error = -ENOMEM;
  191. return error;
  192. }
  193. /* When the T3-RTX timer expires, it calls this function to create the
  194. * relevant state machine event.
  195. */
  196. void sctp_generate_t3_rtx_event(unsigned long peer)
  197. {
  198. int error;
  199. struct sctp_transport *transport = (struct sctp_transport *) peer;
  200. struct sctp_association *asoc = transport->asoc;
  201. /* Check whether a task is in the sock. */
  202. sctp_bh_lock_sock(asoc->base.sk);
  203. if (sock_owned_by_user(asoc->base.sk)) {
  204. SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __FUNCTION__);
  205. /* Try again later. */
  206. if (!mod_timer(&transport->T3_rtx_timer, jiffies + (HZ/20)))
  207. sctp_transport_hold(transport);
  208. goto out_unlock;
  209. }
  210. /* Is this transport really dead and just waiting around for
  211. * the timer to let go of the reference?
  212. */
  213. if (transport->dead)
  214. goto out_unlock;
  215. /* Run through the state machine. */
  216. error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  217. SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_T3_RTX),
  218. asoc->state,
  219. asoc->ep, asoc,
  220. transport, GFP_ATOMIC);
  221. if (error)
  222. asoc->base.sk->sk_err = -error;
  223. out_unlock:
  224. sctp_bh_unlock_sock(asoc->base.sk);
  225. sctp_transport_put(transport);
  226. }
  227. /* This is a sa interface for producing timeout events. It works
  228. * for timeouts which use the association as their parameter.
  229. */
  230. static void sctp_generate_timeout_event(struct sctp_association *asoc,
  231. sctp_event_timeout_t timeout_type)
  232. {
  233. int error = 0;
  234. sctp_bh_lock_sock(asoc->base.sk);
  235. if (sock_owned_by_user(asoc->base.sk)) {
  236. SCTP_DEBUG_PRINTK("%s:Sock is busy: timer %d\n",
  237. __FUNCTION__,
  238. timeout_type);
  239. /* Try again later. */
  240. if (!mod_timer(&asoc->timers[timeout_type], jiffies + (HZ/20)))
  241. sctp_association_hold(asoc);
  242. goto out_unlock;
  243. }
  244. /* Is this association really dead and just waiting around for
  245. * the timer to let go of the reference?
  246. */
  247. if (asoc->base.dead)
  248. goto out_unlock;
  249. /* Run through the state machine. */
  250. error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  251. SCTP_ST_TIMEOUT(timeout_type),
  252. asoc->state, asoc->ep, asoc,
  253. (void *)timeout_type, GFP_ATOMIC);
  254. if (error)
  255. asoc->base.sk->sk_err = -error;
  256. out_unlock:
  257. sctp_bh_unlock_sock(asoc->base.sk);
  258. sctp_association_put(asoc);
  259. }
  260. static void sctp_generate_t1_cookie_event(unsigned long data)
  261. {
  262. struct sctp_association *asoc = (struct sctp_association *) data;
  263. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_COOKIE);
  264. }
  265. static void sctp_generate_t1_init_event(unsigned long data)
  266. {
  267. struct sctp_association *asoc = (struct sctp_association *) data;
  268. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T1_INIT);
  269. }
  270. static void sctp_generate_t2_shutdown_event(unsigned long data)
  271. {
  272. struct sctp_association *asoc = (struct sctp_association *) data;
  273. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T2_SHUTDOWN);
  274. }
  275. static void sctp_generate_t4_rto_event(unsigned long data)
  276. {
  277. struct sctp_association *asoc = (struct sctp_association *) data;
  278. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_T4_RTO);
  279. }
  280. static void sctp_generate_t5_shutdown_guard_event(unsigned long data)
  281. {
  282. struct sctp_association *asoc = (struct sctp_association *)data;
  283. sctp_generate_timeout_event(asoc,
  284. SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD);
  285. } /* sctp_generate_t5_shutdown_guard_event() */
  286. static void sctp_generate_autoclose_event(unsigned long data)
  287. {
  288. struct sctp_association *asoc = (struct sctp_association *) data;
  289. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_AUTOCLOSE);
  290. }
  291. /* Generate a heart beat event. If the sock is busy, reschedule. Make
  292. * sure that the transport is still valid.
  293. */
  294. void sctp_generate_heartbeat_event(unsigned long data)
  295. {
  296. int error = 0;
  297. struct sctp_transport *transport = (struct sctp_transport *) data;
  298. struct sctp_association *asoc = transport->asoc;
  299. sctp_bh_lock_sock(asoc->base.sk);
  300. if (sock_owned_by_user(asoc->base.sk)) {
  301. SCTP_DEBUG_PRINTK("%s:Sock is busy.\n", __FUNCTION__);
  302. /* Try again later. */
  303. if (!mod_timer(&transport->hb_timer, jiffies + (HZ/20)))
  304. sctp_transport_hold(transport);
  305. goto out_unlock;
  306. }
  307. /* Is this structure just waiting around for us to actually
  308. * get destroyed?
  309. */
  310. if (transport->dead)
  311. goto out_unlock;
  312. error = sctp_do_sm(SCTP_EVENT_T_TIMEOUT,
  313. SCTP_ST_TIMEOUT(SCTP_EVENT_TIMEOUT_HEARTBEAT),
  314. asoc->state, asoc->ep, asoc,
  315. transport, GFP_ATOMIC);
  316. if (error)
  317. asoc->base.sk->sk_err = -error;
  318. out_unlock:
  319. sctp_bh_unlock_sock(asoc->base.sk);
  320. sctp_transport_put(transport);
  321. }
  322. /* Inject a SACK Timeout event into the state machine. */
  323. static void sctp_generate_sack_event(unsigned long data)
  324. {
  325. struct sctp_association *asoc = (struct sctp_association *) data;
  326. sctp_generate_timeout_event(asoc, SCTP_EVENT_TIMEOUT_SACK);
  327. }
  328. sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES] = {
  329. NULL,
  330. sctp_generate_t1_cookie_event,
  331. sctp_generate_t1_init_event,
  332. sctp_generate_t2_shutdown_event,
  333. NULL,
  334. sctp_generate_t4_rto_event,
  335. sctp_generate_t5_shutdown_guard_event,
  336. sctp_generate_heartbeat_event,
  337. sctp_generate_sack_event,
  338. sctp_generate_autoclose_event,
  339. };
  340. /* RFC 2960 8.2 Path Failure Detection
  341. *
  342. * When its peer endpoint is multi-homed, an endpoint should keep a
  343. * error counter for each of the destination transport addresses of the
  344. * peer endpoint.
  345. *
  346. * Each time the T3-rtx timer expires on any address, or when a
  347. * HEARTBEAT sent to an idle address is not acknowledged within a RTO,
  348. * the error counter of that destination address will be incremented.
  349. * When the value in the error counter exceeds the protocol parameter
  350. * 'Path.Max.Retrans' of that destination address, the endpoint should
  351. * mark the destination transport address as inactive, and a
  352. * notification SHOULD be sent to the upper layer.
  353. *
  354. */
  355. static void sctp_do_8_2_transport_strike(struct sctp_association *asoc,
  356. struct sctp_transport *transport)
  357. {
  358. /* The check for association's overall error counter exceeding the
  359. * threshold is done in the state function.
  360. */
  361. asoc->overall_error_count++;
  362. if (transport->state != SCTP_INACTIVE &&
  363. (transport->error_count++ >= transport->max_retrans)) {
  364. SCTP_DEBUG_PRINTK_IPADDR("transport_strike:association %p",
  365. " transport IP: port:%d failed.\n",
  366. asoc,
  367. (&transport->ipaddr),
  368. transport->ipaddr.v4.sin_port);
  369. sctp_assoc_control_transport(asoc, transport,
  370. SCTP_TRANSPORT_DOWN,
  371. SCTP_FAILED_THRESHOLD);
  372. }
  373. /* E2) For the destination address for which the timer
  374. * expires, set RTO <- RTO * 2 ("back off the timer"). The
  375. * maximum value discussed in rule C7 above (RTO.max) may be
  376. * used to provide an upper bound to this doubling operation.
  377. */
  378. transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
  379. }
  380. /* Worker routine to handle INIT command failure. */
  381. static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
  382. struct sctp_association *asoc,
  383. unsigned error)
  384. {
  385. struct sctp_ulpevent *event;
  386. event = sctp_ulpevent_make_assoc_change(asoc,0, SCTP_CANT_STR_ASSOC,
  387. (__u16)error, 0, 0,
  388. GFP_ATOMIC);
  389. if (event)
  390. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  391. SCTP_ULPEVENT(event));
  392. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  393. SCTP_STATE(SCTP_STATE_CLOSED));
  394. /* SEND_FAILED sent later when cleaning up the association. */
  395. asoc->outqueue.error = error;
  396. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  397. }
  398. /* Worker routine to handle SCTP_CMD_ASSOC_FAILED. */
  399. static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
  400. struct sctp_association *asoc,
  401. sctp_event_t event_type,
  402. sctp_subtype_t subtype,
  403. struct sctp_chunk *chunk,
  404. unsigned error)
  405. {
  406. struct sctp_ulpevent *event;
  407. /* Cancel any partial delivery in progress. */
  408. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  409. event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
  410. (__u16)error, 0, 0,
  411. GFP_ATOMIC);
  412. if (event)
  413. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  414. SCTP_ULPEVENT(event));
  415. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  416. SCTP_STATE(SCTP_STATE_CLOSED));
  417. /* Set sk_err to ECONNRESET on a 1-1 style socket. */
  418. if (!sctp_style(asoc->base.sk, UDP))
  419. asoc->base.sk->sk_err = ECONNRESET;
  420. /* SEND_FAILED sent later when cleaning up the association. */
  421. asoc->outqueue.error = error;
  422. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  423. }
  424. /* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
  425. * inside the cookie. In reality, this is only used for INIT-ACK processing
  426. * since all other cases use "temporary" associations and can do all
  427. * their work in statefuns directly.
  428. */
  429. static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
  430. struct sctp_association *asoc,
  431. struct sctp_chunk *chunk,
  432. sctp_init_chunk_t *peer_init,
  433. unsigned int __nocast gfp)
  434. {
  435. int error;
  436. /* We only process the init as a sideeffect in a single
  437. * case. This is when we process the INIT-ACK. If we
  438. * fail during INIT processing (due to malloc problems),
  439. * just return the error and stop processing the stack.
  440. */
  441. if (!sctp_process_init(asoc, chunk->chunk_hdr->type,
  442. sctp_source(chunk), peer_init, gfp))
  443. error = -ENOMEM;
  444. else
  445. error = 0;
  446. return error;
  447. }
  448. /* Helper function to break out starting up of heartbeat timers. */
  449. static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
  450. struct sctp_association *asoc)
  451. {
  452. struct sctp_transport *t;
  453. struct list_head *pos;
  454. /* Start a heartbeat timer for each transport on the association.
  455. * hold a reference on the transport to make sure none of
  456. * the needed data structures go away.
  457. */
  458. list_for_each(pos, &asoc->peer.transport_addr_list) {
  459. t = list_entry(pos, struct sctp_transport, transports);
  460. if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  461. sctp_transport_hold(t);
  462. }
  463. }
  464. static void sctp_cmd_hb_timers_stop(sctp_cmd_seq_t *cmds,
  465. struct sctp_association *asoc)
  466. {
  467. struct sctp_transport *t;
  468. struct list_head *pos;
  469. /* Stop all heartbeat timers. */
  470. list_for_each(pos, &asoc->peer.transport_addr_list) {
  471. t = list_entry(pos, struct sctp_transport, transports);
  472. if (del_timer(&t->hb_timer))
  473. sctp_transport_put(t);
  474. }
  475. }
  476. /* Helper function to stop any pending T3-RTX timers */
  477. static void sctp_cmd_t3_rtx_timers_stop(sctp_cmd_seq_t *cmds,
  478. struct sctp_association *asoc)
  479. {
  480. struct sctp_transport *t;
  481. struct list_head *pos;
  482. list_for_each(pos, &asoc->peer.transport_addr_list) {
  483. t = list_entry(pos, struct sctp_transport, transports);
  484. if (timer_pending(&t->T3_rtx_timer) &&
  485. del_timer(&t->T3_rtx_timer)) {
  486. sctp_transport_put(t);
  487. }
  488. }
  489. }
  490. /* Helper function to update the heartbeat timer. */
  491. static void sctp_cmd_hb_timer_update(sctp_cmd_seq_t *cmds,
  492. struct sctp_association *asoc,
  493. struct sctp_transport *t)
  494. {
  495. /* Update the heartbeat timer. */
  496. if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  497. sctp_transport_hold(t);
  498. }
  499. /* Helper function to handle the reception of an HEARTBEAT ACK. */
  500. static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
  501. struct sctp_association *asoc,
  502. struct sctp_transport *t,
  503. struct sctp_chunk *chunk)
  504. {
  505. sctp_sender_hb_info_t *hbinfo;
  506. /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the
  507. * HEARTBEAT should clear the error counter of the destination
  508. * transport address to which the HEARTBEAT was sent.
  509. * The association's overall error count is also cleared.
  510. */
  511. t->error_count = 0;
  512. t->asoc->overall_error_count = 0;
  513. /* Mark the destination transport address as active if it is not so
  514. * marked.
  515. */
  516. if (t->state == SCTP_INACTIVE)
  517. sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
  518. SCTP_HEARTBEAT_SUCCESS);
  519. /* The receiver of the HEARTBEAT ACK should also perform an
  520. * RTT measurement for that destination transport address
  521. * using the time value carried in the HEARTBEAT ACK chunk.
  522. */
  523. hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
  524. sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
  525. }
  526. /* Helper function to do a transport reset at the expiry of the hearbeat
  527. * timer.
  528. */
  529. static void sctp_cmd_transport_reset(sctp_cmd_seq_t *cmds,
  530. struct sctp_association *asoc,
  531. struct sctp_transport *t)
  532. {
  533. sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
  534. /* Mark one strike against a transport. */
  535. sctp_do_8_2_transport_strike(asoc, t);
  536. }
  537. /* Helper function to process the process SACK command. */
  538. static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
  539. struct sctp_association *asoc,
  540. struct sctp_sackhdr *sackh)
  541. {
  542. int err;
  543. if (sctp_outq_sack(&asoc->outqueue, sackh)) {
  544. /* There are no more TSNs awaiting SACK. */
  545. err = sctp_do_sm(SCTP_EVENT_T_OTHER,
  546. SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
  547. asoc->state, asoc->ep, asoc, NULL,
  548. GFP_ATOMIC);
  549. } else {
  550. /* Windows may have opened, so we need
  551. * to check if we have DATA to transmit
  552. */
  553. err = sctp_outq_flush(&asoc->outqueue, 0);
  554. }
  555. return err;
  556. }
  557. /* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
  558. * the transport for a shutdown chunk.
  559. */
  560. static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds,
  561. struct sctp_association *asoc,
  562. struct sctp_chunk *chunk)
  563. {
  564. struct sctp_transport *t;
  565. t = sctp_assoc_choose_shutdown_transport(asoc);
  566. asoc->shutdown_last_sent_to = t;
  567. asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
  568. chunk->transport = t;
  569. }
  570. /* Helper function to change the state of an association. */
  571. static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds,
  572. struct sctp_association *asoc,
  573. sctp_state_t state)
  574. {
  575. struct sock *sk = asoc->base.sk;
  576. asoc->state = state;
  577. SCTP_DEBUG_PRINTK("sctp_cmd_new_state: asoc %p[%s]\n",
  578. asoc, sctp_state_tbl[state]);
  579. if (sctp_style(sk, TCP)) {
  580. /* Change the sk->sk_state of a TCP-style socket that has
  581. * sucessfully completed a connect() call.
  582. */
  583. if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED))
  584. sk->sk_state = SCTP_SS_ESTABLISHED;
  585. /* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */
  586. if (sctp_state(asoc, SHUTDOWN_RECEIVED) &&
  587. sctp_sstate(sk, ESTABLISHED))
  588. sk->sk_shutdown |= RCV_SHUTDOWN;
  589. }
  590. if (sctp_state(asoc, COOKIE_WAIT)) {
  591. /* Reset init timeouts since they may have been
  592. * increased due to timer expirations.
  593. */
  594. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
  595. asoc->ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT];
  596. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
  597. asoc->ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE];
  598. }
  599. if (sctp_state(asoc, ESTABLISHED) ||
  600. sctp_state(asoc, CLOSED) ||
  601. sctp_state(asoc, SHUTDOWN_RECEIVED)) {
  602. /* Wake up any processes waiting in the asoc's wait queue in
  603. * sctp_wait_for_connect() or sctp_wait_for_sndbuf().
  604. */
  605. if (waitqueue_active(&asoc->wait))
  606. wake_up_interruptible(&asoc->wait);
  607. /* Wake up any processes waiting in the sk's sleep queue of
  608. * a TCP-style or UDP-style peeled-off socket in
  609. * sctp_wait_for_accept() or sctp_wait_for_packet().
  610. * For a UDP-style socket, the waiters are woken up by the
  611. * notifications.
  612. */
  613. if (!sctp_style(sk, UDP))
  614. sk->sk_state_change(sk);
  615. }
  616. }
  617. /* Helper function to delete an association. */
  618. static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
  619. struct sctp_association *asoc)
  620. {
  621. struct sock *sk = asoc->base.sk;
  622. /* If it is a non-temporary association belonging to a TCP-style
  623. * listening socket that is not closed, do not free it so that accept()
  624. * can pick it up later.
  625. */
  626. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
  627. (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
  628. return;
  629. sctp_unhash_established(asoc);
  630. sctp_association_free(asoc);
  631. }
  632. /*
  633. * ADDIP Section 4.1 ASCONF Chunk Procedures
  634. * A4) Start a T-4 RTO timer, using the RTO value of the selected
  635. * destination address (we use active path instead of primary path just
  636. * because primary path may be inactive.
  637. */
  638. static void sctp_cmd_setup_t4(sctp_cmd_seq_t *cmds,
  639. struct sctp_association *asoc,
  640. struct sctp_chunk *chunk)
  641. {
  642. struct sctp_transport *t;
  643. t = asoc->peer.active_path;
  644. asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
  645. chunk->transport = t;
  646. }
  647. /* Process an incoming Operation Error Chunk. */
  648. static void sctp_cmd_process_operr(sctp_cmd_seq_t *cmds,
  649. struct sctp_association *asoc,
  650. struct sctp_chunk *chunk)
  651. {
  652. struct sctp_operr_chunk *operr_chunk;
  653. struct sctp_errhdr *err_hdr;
  654. operr_chunk = (struct sctp_operr_chunk *)chunk->chunk_hdr;
  655. err_hdr = &operr_chunk->err_hdr;
  656. switch (err_hdr->cause) {
  657. case SCTP_ERROR_UNKNOWN_CHUNK:
  658. {
  659. struct sctp_chunkhdr *unk_chunk_hdr;
  660. unk_chunk_hdr = (struct sctp_chunkhdr *)err_hdr->variable;
  661. switch (unk_chunk_hdr->type) {
  662. /* ADDIP 4.1 A9) If the peer responds to an ASCONF with an
  663. * ERROR chunk reporting that it did not recognized the ASCONF
  664. * chunk type, the sender of the ASCONF MUST NOT send any
  665. * further ASCONF chunks and MUST stop its T-4 timer.
  666. */
  667. case SCTP_CID_ASCONF:
  668. asoc->peer.asconf_capable = 0;
  669. sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
  670. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  671. break;
  672. default:
  673. break;
  674. }
  675. break;
  676. }
  677. default:
  678. break;
  679. }
  680. }
  681. /* Process variable FWDTSN chunk information. */
  682. static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
  683. struct sctp_chunk *chunk)
  684. {
  685. struct sctp_fwdtsn_skip *skip;
  686. /* Walk through all the skipped SSNs */
  687. sctp_walk_fwdtsn(skip, chunk) {
  688. sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
  689. }
  690. return;
  691. }
  692. /* Helper function to remove the association non-primary peer
  693. * transports.
  694. */
  695. static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
  696. {
  697. struct sctp_transport *t;
  698. struct list_head *pos;
  699. struct list_head *temp;
  700. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  701. t = list_entry(pos, struct sctp_transport, transports);
  702. if (!sctp_cmp_addr_exact(&t->ipaddr,
  703. &asoc->peer.primary_addr)) {
  704. sctp_assoc_del_peer(asoc, &t->ipaddr);
  705. }
  706. }
  707. return;
  708. }
  709. /* These three macros allow us to pull the debugging code out of the
  710. * main flow of sctp_do_sm() to keep attention focused on the real
  711. * functionality there.
  712. */
  713. #define DEBUG_PRE \
  714. SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
  715. "ep %p, %s, %s, asoc %p[%s], %s\n", \
  716. ep, sctp_evttype_tbl[event_type], \
  717. (*debug_fn)(subtype), asoc, \
  718. sctp_state_tbl[state], state_fn->name)
  719. #define DEBUG_POST \
  720. SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
  721. "asoc %p, status: %s\n", \
  722. asoc, sctp_status_tbl[status])
  723. #define DEBUG_POST_SFX \
  724. SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
  725. error, asoc, \
  726. sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
  727. sctp_assoc2id(asoc)))?asoc->state:SCTP_STATE_CLOSED])
  728. /*
  729. * This is the master state machine processing function.
  730. *
  731. * If you want to understand all of lksctp, this is a
  732. * good place to start.
  733. */
  734. int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
  735. sctp_state_t state,
  736. struct sctp_endpoint *ep,
  737. struct sctp_association *asoc,
  738. void *event_arg,
  739. unsigned int __nocast gfp)
  740. {
  741. sctp_cmd_seq_t commands;
  742. const sctp_sm_table_entry_t *state_fn;
  743. sctp_disposition_t status;
  744. int error = 0;
  745. typedef const char *(printfn_t)(sctp_subtype_t);
  746. static printfn_t *table[] = {
  747. NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
  748. };
  749. printfn_t *debug_fn __attribute__ ((unused)) = table[event_type];
  750. /* Look up the state function, run it, and then process the
  751. * side effects. These three steps are the heart of lksctp.
  752. */
  753. state_fn = sctp_sm_lookup_event(event_type, state, subtype);
  754. sctp_init_cmd_seq(&commands);
  755. DEBUG_PRE;
  756. status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
  757. DEBUG_POST;
  758. error = sctp_side_effects(event_type, subtype, state,
  759. ep, asoc, event_arg, status,
  760. &commands, gfp);
  761. DEBUG_POST_SFX;
  762. return error;
  763. }
  764. #undef DEBUG_PRE
  765. #undef DEBUG_POST
  766. /*****************************************************************
  767. * This the master state function side effect processing function.
  768. *****************************************************************/
  769. static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
  770. sctp_state_t state,
  771. struct sctp_endpoint *ep,
  772. struct sctp_association *asoc,
  773. void *event_arg,
  774. sctp_disposition_t status,
  775. sctp_cmd_seq_t *commands,
  776. unsigned int __nocast gfp)
  777. {
  778. int error;
  779. /* FIXME - Most of the dispositions left today would be categorized
  780. * as "exceptional" dispositions. For those dispositions, it
  781. * may not be proper to run through any of the commands at all.
  782. * For example, the command interpreter might be run only with
  783. * disposition SCTP_DISPOSITION_CONSUME.
  784. */
  785. if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
  786. ep, asoc,
  787. event_arg, status,
  788. commands, gfp)))
  789. goto bail;
  790. switch (status) {
  791. case SCTP_DISPOSITION_DISCARD:
  792. SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
  793. "event_type %d, event_id %d\n",
  794. state, event_type, subtype.chunk);
  795. break;
  796. case SCTP_DISPOSITION_NOMEM:
  797. /* We ran out of memory, so we need to discard this
  798. * packet.
  799. */
  800. /* BUG--we should now recover some memory, probably by
  801. * reneging...
  802. */
  803. error = -ENOMEM;
  804. break;
  805. case SCTP_DISPOSITION_DELETE_TCB:
  806. /* This should now be a command. */
  807. break;
  808. case SCTP_DISPOSITION_CONSUME:
  809. case SCTP_DISPOSITION_ABORT:
  810. /*
  811. * We should no longer have much work to do here as the
  812. * real work has been done as explicit commands above.
  813. */
  814. break;
  815. case SCTP_DISPOSITION_VIOLATION:
  816. printk(KERN_ERR "sctp protocol violation state %d "
  817. "chunkid %d\n", state, subtype.chunk);
  818. break;
  819. case SCTP_DISPOSITION_NOT_IMPL:
  820. printk(KERN_WARNING "sctp unimplemented feature in state %d, "
  821. "event_type %d, event_id %d\n",
  822. state, event_type, subtype.chunk);
  823. break;
  824. case SCTP_DISPOSITION_BUG:
  825. printk(KERN_ERR "sctp bug in state %d, "
  826. "event_type %d, event_id %d\n",
  827. state, event_type, subtype.chunk);
  828. BUG();
  829. break;
  830. default:
  831. printk(KERN_ERR "sctp impossible disposition %d "
  832. "in state %d, event_type %d, event_id %d\n",
  833. status, state, event_type, subtype.chunk);
  834. BUG();
  835. break;
  836. };
  837. bail:
  838. return error;
  839. }
  840. /********************************************************************
  841. * 2nd Level Abstractions
  842. ********************************************************************/
  843. /* This is the side-effect interpreter. */
  844. static int sctp_cmd_interpreter(sctp_event_t event_type,
  845. sctp_subtype_t subtype,
  846. sctp_state_t state,
  847. struct sctp_endpoint *ep,
  848. struct sctp_association *asoc,
  849. void *event_arg,
  850. sctp_disposition_t status,
  851. sctp_cmd_seq_t *commands,
  852. unsigned int __nocast gfp)
  853. {
  854. int error = 0;
  855. int force;
  856. sctp_cmd_t *cmd;
  857. struct sctp_chunk *new_obj;
  858. struct sctp_chunk *chunk = NULL;
  859. struct sctp_packet *packet;
  860. struct list_head *pos;
  861. struct timer_list *timer;
  862. unsigned long timeout;
  863. struct sctp_transport *t;
  864. struct sctp_sackhdr sackh;
  865. int local_cork = 0;
  866. if (SCTP_EVENT_T_TIMEOUT != event_type)
  867. chunk = (struct sctp_chunk *) event_arg;
  868. /* Note: This whole file is a huge candidate for rework.
  869. * For example, each command could either have its own handler, so
  870. * the loop would look like:
  871. * while (cmds)
  872. * cmd->handle(x, y, z)
  873. * --jgrimm
  874. */
  875. while (NULL != (cmd = sctp_next_cmd(commands))) {
  876. switch (cmd->verb) {
  877. case SCTP_CMD_NOP:
  878. /* Do nothing. */
  879. break;
  880. case SCTP_CMD_NEW_ASOC:
  881. /* Register a new association. */
  882. if (local_cork) {
  883. sctp_outq_uncork(&asoc->outqueue);
  884. local_cork = 0;
  885. }
  886. asoc = cmd->obj.ptr;
  887. /* Register with the endpoint. */
  888. sctp_endpoint_add_asoc(ep, asoc);
  889. sctp_hash_established(asoc);
  890. break;
  891. case SCTP_CMD_UPDATE_ASSOC:
  892. sctp_assoc_update(asoc, cmd->obj.ptr);
  893. break;
  894. case SCTP_CMD_PURGE_OUTQUEUE:
  895. sctp_outq_teardown(&asoc->outqueue);
  896. break;
  897. case SCTP_CMD_DELETE_TCB:
  898. if (local_cork) {
  899. sctp_outq_uncork(&asoc->outqueue);
  900. local_cork = 0;
  901. }
  902. /* Delete the current association. */
  903. sctp_cmd_delete_tcb(commands, asoc);
  904. asoc = NULL;
  905. break;
  906. case SCTP_CMD_NEW_STATE:
  907. /* Enter a new state. */
  908. sctp_cmd_new_state(commands, asoc, cmd->obj.state);
  909. break;
  910. case SCTP_CMD_REPORT_TSN:
  911. /* Record the arrival of a TSN. */
  912. sctp_tsnmap_mark(&asoc->peer.tsn_map, cmd->obj.u32);
  913. break;
  914. case SCTP_CMD_REPORT_FWDTSN:
  915. /* Move the Cumulattive TSN Ack ahead. */
  916. sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
  917. /* Abort any in progress partial delivery. */
  918. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  919. break;
  920. case SCTP_CMD_PROCESS_FWDTSN:
  921. sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
  922. break;
  923. case SCTP_CMD_GEN_SACK:
  924. /* Generate a Selective ACK.
  925. * The argument tells us whether to just count
  926. * the packet and MAYBE generate a SACK, or
  927. * force a SACK out.
  928. */
  929. force = cmd->obj.i32;
  930. error = sctp_gen_sack(asoc, force, commands);
  931. break;
  932. case SCTP_CMD_PROCESS_SACK:
  933. /* Process an inbound SACK. */
  934. error = sctp_cmd_process_sack(commands, asoc,
  935. cmd->obj.ptr);
  936. break;
  937. case SCTP_CMD_GEN_INIT_ACK:
  938. /* Generate an INIT ACK chunk. */
  939. new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
  940. 0);
  941. if (!new_obj)
  942. goto nomem;
  943. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  944. SCTP_CHUNK(new_obj));
  945. break;
  946. case SCTP_CMD_PEER_INIT:
  947. /* Process a unified INIT from the peer.
  948. * Note: Only used during INIT-ACK processing. If
  949. * there is an error just return to the outter
  950. * layer which will bail.
  951. */
  952. error = sctp_cmd_process_init(commands, asoc, chunk,
  953. cmd->obj.ptr, gfp);
  954. break;
  955. case SCTP_CMD_GEN_COOKIE_ECHO:
  956. /* Generate a COOKIE ECHO chunk. */
  957. new_obj = sctp_make_cookie_echo(asoc, chunk);
  958. if (!new_obj) {
  959. if (cmd->obj.ptr)
  960. sctp_chunk_free(cmd->obj.ptr);
  961. goto nomem;
  962. }
  963. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  964. SCTP_CHUNK(new_obj));
  965. /* If there is an ERROR chunk to be sent along with
  966. * the COOKIE_ECHO, send it, too.
  967. */
  968. if (cmd->obj.ptr)
  969. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  970. SCTP_CHUNK(cmd->obj.ptr));
  971. /* FIXME - Eventually come up with a cleaner way to
  972. * enabling COOKIE-ECHO + DATA bundling during
  973. * multihoming stale cookie scenarios, the following
  974. * command plays with asoc->peer.retran_path to
  975. * avoid the problem of sending the COOKIE-ECHO and
  976. * DATA in different paths, which could result
  977. * in the association being ABORTed if the DATA chunk
  978. * is processed first by the server. Checking the
  979. * init error counter simply causes this command
  980. * to be executed only during failed attempts of
  981. * association establishment.
  982. */
  983. if ((asoc->peer.retran_path !=
  984. asoc->peer.primary_path) &&
  985. (asoc->init_err_counter > 0)) {
  986. sctp_add_cmd_sf(commands,
  987. SCTP_CMD_FORCE_PRIM_RETRAN,
  988. SCTP_NULL());
  989. }
  990. break;
  991. case SCTP_CMD_GEN_SHUTDOWN:
  992. /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
  993. * Reset error counts.
  994. */
  995. asoc->overall_error_count = 0;
  996. /* Generate a SHUTDOWN chunk. */
  997. new_obj = sctp_make_shutdown(asoc, chunk);
  998. if (!new_obj)
  999. goto nomem;
  1000. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1001. SCTP_CHUNK(new_obj));
  1002. break;
  1003. case SCTP_CMD_CHUNK_ULP:
  1004. /* Send a chunk to the sockets layer. */
  1005. SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
  1006. "chunk_up:", cmd->obj.ptr,
  1007. "ulpq:", &asoc->ulpq);
  1008. sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
  1009. GFP_ATOMIC);
  1010. break;
  1011. case SCTP_CMD_EVENT_ULP:
  1012. /* Send a notification to the sockets layer. */
  1013. SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
  1014. "event_up:",cmd->obj.ptr,
  1015. "ulpq:",&asoc->ulpq);
  1016. sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
  1017. break;
  1018. case SCTP_CMD_REPLY:
  1019. /* If an caller has not already corked, do cork. */
  1020. if (!asoc->outqueue.cork) {
  1021. sctp_outq_cork(&asoc->outqueue);
  1022. local_cork = 1;
  1023. }
  1024. /* Send a chunk to our peer. */
  1025. error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
  1026. break;
  1027. case SCTP_CMD_SEND_PKT:
  1028. /* Send a full packet to our peer. */
  1029. packet = cmd->obj.ptr;
  1030. sctp_packet_transmit(packet);
  1031. sctp_ootb_pkt_free(packet);
  1032. break;
  1033. case SCTP_CMD_RETRAN:
  1034. /* Mark a transport for retransmission. */
  1035. sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  1036. SCTP_RTXR_T3_RTX);
  1037. break;
  1038. case SCTP_CMD_TRANSMIT:
  1039. /* Kick start transmission. */
  1040. error = sctp_outq_uncork(&asoc->outqueue);
  1041. local_cork = 0;
  1042. break;
  1043. case SCTP_CMD_ECN_CE:
  1044. /* Do delayed CE processing. */
  1045. sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
  1046. break;
  1047. case SCTP_CMD_ECN_ECNE:
  1048. /* Do delayed ECNE processing. */
  1049. new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
  1050. chunk);
  1051. if (new_obj)
  1052. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1053. SCTP_CHUNK(new_obj));
  1054. break;
  1055. case SCTP_CMD_ECN_CWR:
  1056. /* Do delayed CWR processing. */
  1057. sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
  1058. break;
  1059. case SCTP_CMD_SETUP_T2:
  1060. sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
  1061. break;
  1062. case SCTP_CMD_TIMER_START:
  1063. timer = &asoc->timers[cmd->obj.to];
  1064. timeout = asoc->timeouts[cmd->obj.to];
  1065. if (!timeout)
  1066. BUG();
  1067. timer->expires = jiffies + timeout;
  1068. sctp_association_hold(asoc);
  1069. add_timer(timer);
  1070. break;
  1071. case SCTP_CMD_TIMER_RESTART:
  1072. timer = &asoc->timers[cmd->obj.to];
  1073. timeout = asoc->timeouts[cmd->obj.to];
  1074. if (!mod_timer(timer, jiffies + timeout))
  1075. sctp_association_hold(asoc);
  1076. break;
  1077. case SCTP_CMD_TIMER_STOP:
  1078. timer = &asoc->timers[cmd->obj.to];
  1079. if (timer_pending(timer) && del_timer(timer))
  1080. sctp_association_put(asoc);
  1081. break;
  1082. case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
  1083. chunk = cmd->obj.ptr;
  1084. t = sctp_assoc_choose_init_transport(asoc);
  1085. asoc->init_last_sent_to = t;
  1086. chunk->transport = t;
  1087. t->init_sent_count++;
  1088. break;
  1089. case SCTP_CMD_INIT_RESTART:
  1090. /* Do the needed accounting and updates
  1091. * associated with restarting an initialization
  1092. * timer. Only multiply the timeout by two if
  1093. * all transports have been tried at the current
  1094. * timeout.
  1095. */
  1096. t = asoc->init_last_sent_to;
  1097. asoc->init_err_counter++;
  1098. if (t->init_sent_count > (asoc->init_cycle + 1)) {
  1099. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] *= 2;
  1100. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] >
  1101. asoc->max_init_timeo) {
  1102. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
  1103. asoc->max_init_timeo;
  1104. }
  1105. asoc->init_cycle++;
  1106. SCTP_DEBUG_PRINTK(
  1107. "T1 INIT Timeout adjustment"
  1108. " init_err_counter: %d"
  1109. " cycle: %d"
  1110. " timeout: %d\n",
  1111. asoc->init_err_counter,
  1112. asoc->init_cycle,
  1113. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT]);
  1114. }
  1115. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  1116. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  1117. break;
  1118. case SCTP_CMD_COOKIEECHO_RESTART:
  1119. /* Do the needed accounting and updates
  1120. * associated with restarting an initialization
  1121. * timer. Only multiply the timeout by two if
  1122. * all transports have been tried at the current
  1123. * timeout.
  1124. */
  1125. asoc->init_err_counter++;
  1126. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] *= 2;
  1127. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] >
  1128. asoc->max_init_timeo) {
  1129. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
  1130. asoc->max_init_timeo;
  1131. }
  1132. SCTP_DEBUG_PRINTK(
  1133. "T1 COOKIE Timeout adjustment"
  1134. " init_err_counter: %d"
  1135. " timeout: %d\n",
  1136. asoc->init_err_counter,
  1137. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE]);
  1138. /* If we've sent any data bundled with
  1139. * COOKIE-ECHO we need to resend.
  1140. */
  1141. list_for_each(pos, &asoc->peer.transport_addr_list) {
  1142. t = list_entry(pos, struct sctp_transport,
  1143. transports);
  1144. sctp_retransmit_mark(&asoc->outqueue, t, 0);
  1145. }
  1146. sctp_add_cmd_sf(commands,
  1147. SCTP_CMD_TIMER_RESTART,
  1148. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  1149. break;
  1150. case SCTP_CMD_INIT_FAILED:
  1151. sctp_cmd_init_failed(commands, asoc, cmd->obj.u32);
  1152. break;
  1153. case SCTP_CMD_ASSOC_FAILED:
  1154. sctp_cmd_assoc_failed(commands, asoc, event_type,
  1155. subtype, chunk, cmd->obj.u32);
  1156. break;
  1157. case SCTP_CMD_INIT_COUNTER_INC:
  1158. asoc->init_err_counter++;
  1159. break;
  1160. case SCTP_CMD_INIT_COUNTER_RESET:
  1161. asoc->init_err_counter = 0;
  1162. asoc->init_cycle = 0;
  1163. break;
  1164. case SCTP_CMD_REPORT_DUP:
  1165. sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
  1166. cmd->obj.u32);
  1167. break;
  1168. case SCTP_CMD_REPORT_BAD_TAG:
  1169. SCTP_DEBUG_PRINTK("vtag mismatch!\n");
  1170. break;
  1171. case SCTP_CMD_STRIKE:
  1172. /* Mark one strike against a transport. */
  1173. sctp_do_8_2_transport_strike(asoc, cmd->obj.transport);
  1174. break;
  1175. case SCTP_CMD_TRANSPORT_RESET:
  1176. t = cmd->obj.transport;
  1177. sctp_cmd_transport_reset(commands, asoc, t);
  1178. break;
  1179. case SCTP_CMD_TRANSPORT_ON:
  1180. t = cmd->obj.transport;
  1181. sctp_cmd_transport_on(commands, asoc, t, chunk);
  1182. break;
  1183. case SCTP_CMD_HB_TIMERS_START:
  1184. sctp_cmd_hb_timers_start(commands, asoc);
  1185. break;
  1186. case SCTP_CMD_HB_TIMER_UPDATE:
  1187. t = cmd->obj.transport;
  1188. sctp_cmd_hb_timer_update(commands, asoc, t);
  1189. break;
  1190. case SCTP_CMD_HB_TIMERS_STOP:
  1191. sctp_cmd_hb_timers_stop(commands, asoc);
  1192. break;
  1193. case SCTP_CMD_REPORT_ERROR:
  1194. error = cmd->obj.error;
  1195. break;
  1196. case SCTP_CMD_PROCESS_CTSN:
  1197. /* Dummy up a SACK for processing. */
  1198. sackh.cum_tsn_ack = cmd->obj.u32;
  1199. sackh.a_rwnd = 0;
  1200. sackh.num_gap_ack_blocks = 0;
  1201. sackh.num_dup_tsns = 0;
  1202. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
  1203. SCTP_SACKH(&sackh));
  1204. break;
  1205. case SCTP_CMD_DISCARD_PACKET:
  1206. /* We need to discard the whole packet. */
  1207. chunk->pdiscard = 1;
  1208. break;
  1209. case SCTP_CMD_RTO_PENDING:
  1210. t = cmd->obj.transport;
  1211. t->rto_pending = 1;
  1212. break;
  1213. case SCTP_CMD_PART_DELIVER:
  1214. sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
  1215. GFP_ATOMIC);
  1216. break;
  1217. case SCTP_CMD_RENEGE:
  1218. sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
  1219. GFP_ATOMIC);
  1220. break;
  1221. case SCTP_CMD_SETUP_T4:
  1222. sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
  1223. break;
  1224. case SCTP_CMD_PROCESS_OPERR:
  1225. sctp_cmd_process_operr(commands, asoc, chunk);
  1226. break;
  1227. case SCTP_CMD_CLEAR_INIT_TAG:
  1228. asoc->peer.i.init_tag = 0;
  1229. break;
  1230. case SCTP_CMD_DEL_NON_PRIMARY:
  1231. sctp_cmd_del_non_primary(asoc);
  1232. break;
  1233. case SCTP_CMD_T3_RTX_TIMERS_STOP:
  1234. sctp_cmd_t3_rtx_timers_stop(commands, asoc);
  1235. break;
  1236. case SCTP_CMD_FORCE_PRIM_RETRAN:
  1237. t = asoc->peer.retran_path;
  1238. asoc->peer.retran_path = asoc->peer.primary_path;
  1239. error = sctp_outq_uncork(&asoc->outqueue);
  1240. local_cork = 0;
  1241. asoc->peer.retran_path = t;
  1242. break;
  1243. default:
  1244. printk(KERN_WARNING "Impossible command: %u, %p\n",
  1245. cmd->verb, cmd->obj.ptr);
  1246. break;
  1247. };
  1248. if (error)
  1249. break;
  1250. }
  1251. out:
  1252. if (local_cork)
  1253. sctp_outq_uncork(&asoc->outqueue);
  1254. return error;
  1255. nomem:
  1256. error = -ENOMEM;
  1257. goto out;
  1258. }