sm_sideeffect.c 51 KB

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