sm_sideeffect.c 47 KB

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