sm_sideeffect.c 48 KB

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