sm_sideeffect.c 48 KB

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