sm_sideeffect.c 42 KB

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