sm_sideeffect.c 46 KB

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