sm_sideeffect.c 49 KB

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