sm_sideeffect.c 49 KB

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