sm_sideeffect.c 49 KB

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