sm_sideeffect.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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 <[email protected]>
  16. *
  17. * Written or modified by:
  18. * La Monte H.P. Yarroll <[email protected]>
  19. * Karl Knutson <[email protected]>
  20. * Jon Grimm <[email protected]>
  21. * Hui Huang <[email protected]>
  22. * Dajiang Zhang <[email protected]>
  23. * Daisy Chang <[email protected]>
  24. * Sridhar Samudrala <[email protected]>
  25. * Ardelle Fan <[email protected]>
  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 *)
  835. err_hdr->variable;
  836. switch (unk_chunk_hdr->type) {
  837. /* ADDIP 4.1 A9) If the peer responds to an ASCONF with
  838. * an ERROR chunk reporting that it did not recognized
  839. * the ASCONF chunk type, the sender of the ASCONF MUST
  840. * NOT send any further ASCONF chunks and MUST stop its
  841. * T-4 timer.
  842. */
  843. case SCTP_CID_ASCONF:
  844. if (asoc->peer.asconf_capable == 0)
  845. break;
  846. asoc->peer.asconf_capable = 0;
  847. sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
  848. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  849. break;
  850. default:
  851. break;
  852. }
  853. break;
  854. }
  855. default:
  856. break;
  857. }
  858. }
  859. }
  860. /* Helper function to remove the association non-primary peer
  861. * transports.
  862. */
  863. static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
  864. {
  865. struct sctp_transport *t;
  866. struct list_head *temp;
  867. struct list_head *pos;
  868. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  869. t = list_entry(pos, struct sctp_transport, transports);
  870. if (!sctp_cmp_addr_exact(&t->ipaddr,
  871. &asoc->peer.primary_addr)) {
  872. sctp_assoc_rm_peer(asoc, t);
  873. }
  874. }
  875. }
  876. /* Helper function to set sk_err on a 1-1 style socket. */
  877. static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
  878. {
  879. struct sock *sk = asoc->base.sk;
  880. if (!sctp_style(sk, UDP))
  881. sk->sk_err = error;
  882. }
  883. /* Helper function to generate an association change event */
  884. static void sctp_cmd_assoc_change(struct sctp_cmd_seq *commands,
  885. struct sctp_association *asoc,
  886. u8 state)
  887. {
  888. struct sctp_ulpevent *ev;
  889. ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
  890. asoc->c.sinit_num_ostreams,
  891. asoc->c.sinit_max_instreams,
  892. NULL, GFP_ATOMIC);
  893. if (ev)
  894. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  895. }
  896. static void sctp_cmd_peer_no_auth(struct sctp_cmd_seq *commands,
  897. struct sctp_association *asoc)
  898. {
  899. struct sctp_ulpevent *ev;
  900. ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH, GFP_ATOMIC);
  901. if (ev)
  902. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  903. }
  904. /* Helper function to generate an adaptation indication event */
  905. static void sctp_cmd_adaptation_ind(struct sctp_cmd_seq *commands,
  906. struct sctp_association *asoc)
  907. {
  908. struct sctp_ulpevent *ev;
  909. ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
  910. if (ev)
  911. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  912. }
  913. static void sctp_cmd_t1_timer_update(struct sctp_association *asoc,
  914. enum sctp_event_timeout timer,
  915. char *name)
  916. {
  917. struct sctp_transport *t;
  918. t = asoc->init_last_sent_to;
  919. asoc->init_err_counter++;
  920. if (t->init_sent_count > (asoc->init_cycle + 1)) {
  921. asoc->timeouts[timer] *= 2;
  922. if (asoc->timeouts[timer] > asoc->max_init_timeo) {
  923. asoc->timeouts[timer] = asoc->max_init_timeo;
  924. }
  925. asoc->init_cycle++;
  926. pr_debug("%s: T1[%s] timeout adjustment init_err_counter:%d"
  927. " cycle:%d timeout:%ld\n", __func__, name,
  928. asoc->init_err_counter, asoc->init_cycle,
  929. asoc->timeouts[timer]);
  930. }
  931. }
  932. /* Send the whole message, chunk by chunk, to the outqueue.
  933. * This way the whole message is queued up and bundling if
  934. * encouraged for small fragments.
  935. */
  936. static void sctp_cmd_send_msg(struct sctp_association *asoc,
  937. struct sctp_datamsg *msg, gfp_t gfp)
  938. {
  939. struct sctp_chunk *chunk;
  940. list_for_each_entry(chunk, &msg->chunks, frag_list)
  941. sctp_outq_tail(&asoc->outqueue, chunk, gfp);
  942. asoc->outqueue.sched->enqueue(&asoc->outqueue, msg);
  943. }
  944. /* These three macros allow us to pull the debugging code out of the
  945. * main flow of sctp_do_sm() to keep attention focused on the real
  946. * functionality there.
  947. */
  948. #define debug_pre_sfn() \
  949. pr_debug("%s[pre-fn]: ep:%p, %s, %s, asoc:%p[%s], %s\n", __func__, \
  950. ep, sctp_evttype_tbl[event_type], (*debug_fn)(subtype), \
  951. asoc, sctp_state_tbl[state], state_fn->name)
  952. #define debug_post_sfn() \
  953. pr_debug("%s[post-fn]: asoc:%p, status:%s\n", __func__, asoc, \
  954. sctp_status_tbl[status])
  955. #define debug_post_sfx() \
  956. pr_debug("%s[post-sfx]: error:%d, asoc:%p[%s]\n", __func__, error, \
  957. asoc, sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
  958. sctp_assoc2id(asoc))) ? asoc->state : SCTP_STATE_CLOSED])
  959. /*
  960. * This is the master state machine processing function.
  961. *
  962. * If you want to understand all of lksctp, this is a
  963. * good place to start.
  964. */
  965. int sctp_do_sm(struct net *net, enum sctp_event_type event_type,
  966. union sctp_subtype subtype, enum sctp_state state,
  967. struct sctp_endpoint *ep, struct sctp_association *asoc,
  968. void *event_arg, gfp_t gfp)
  969. {
  970. typedef const char *(printfn_t)(union sctp_subtype);
  971. static printfn_t *table[] = {
  972. NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
  973. };
  974. printfn_t *debug_fn __attribute__ ((unused)) = table[event_type];
  975. const struct sctp_sm_table_entry *state_fn;
  976. struct sctp_cmd_seq commands;
  977. enum sctp_disposition status;
  978. int error = 0;
  979. /* Look up the state function, run it, and then process the
  980. * side effects. These three steps are the heart of lksctp.
  981. */
  982. state_fn = sctp_sm_lookup_event(net, event_type, state, subtype);
  983. sctp_init_cmd_seq(&commands);
  984. debug_pre_sfn();
  985. status = state_fn->fn(net, ep, asoc, subtype, event_arg, &commands);
  986. debug_post_sfn();
  987. error = sctp_side_effects(event_type, subtype, state,
  988. ep, &asoc, event_arg, status,
  989. &commands, gfp);
  990. debug_post_sfx();
  991. return error;
  992. }
  993. /*****************************************************************
  994. * This the master state function side effect processing function.
  995. *****************************************************************/
  996. static int sctp_side_effects(enum sctp_event_type event_type,
  997. union sctp_subtype subtype,
  998. enum sctp_state state,
  999. struct sctp_endpoint *ep,
  1000. struct sctp_association **asoc,
  1001. void *event_arg,
  1002. enum sctp_disposition status,
  1003. struct sctp_cmd_seq *commands,
  1004. gfp_t gfp)
  1005. {
  1006. int error;
  1007. /* FIXME - Most of the dispositions left today would be categorized
  1008. * as "exceptional" dispositions. For those dispositions, it
  1009. * may not be proper to run through any of the commands at all.
  1010. * For example, the command interpreter might be run only with
  1011. * disposition SCTP_DISPOSITION_CONSUME.
  1012. */
  1013. if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
  1014. ep, *asoc,
  1015. event_arg, status,
  1016. commands, gfp)))
  1017. goto bail;
  1018. switch (status) {
  1019. case SCTP_DISPOSITION_DISCARD:
  1020. pr_debug("%s: ignored sctp protocol event - state:%d, "
  1021. "event_type:%d, event_id:%d\n", __func__, state,
  1022. event_type, subtype.chunk);
  1023. break;
  1024. case SCTP_DISPOSITION_NOMEM:
  1025. /* We ran out of memory, so we need to discard this
  1026. * packet.
  1027. */
  1028. /* BUG--we should now recover some memory, probably by
  1029. * reneging...
  1030. */
  1031. error = -ENOMEM;
  1032. break;
  1033. case SCTP_DISPOSITION_DELETE_TCB:
  1034. case SCTP_DISPOSITION_ABORT:
  1035. /* This should now be a command. */
  1036. *asoc = NULL;
  1037. break;
  1038. case SCTP_DISPOSITION_CONSUME:
  1039. /*
  1040. * We should no longer have much work to do here as the
  1041. * real work has been done as explicit commands above.
  1042. */
  1043. break;
  1044. case SCTP_DISPOSITION_VIOLATION:
  1045. net_err_ratelimited("protocol violation state %d chunkid %d\n",
  1046. state, subtype.chunk);
  1047. break;
  1048. case SCTP_DISPOSITION_NOT_IMPL:
  1049. pr_warn("unimplemented feature in state %d, event_type %d, event_id %d\n",
  1050. state, event_type, subtype.chunk);
  1051. break;
  1052. case SCTP_DISPOSITION_BUG:
  1053. pr_err("bug in state %d, event_type %d, event_id %d\n",
  1054. state, event_type, subtype.chunk);
  1055. BUG();
  1056. break;
  1057. default:
  1058. pr_err("impossible disposition %d in state %d, event_type %d, event_id %d\n",
  1059. status, state, event_type, subtype.chunk);
  1060. error = status;
  1061. if (error >= 0)
  1062. error = -EINVAL;
  1063. WARN_ON_ONCE(1);
  1064. break;
  1065. }
  1066. bail:
  1067. return error;
  1068. }
  1069. /********************************************************************
  1070. * 2nd Level Abstractions
  1071. ********************************************************************/
  1072. /* This is the side-effect interpreter. */
  1073. static int sctp_cmd_interpreter(enum sctp_event_type event_type,
  1074. union sctp_subtype subtype,
  1075. enum sctp_state state,
  1076. struct sctp_endpoint *ep,
  1077. struct sctp_association *asoc,
  1078. void *event_arg,
  1079. enum sctp_disposition status,
  1080. struct sctp_cmd_seq *commands,
  1081. gfp_t gfp)
  1082. {
  1083. struct sctp_sock *sp = sctp_sk(ep->base.sk);
  1084. struct sctp_chunk *chunk = NULL, *new_obj;
  1085. struct sctp_packet *packet;
  1086. struct sctp_sackhdr sackh;
  1087. struct timer_list *timer;
  1088. struct sctp_transport *t;
  1089. unsigned long timeout;
  1090. struct sctp_cmd *cmd;
  1091. int local_cork = 0;
  1092. int error = 0;
  1093. int force;
  1094. if (SCTP_EVENT_T_TIMEOUT != event_type)
  1095. chunk = event_arg;
  1096. /* Note: This whole file is a huge candidate for rework.
  1097. * For example, each command could either have its own handler, so
  1098. * the loop would look like:
  1099. * while (cmds)
  1100. * cmd->handle(x, y, z)
  1101. * --jgrimm
  1102. */
  1103. while (NULL != (cmd = sctp_next_cmd(commands))) {
  1104. switch (cmd->verb) {
  1105. case SCTP_CMD_NOP:
  1106. /* Do nothing. */
  1107. break;
  1108. case SCTP_CMD_NEW_ASOC:
  1109. /* Register a new association. */
  1110. if (local_cork) {
  1111. sctp_outq_uncork(&asoc->outqueue, gfp);
  1112. local_cork = 0;
  1113. }
  1114. /* Register with the endpoint. */
  1115. asoc = cmd->obj.asoc;
  1116. BUG_ON(asoc->peer.primary_path == NULL);
  1117. sctp_endpoint_add_asoc(ep, asoc);
  1118. break;
  1119. case SCTP_CMD_PURGE_OUTQUEUE:
  1120. sctp_outq_teardown(&asoc->outqueue);
  1121. break;
  1122. case SCTP_CMD_DELETE_TCB:
  1123. if (local_cork) {
  1124. sctp_outq_uncork(&asoc->outqueue, gfp);
  1125. local_cork = 0;
  1126. }
  1127. /* Delete the current association. */
  1128. sctp_cmd_delete_tcb(commands, asoc);
  1129. asoc = NULL;
  1130. break;
  1131. case SCTP_CMD_NEW_STATE:
  1132. /* Enter a new state. */
  1133. sctp_cmd_new_state(commands, asoc, cmd->obj.state);
  1134. break;
  1135. case SCTP_CMD_REPORT_TSN:
  1136. /* Record the arrival of a TSN. */
  1137. error = sctp_tsnmap_mark(&asoc->peer.tsn_map,
  1138. cmd->obj.u32, NULL);
  1139. break;
  1140. case SCTP_CMD_REPORT_FWDTSN:
  1141. asoc->stream.si->report_ftsn(&asoc->ulpq, cmd->obj.u32);
  1142. break;
  1143. case SCTP_CMD_PROCESS_FWDTSN:
  1144. asoc->stream.si->handle_ftsn(&asoc->ulpq,
  1145. cmd->obj.chunk);
  1146. break;
  1147. case SCTP_CMD_GEN_SACK:
  1148. /* Generate a Selective ACK.
  1149. * The argument tells us whether to just count
  1150. * the packet and MAYBE generate a SACK, or
  1151. * force a SACK out.
  1152. */
  1153. force = cmd->obj.i32;
  1154. error = sctp_gen_sack(asoc, force, commands);
  1155. break;
  1156. case SCTP_CMD_PROCESS_SACK:
  1157. /* Process an inbound SACK. */
  1158. error = sctp_cmd_process_sack(commands, asoc,
  1159. cmd->obj.chunk);
  1160. break;
  1161. case SCTP_CMD_GEN_INIT_ACK:
  1162. /* Generate an INIT ACK chunk. */
  1163. new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
  1164. 0);
  1165. if (!new_obj) {
  1166. error = -ENOMEM;
  1167. break;
  1168. }
  1169. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1170. SCTP_CHUNK(new_obj));
  1171. break;
  1172. case SCTP_CMD_PEER_INIT:
  1173. /* Process a unified INIT from the peer.
  1174. * Note: Only used during INIT-ACK processing. If
  1175. * there is an error just return to the outter
  1176. * layer which will bail.
  1177. */
  1178. error = sctp_cmd_process_init(commands, asoc, chunk,
  1179. cmd->obj.init, gfp);
  1180. break;
  1181. case SCTP_CMD_GEN_COOKIE_ECHO:
  1182. /* Generate a COOKIE ECHO chunk. */
  1183. new_obj = sctp_make_cookie_echo(asoc, chunk);
  1184. if (!new_obj) {
  1185. if (cmd->obj.chunk)
  1186. sctp_chunk_free(cmd->obj.chunk);
  1187. error = -ENOMEM;
  1188. break;
  1189. }
  1190. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1191. SCTP_CHUNK(new_obj));
  1192. /* If there is an ERROR chunk to be sent along with
  1193. * the COOKIE_ECHO, send it, too.
  1194. */
  1195. if (cmd->obj.chunk)
  1196. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1197. SCTP_CHUNK(cmd->obj.chunk));
  1198. if (new_obj->transport) {
  1199. new_obj->transport->init_sent_count++;
  1200. asoc->init_last_sent_to = new_obj->transport;
  1201. }
  1202. /* FIXME - Eventually come up with a cleaner way to
  1203. * enabling COOKIE-ECHO + DATA bundling during
  1204. * multihoming stale cookie scenarios, the following
  1205. * command plays with asoc->peer.retran_path to
  1206. * avoid the problem of sending the COOKIE-ECHO and
  1207. * DATA in different paths, which could result
  1208. * in the association being ABORTed if the DATA chunk
  1209. * is processed first by the server. Checking the
  1210. * init error counter simply causes this command
  1211. * to be executed only during failed attempts of
  1212. * association establishment.
  1213. */
  1214. if ((asoc->peer.retran_path !=
  1215. asoc->peer.primary_path) &&
  1216. (asoc->init_err_counter > 0)) {
  1217. sctp_add_cmd_sf(commands,
  1218. SCTP_CMD_FORCE_PRIM_RETRAN,
  1219. SCTP_NULL());
  1220. }
  1221. break;
  1222. case SCTP_CMD_GEN_SHUTDOWN:
  1223. /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
  1224. * Reset error counts.
  1225. */
  1226. asoc->overall_error_count = 0;
  1227. /* Generate a SHUTDOWN chunk. */
  1228. new_obj = sctp_make_shutdown(asoc, chunk);
  1229. if (!new_obj) {
  1230. error = -ENOMEM;
  1231. break;
  1232. }
  1233. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1234. SCTP_CHUNK(new_obj));
  1235. break;
  1236. case SCTP_CMD_CHUNK_ULP:
  1237. /* Send a chunk to the sockets layer. */
  1238. pr_debug("%s: sm_sideff: chunk_up:%p, ulpq:%p\n",
  1239. __func__, cmd->obj.chunk, &asoc->ulpq);
  1240. asoc->stream.si->ulpevent_data(&asoc->ulpq,
  1241. cmd->obj.chunk,
  1242. GFP_ATOMIC);
  1243. break;
  1244. case SCTP_CMD_EVENT_ULP:
  1245. /* Send a notification to the sockets layer. */
  1246. pr_debug("%s: sm_sideff: event_up:%p, ulpq:%p\n",
  1247. __func__, cmd->obj.ulpevent, &asoc->ulpq);
  1248. asoc->stream.si->enqueue_event(&asoc->ulpq,
  1249. cmd->obj.ulpevent);
  1250. break;
  1251. case SCTP_CMD_REPLY:
  1252. /* If an caller has not already corked, do cork. */
  1253. if (!asoc->outqueue.cork) {
  1254. sctp_outq_cork(&asoc->outqueue);
  1255. local_cork = 1;
  1256. }
  1257. /* Send a chunk to our peer. */
  1258. sctp_outq_tail(&asoc->outqueue, cmd->obj.chunk, gfp);
  1259. break;
  1260. case SCTP_CMD_SEND_PKT:
  1261. /* Send a full packet to our peer. */
  1262. packet = cmd->obj.packet;
  1263. sctp_packet_transmit(packet, gfp);
  1264. sctp_ootb_pkt_free(packet);
  1265. break;
  1266. case SCTP_CMD_T1_RETRAN:
  1267. /* Mark a transport for retransmission. */
  1268. sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  1269. SCTP_RTXR_T1_RTX);
  1270. break;
  1271. case SCTP_CMD_RETRAN:
  1272. /* Mark a transport for retransmission. */
  1273. sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  1274. SCTP_RTXR_T3_RTX);
  1275. break;
  1276. case SCTP_CMD_ECN_CE:
  1277. /* Do delayed CE processing. */
  1278. sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
  1279. break;
  1280. case SCTP_CMD_ECN_ECNE:
  1281. /* Do delayed ECNE processing. */
  1282. new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
  1283. chunk);
  1284. if (new_obj)
  1285. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1286. SCTP_CHUNK(new_obj));
  1287. break;
  1288. case SCTP_CMD_ECN_CWR:
  1289. /* Do delayed CWR processing. */
  1290. sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
  1291. break;
  1292. case SCTP_CMD_SETUP_T2:
  1293. sctp_cmd_setup_t2(commands, asoc, cmd->obj.chunk);
  1294. break;
  1295. case SCTP_CMD_TIMER_START_ONCE:
  1296. timer = &asoc->timers[cmd->obj.to];
  1297. if (timer_pending(timer))
  1298. break;
  1299. fallthrough;
  1300. case SCTP_CMD_TIMER_START:
  1301. timer = &asoc->timers[cmd->obj.to];
  1302. timeout = asoc->timeouts[cmd->obj.to];
  1303. BUG_ON(!timeout);
  1304. /*
  1305. * SCTP has a hard time with timer starts. Because we process
  1306. * timer starts as side effects, it can be hard to tell if we
  1307. * have already started a timer or not, which leads to BUG
  1308. * halts when we call add_timer. So here, instead of just starting
  1309. * a timer, if the timer is already started, and just mod
  1310. * the timer with the shorter of the two expiration times
  1311. */
  1312. if (!timer_pending(timer))
  1313. sctp_association_hold(asoc);
  1314. timer_reduce(timer, jiffies + timeout);
  1315. break;
  1316. case SCTP_CMD_TIMER_RESTART:
  1317. timer = &asoc->timers[cmd->obj.to];
  1318. timeout = asoc->timeouts[cmd->obj.to];
  1319. if (!mod_timer(timer, jiffies + timeout))
  1320. sctp_association_hold(asoc);
  1321. break;
  1322. case SCTP_CMD_TIMER_STOP:
  1323. timer = &asoc->timers[cmd->obj.to];
  1324. if (del_timer(timer))
  1325. sctp_association_put(asoc);
  1326. break;
  1327. case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
  1328. chunk = cmd->obj.chunk;
  1329. t = sctp_assoc_choose_alter_transport(asoc,
  1330. asoc->init_last_sent_to);
  1331. asoc->init_last_sent_to = t;
  1332. chunk->transport = t;
  1333. t->init_sent_count++;
  1334. /* Set the new transport as primary */
  1335. sctp_assoc_set_primary(asoc, t);
  1336. break;
  1337. case SCTP_CMD_INIT_RESTART:
  1338. /* Do the needed accounting and updates
  1339. * associated with restarting an initialization
  1340. * timer. Only multiply the timeout by two if
  1341. * all transports have been tried at the current
  1342. * timeout.
  1343. */
  1344. sctp_cmd_t1_timer_update(asoc,
  1345. SCTP_EVENT_TIMEOUT_T1_INIT,
  1346. "INIT");
  1347. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  1348. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  1349. break;
  1350. case SCTP_CMD_COOKIEECHO_RESTART:
  1351. /* Do the needed accounting and updates
  1352. * associated with restarting an initialization
  1353. * timer. Only multiply the timeout by two if
  1354. * all transports have been tried at the current
  1355. * timeout.
  1356. */
  1357. sctp_cmd_t1_timer_update(asoc,
  1358. SCTP_EVENT_TIMEOUT_T1_COOKIE,
  1359. "COOKIE");
  1360. /* If we've sent any data bundled with
  1361. * COOKIE-ECHO we need to resend.
  1362. */
  1363. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  1364. transports) {
  1365. sctp_retransmit_mark(&asoc->outqueue, t,
  1366. SCTP_RTXR_T1_RTX);
  1367. }
  1368. sctp_add_cmd_sf(commands,
  1369. SCTP_CMD_TIMER_RESTART,
  1370. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  1371. break;
  1372. case SCTP_CMD_INIT_FAILED:
  1373. sctp_cmd_init_failed(commands, asoc, cmd->obj.u16);
  1374. break;
  1375. case SCTP_CMD_ASSOC_FAILED:
  1376. sctp_cmd_assoc_failed(commands, asoc, event_type,
  1377. subtype, chunk, cmd->obj.u16);
  1378. break;
  1379. case SCTP_CMD_INIT_COUNTER_INC:
  1380. asoc->init_err_counter++;
  1381. break;
  1382. case SCTP_CMD_INIT_COUNTER_RESET:
  1383. asoc->init_err_counter = 0;
  1384. asoc->init_cycle = 0;
  1385. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  1386. transports) {
  1387. t->init_sent_count = 0;
  1388. }
  1389. break;
  1390. case SCTP_CMD_REPORT_DUP:
  1391. sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
  1392. cmd->obj.u32);
  1393. break;
  1394. case SCTP_CMD_REPORT_BAD_TAG:
  1395. pr_debug("%s: vtag mismatch!\n", __func__);
  1396. break;
  1397. case SCTP_CMD_STRIKE:
  1398. /* Mark one strike against a transport. */
  1399. sctp_do_8_2_transport_strike(commands, asoc,
  1400. cmd->obj.transport, 0);
  1401. break;
  1402. case SCTP_CMD_TRANSPORT_IDLE:
  1403. t = cmd->obj.transport;
  1404. sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
  1405. break;
  1406. case SCTP_CMD_TRANSPORT_HB_SENT:
  1407. t = cmd->obj.transport;
  1408. sctp_do_8_2_transport_strike(commands, asoc,
  1409. t, 1);
  1410. t->hb_sent = 1;
  1411. break;
  1412. case SCTP_CMD_TRANSPORT_ON:
  1413. t = cmd->obj.transport;
  1414. sctp_cmd_transport_on(commands, asoc, t, chunk);
  1415. break;
  1416. case SCTP_CMD_HB_TIMERS_START:
  1417. sctp_cmd_hb_timers_start(commands, asoc);
  1418. break;
  1419. case SCTP_CMD_HB_TIMER_UPDATE:
  1420. t = cmd->obj.transport;
  1421. sctp_transport_reset_hb_timer(t);
  1422. break;
  1423. case SCTP_CMD_HB_TIMERS_STOP:
  1424. sctp_cmd_hb_timers_stop(commands, asoc);
  1425. break;
  1426. case SCTP_CMD_PROBE_TIMER_UPDATE:
  1427. t = cmd->obj.transport;
  1428. sctp_transport_reset_probe_timer(t);
  1429. break;
  1430. case SCTP_CMD_REPORT_ERROR:
  1431. error = cmd->obj.error;
  1432. break;
  1433. case SCTP_CMD_PROCESS_CTSN:
  1434. /* Dummy up a SACK for processing. */
  1435. sackh.cum_tsn_ack = cmd->obj.be32;
  1436. sackh.a_rwnd = htonl(asoc->peer.rwnd +
  1437. asoc->outqueue.outstanding_bytes);
  1438. sackh.num_gap_ack_blocks = 0;
  1439. sackh.num_dup_tsns = 0;
  1440. chunk->subh.sack_hdr = &sackh;
  1441. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
  1442. SCTP_CHUNK(chunk));
  1443. break;
  1444. case SCTP_CMD_DISCARD_PACKET:
  1445. /* We need to discard the whole packet.
  1446. * Uncork the queue since there might be
  1447. * responses pending
  1448. */
  1449. chunk->pdiscard = 1;
  1450. if (asoc) {
  1451. sctp_outq_uncork(&asoc->outqueue, gfp);
  1452. local_cork = 0;
  1453. }
  1454. break;
  1455. case SCTP_CMD_RTO_PENDING:
  1456. t = cmd->obj.transport;
  1457. t->rto_pending = 1;
  1458. break;
  1459. case SCTP_CMD_PART_DELIVER:
  1460. asoc->stream.si->start_pd(&asoc->ulpq, GFP_ATOMIC);
  1461. break;
  1462. case SCTP_CMD_RENEGE:
  1463. asoc->stream.si->renege_events(&asoc->ulpq,
  1464. cmd->obj.chunk,
  1465. GFP_ATOMIC);
  1466. break;
  1467. case SCTP_CMD_SETUP_T4:
  1468. sctp_cmd_setup_t4(commands, asoc, cmd->obj.chunk);
  1469. break;
  1470. case SCTP_CMD_PROCESS_OPERR:
  1471. sctp_cmd_process_operr(commands, asoc, chunk);
  1472. break;
  1473. case SCTP_CMD_CLEAR_INIT_TAG:
  1474. asoc->peer.i.init_tag = 0;
  1475. break;
  1476. case SCTP_CMD_DEL_NON_PRIMARY:
  1477. sctp_cmd_del_non_primary(asoc);
  1478. break;
  1479. case SCTP_CMD_T3_RTX_TIMERS_STOP:
  1480. sctp_cmd_t3_rtx_timers_stop(commands, asoc);
  1481. break;
  1482. case SCTP_CMD_FORCE_PRIM_RETRAN:
  1483. t = asoc->peer.retran_path;
  1484. asoc->peer.retran_path = asoc->peer.primary_path;
  1485. sctp_outq_uncork(&asoc->outqueue, gfp);
  1486. local_cork = 0;
  1487. asoc->peer.retran_path = t;
  1488. break;
  1489. case SCTP_CMD_SET_SK_ERR:
  1490. sctp_cmd_set_sk_err(asoc, cmd->obj.error);
  1491. break;
  1492. case SCTP_CMD_ASSOC_CHANGE:
  1493. sctp_cmd_assoc_change(commands, asoc,
  1494. cmd->obj.u8);
  1495. break;
  1496. case SCTP_CMD_ADAPTATION_IND:
  1497. sctp_cmd_adaptation_ind(commands, asoc);
  1498. break;
  1499. case SCTP_CMD_PEER_NO_AUTH:
  1500. sctp_cmd_peer_no_auth(commands, asoc);
  1501. break;
  1502. case SCTP_CMD_ASSOC_SHKEY:
  1503. error = sctp_auth_asoc_init_active_key(asoc,
  1504. GFP_ATOMIC);
  1505. break;
  1506. case SCTP_CMD_UPDATE_INITTAG:
  1507. asoc->peer.i.init_tag = cmd->obj.u32;
  1508. break;
  1509. case SCTP_CMD_SEND_MSG:
  1510. if (!asoc->outqueue.cork) {
  1511. sctp_outq_cork(&asoc->outqueue);
  1512. local_cork = 1;
  1513. }
  1514. sctp_cmd_send_msg(asoc, cmd->obj.msg, gfp);
  1515. break;
  1516. case SCTP_CMD_PURGE_ASCONF_QUEUE:
  1517. sctp_asconf_queue_teardown(asoc);
  1518. break;
  1519. case SCTP_CMD_SET_ASOC:
  1520. if (asoc && local_cork) {
  1521. sctp_outq_uncork(&asoc->outqueue, gfp);
  1522. local_cork = 0;
  1523. }
  1524. asoc = cmd->obj.asoc;
  1525. break;
  1526. default:
  1527. pr_warn("Impossible command: %u\n",
  1528. cmd->verb);
  1529. break;
  1530. }
  1531. if (error) {
  1532. cmd = sctp_next_cmd(commands);
  1533. while (cmd) {
  1534. if (cmd->verb == SCTP_CMD_REPLY)
  1535. sctp_chunk_free(cmd->obj.chunk);
  1536. cmd = sctp_next_cmd(commands);
  1537. }
  1538. break;
  1539. }
  1540. }
  1541. /* If this is in response to a received chunk, wait until
  1542. * we are done with the packet to open the queue so that we don't
  1543. * send multiple packets in response to a single request.
  1544. */
  1545. if (asoc && SCTP_EVENT_T_CHUNK == event_type && chunk) {
  1546. if (chunk->end_of_packet || chunk->singleton)
  1547. sctp_outq_uncork(&asoc->outqueue, gfp);
  1548. } else if (local_cork)
  1549. sctp_outq_uncork(&asoc->outqueue, gfp);
  1550. if (sp->data_ready_signalled)
  1551. sp->data_ready_signalled = 0;
  1552. return error;
  1553. }