sm_sideeffect.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(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. /* When probing UNCONFIRMED addresses, the association overall
  377. * error count is NOT incremented
  378. */
  379. if (transport->state != SCTP_UNCONFIRMED)
  380. asoc->overall_error_count++;
  381. if (transport->state != SCTP_INACTIVE &&
  382. (transport->error_count++ >= transport->pathmaxrxt)) {
  383. SCTP_DEBUG_PRINTK_IPADDR("transport_strike:association %p",
  384. " transport IP: port:%d failed.\n",
  385. asoc,
  386. (&transport->ipaddr),
  387. ntohs(transport->ipaddr.v4.sin_port));
  388. sctp_assoc_control_transport(asoc, transport,
  389. SCTP_TRANSPORT_DOWN,
  390. SCTP_FAILED_THRESHOLD);
  391. }
  392. /* E2) For the destination address for which the timer
  393. * expires, set RTO <- RTO * 2 ("back off the timer"). The
  394. * maximum value discussed in rule C7 above (RTO.max) may be
  395. * used to provide an upper bound to this doubling operation.
  396. */
  397. transport->last_rto = transport->rto;
  398. transport->rto = min((transport->rto * 2), transport->asoc->rto_max);
  399. }
  400. /* Worker routine to handle INIT command failure. */
  401. static void sctp_cmd_init_failed(sctp_cmd_seq_t *commands,
  402. struct sctp_association *asoc,
  403. unsigned error)
  404. {
  405. struct sctp_ulpevent *event;
  406. event = sctp_ulpevent_make_assoc_change(asoc,0, SCTP_CANT_STR_ASSOC,
  407. (__u16)error, 0, 0, NULL,
  408. GFP_ATOMIC);
  409. if (event)
  410. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  411. SCTP_ULPEVENT(event));
  412. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  413. SCTP_STATE(SCTP_STATE_CLOSED));
  414. /* SEND_FAILED sent later when cleaning up the association. */
  415. asoc->outqueue.error = error;
  416. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  417. }
  418. /* Worker routine to handle SCTP_CMD_ASSOC_FAILED. */
  419. static void sctp_cmd_assoc_failed(sctp_cmd_seq_t *commands,
  420. struct sctp_association *asoc,
  421. sctp_event_t event_type,
  422. sctp_subtype_t subtype,
  423. struct sctp_chunk *chunk,
  424. unsigned error)
  425. {
  426. struct sctp_ulpevent *event;
  427. /* Cancel any partial delivery in progress. */
  428. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  429. if (event_type == SCTP_EVENT_T_CHUNK && subtype.chunk == SCTP_CID_ABORT)
  430. event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
  431. (__u16)error, 0, 0, chunk,
  432. GFP_ATOMIC);
  433. else
  434. event = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_LOST,
  435. (__u16)error, 0, 0, NULL,
  436. GFP_ATOMIC);
  437. if (event)
  438. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  439. SCTP_ULPEVENT(event));
  440. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  441. SCTP_STATE(SCTP_STATE_CLOSED));
  442. /* SEND_FAILED sent later when cleaning up the association. */
  443. asoc->outqueue.error = error;
  444. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  445. }
  446. /* Process an init chunk (may be real INIT/INIT-ACK or an embedded INIT
  447. * inside the cookie. In reality, this is only used for INIT-ACK processing
  448. * since all other cases use "temporary" associations and can do all
  449. * their work in statefuns directly.
  450. */
  451. static int sctp_cmd_process_init(sctp_cmd_seq_t *commands,
  452. struct sctp_association *asoc,
  453. struct sctp_chunk *chunk,
  454. sctp_init_chunk_t *peer_init,
  455. gfp_t gfp)
  456. {
  457. int error;
  458. /* We only process the init as a sideeffect in a single
  459. * case. This is when we process the INIT-ACK. If we
  460. * fail during INIT processing (due to malloc problems),
  461. * just return the error and stop processing the stack.
  462. */
  463. if (!sctp_process_init(asoc, chunk->chunk_hdr->type,
  464. sctp_source(chunk), peer_init, gfp))
  465. error = -ENOMEM;
  466. else
  467. error = 0;
  468. return error;
  469. }
  470. /* Helper function to break out starting up of heartbeat timers. */
  471. static void sctp_cmd_hb_timers_start(sctp_cmd_seq_t *cmds,
  472. struct sctp_association *asoc)
  473. {
  474. struct sctp_transport *t;
  475. struct list_head *pos;
  476. /* Start a heartbeat timer for each transport on the association.
  477. * hold a reference on the transport to make sure none of
  478. * the needed data structures go away.
  479. */
  480. list_for_each(pos, &asoc->peer.transport_addr_list) {
  481. t = list_entry(pos, struct sctp_transport, transports);
  482. if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  483. sctp_transport_hold(t);
  484. }
  485. }
  486. static void sctp_cmd_hb_timers_stop(sctp_cmd_seq_t *cmds,
  487. struct sctp_association *asoc)
  488. {
  489. struct sctp_transport *t;
  490. struct list_head *pos;
  491. /* Stop all heartbeat timers. */
  492. list_for_each(pos, &asoc->peer.transport_addr_list) {
  493. t = list_entry(pos, struct sctp_transport, transports);
  494. if (del_timer(&t->hb_timer))
  495. sctp_transport_put(t);
  496. }
  497. }
  498. /* Helper function to stop any pending T3-RTX timers */
  499. static void sctp_cmd_t3_rtx_timers_stop(sctp_cmd_seq_t *cmds,
  500. struct sctp_association *asoc)
  501. {
  502. struct sctp_transport *t;
  503. struct list_head *pos;
  504. list_for_each(pos, &asoc->peer.transport_addr_list) {
  505. t = list_entry(pos, struct sctp_transport, transports);
  506. if (timer_pending(&t->T3_rtx_timer) &&
  507. del_timer(&t->T3_rtx_timer)) {
  508. sctp_transport_put(t);
  509. }
  510. }
  511. }
  512. /* Helper function to update the heartbeat timer. */
  513. static void sctp_cmd_hb_timer_update(sctp_cmd_seq_t *cmds,
  514. struct sctp_association *asoc,
  515. struct sctp_transport *t)
  516. {
  517. /* Update the heartbeat timer. */
  518. if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  519. sctp_transport_hold(t);
  520. }
  521. /* Helper function to handle the reception of an HEARTBEAT ACK. */
  522. static void sctp_cmd_transport_on(sctp_cmd_seq_t *cmds,
  523. struct sctp_association *asoc,
  524. struct sctp_transport *t,
  525. struct sctp_chunk *chunk)
  526. {
  527. sctp_sender_hb_info_t *hbinfo;
  528. /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of the
  529. * HEARTBEAT should clear the error counter of the destination
  530. * transport address to which the HEARTBEAT was sent.
  531. * The association's overall error count is also cleared.
  532. */
  533. t->error_count = 0;
  534. t->asoc->overall_error_count = 0;
  535. /* Mark the destination transport address as active if it is not so
  536. * marked.
  537. */
  538. if ((t->state == SCTP_INACTIVE) || (t->state == SCTP_UNCONFIRMED))
  539. sctp_assoc_control_transport(asoc, t, SCTP_TRANSPORT_UP,
  540. SCTP_HEARTBEAT_SUCCESS);
  541. /* The receiver of the HEARTBEAT ACK should also perform an
  542. * RTT measurement for that destination transport address
  543. * using the time value carried in the HEARTBEAT ACK chunk.
  544. * If the transport's rto_pending variable has been cleared,
  545. * it was most likely due to a retransmit. However, we want
  546. * to re-enable it to properly update the rto.
  547. */
  548. if (t->rto_pending == 0)
  549. t->rto_pending = 1;
  550. hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
  551. sctp_transport_update_rto(t, (jiffies - hbinfo->sent_at));
  552. /* Update the heartbeat timer. */
  553. if (!mod_timer(&t->hb_timer, sctp_transport_timeout(t)))
  554. sctp_transport_hold(t);
  555. }
  556. /* Helper function to do a transport reset at the expiry of the hearbeat
  557. * timer.
  558. */
  559. static void sctp_cmd_transport_reset(sctp_cmd_seq_t *cmds,
  560. struct sctp_association *asoc,
  561. struct sctp_transport *t)
  562. {
  563. sctp_transport_lower_cwnd(t, SCTP_LOWER_CWND_INACTIVE);
  564. /* Mark one strike against a transport. */
  565. sctp_do_8_2_transport_strike(asoc, t);
  566. }
  567. /* Helper function to process the process SACK command. */
  568. static int sctp_cmd_process_sack(sctp_cmd_seq_t *cmds,
  569. struct sctp_association *asoc,
  570. struct sctp_sackhdr *sackh)
  571. {
  572. int err;
  573. if (sctp_outq_sack(&asoc->outqueue, sackh)) {
  574. /* There are no more TSNs awaiting SACK. */
  575. err = sctp_do_sm(SCTP_EVENT_T_OTHER,
  576. SCTP_ST_OTHER(SCTP_EVENT_NO_PENDING_TSN),
  577. asoc->state, asoc->ep, asoc, NULL,
  578. GFP_ATOMIC);
  579. } else {
  580. /* Windows may have opened, so we need
  581. * to check if we have DATA to transmit
  582. */
  583. err = sctp_outq_flush(&asoc->outqueue, 0);
  584. }
  585. return err;
  586. }
  587. /* Helper function to set the timeout value for T2-SHUTDOWN timer and to set
  588. * the transport for a shutdown chunk.
  589. */
  590. static void sctp_cmd_setup_t2(sctp_cmd_seq_t *cmds,
  591. struct sctp_association *asoc,
  592. struct sctp_chunk *chunk)
  593. {
  594. struct sctp_transport *t;
  595. t = sctp_assoc_choose_shutdown_transport(asoc);
  596. asoc->shutdown_last_sent_to = t;
  597. asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = t->rto;
  598. chunk->transport = t;
  599. }
  600. /* Helper function to change the state of an association. */
  601. static void sctp_cmd_new_state(sctp_cmd_seq_t *cmds,
  602. struct sctp_association *asoc,
  603. sctp_state_t state)
  604. {
  605. struct sock *sk = asoc->base.sk;
  606. asoc->state = state;
  607. SCTP_DEBUG_PRINTK("sctp_cmd_new_state: asoc %p[%s]\n",
  608. asoc, sctp_state_tbl[state]);
  609. if (sctp_style(sk, TCP)) {
  610. /* Change the sk->sk_state of a TCP-style socket that has
  611. * sucessfully completed a connect() call.
  612. */
  613. if (sctp_state(asoc, ESTABLISHED) && sctp_sstate(sk, CLOSED))
  614. sk->sk_state = SCTP_SS_ESTABLISHED;
  615. /* Set the RCV_SHUTDOWN flag when a SHUTDOWN is received. */
  616. if (sctp_state(asoc, SHUTDOWN_RECEIVED) &&
  617. sctp_sstate(sk, ESTABLISHED))
  618. sk->sk_shutdown |= RCV_SHUTDOWN;
  619. }
  620. if (sctp_state(asoc, COOKIE_WAIT)) {
  621. /* Reset init timeouts since they may have been
  622. * increased due to timer expirations.
  623. */
  624. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
  625. asoc->rto_initial;
  626. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
  627. asoc->rto_initial;
  628. }
  629. if (sctp_state(asoc, ESTABLISHED) ||
  630. sctp_state(asoc, CLOSED) ||
  631. sctp_state(asoc, SHUTDOWN_RECEIVED)) {
  632. /* Wake up any processes waiting in the asoc's wait queue in
  633. * sctp_wait_for_connect() or sctp_wait_for_sndbuf().
  634. */
  635. if (waitqueue_active(&asoc->wait))
  636. wake_up_interruptible(&asoc->wait);
  637. /* Wake up any processes waiting in the sk's sleep queue of
  638. * a TCP-style or UDP-style peeled-off socket in
  639. * sctp_wait_for_accept() or sctp_wait_for_packet().
  640. * For a UDP-style socket, the waiters are woken up by the
  641. * notifications.
  642. */
  643. if (!sctp_style(sk, UDP))
  644. sk->sk_state_change(sk);
  645. }
  646. }
  647. /* Helper function to delete an association. */
  648. static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
  649. struct sctp_association *asoc)
  650. {
  651. struct sock *sk = asoc->base.sk;
  652. /* If it is a non-temporary association belonging to a TCP-style
  653. * listening socket that is not closed, do not free it so that accept()
  654. * can pick it up later.
  655. */
  656. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING) &&
  657. (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
  658. return;
  659. sctp_unhash_established(asoc);
  660. sctp_association_free(asoc);
  661. }
  662. /*
  663. * ADDIP Section 4.1 ASCONF Chunk Procedures
  664. * A4) Start a T-4 RTO timer, using the RTO value of the selected
  665. * destination address (we use active path instead of primary path just
  666. * because primary path may be inactive.
  667. */
  668. static void sctp_cmd_setup_t4(sctp_cmd_seq_t *cmds,
  669. struct sctp_association *asoc,
  670. struct sctp_chunk *chunk)
  671. {
  672. struct sctp_transport *t;
  673. t = asoc->peer.active_path;
  674. asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = t->rto;
  675. chunk->transport = t;
  676. }
  677. /* Process an incoming Operation Error Chunk. */
  678. static void sctp_cmd_process_operr(sctp_cmd_seq_t *cmds,
  679. struct sctp_association *asoc,
  680. struct sctp_chunk *chunk)
  681. {
  682. struct sctp_operr_chunk *operr_chunk;
  683. struct sctp_errhdr *err_hdr;
  684. operr_chunk = (struct sctp_operr_chunk *)chunk->chunk_hdr;
  685. err_hdr = &operr_chunk->err_hdr;
  686. switch (err_hdr->cause) {
  687. case SCTP_ERROR_UNKNOWN_CHUNK:
  688. {
  689. struct sctp_chunkhdr *unk_chunk_hdr;
  690. unk_chunk_hdr = (struct sctp_chunkhdr *)err_hdr->variable;
  691. switch (unk_chunk_hdr->type) {
  692. /* ADDIP 4.1 A9) If the peer responds to an ASCONF with an
  693. * ERROR chunk reporting that it did not recognized the ASCONF
  694. * chunk type, the sender of the ASCONF MUST NOT send any
  695. * further ASCONF chunks and MUST stop its T-4 timer.
  696. */
  697. case SCTP_CID_ASCONF:
  698. asoc->peer.asconf_capable = 0;
  699. sctp_add_cmd_sf(cmds, SCTP_CMD_TIMER_STOP,
  700. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  701. break;
  702. default:
  703. break;
  704. }
  705. break;
  706. }
  707. default:
  708. break;
  709. }
  710. }
  711. /* Process variable FWDTSN chunk information. */
  712. static void sctp_cmd_process_fwdtsn(struct sctp_ulpq *ulpq,
  713. struct sctp_chunk *chunk)
  714. {
  715. struct sctp_fwdtsn_skip *skip;
  716. /* Walk through all the skipped SSNs */
  717. sctp_walk_fwdtsn(skip, chunk) {
  718. sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
  719. }
  720. return;
  721. }
  722. /* Helper function to remove the association non-primary peer
  723. * transports.
  724. */
  725. static void sctp_cmd_del_non_primary(struct sctp_association *asoc)
  726. {
  727. struct sctp_transport *t;
  728. struct list_head *pos;
  729. struct list_head *temp;
  730. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  731. t = list_entry(pos, struct sctp_transport, transports);
  732. if (!sctp_cmp_addr_exact(&t->ipaddr,
  733. &asoc->peer.primary_addr)) {
  734. sctp_assoc_del_peer(asoc, &t->ipaddr);
  735. }
  736. }
  737. return;
  738. }
  739. /* Helper function to set sk_err on a 1-1 style socket. */
  740. static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
  741. {
  742. struct sock *sk = asoc->base.sk;
  743. if (!sctp_style(sk, UDP))
  744. sk->sk_err = error;
  745. }
  746. /* Helper function to generate an association change event */
  747. static void sctp_cmd_assoc_change(sctp_cmd_seq_t *commands,
  748. struct sctp_association *asoc,
  749. u8 state)
  750. {
  751. struct sctp_ulpevent *ev;
  752. ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
  753. asoc->c.sinit_num_ostreams,
  754. asoc->c.sinit_max_instreams,
  755. NULL, GFP_ATOMIC);
  756. if (ev)
  757. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  758. }
  759. /* Helper function to generate an adaptation indication event */
  760. static void sctp_cmd_adaptation_ind(sctp_cmd_seq_t *commands,
  761. struct sctp_association *asoc)
  762. {
  763. struct sctp_ulpevent *ev;
  764. ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
  765. if (ev)
  766. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  767. }
  768. /* These three macros allow us to pull the debugging code out of the
  769. * main flow of sctp_do_sm() to keep attention focused on the real
  770. * functionality there.
  771. */
  772. #define DEBUG_PRE \
  773. SCTP_DEBUG_PRINTK("sctp_do_sm prefn: " \
  774. "ep %p, %s, %s, asoc %p[%s], %s\n", \
  775. ep, sctp_evttype_tbl[event_type], \
  776. (*debug_fn)(subtype), asoc, \
  777. sctp_state_tbl[state], state_fn->name)
  778. #define DEBUG_POST \
  779. SCTP_DEBUG_PRINTK("sctp_do_sm postfn: " \
  780. "asoc %p, status: %s\n", \
  781. asoc, sctp_status_tbl[status])
  782. #define DEBUG_POST_SFX \
  783. SCTP_DEBUG_PRINTK("sctp_do_sm post sfx: error %d, asoc %p[%s]\n", \
  784. error, asoc, \
  785. sctp_state_tbl[(asoc && sctp_id2assoc(ep->base.sk, \
  786. sctp_assoc2id(asoc)))?asoc->state:SCTP_STATE_CLOSED])
  787. /*
  788. * This is the master state machine processing function.
  789. *
  790. * If you want to understand all of lksctp, this is a
  791. * good place to start.
  792. */
  793. int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
  794. sctp_state_t state,
  795. struct sctp_endpoint *ep,
  796. struct sctp_association *asoc,
  797. void *event_arg,
  798. gfp_t gfp)
  799. {
  800. sctp_cmd_seq_t commands;
  801. const sctp_sm_table_entry_t *state_fn;
  802. sctp_disposition_t status;
  803. int error = 0;
  804. typedef const char *(printfn_t)(sctp_subtype_t);
  805. static printfn_t *table[] = {
  806. NULL, sctp_cname, sctp_tname, sctp_oname, sctp_pname,
  807. };
  808. printfn_t *debug_fn __attribute__ ((unused)) = table[event_type];
  809. /* Look up the state function, run it, and then process the
  810. * side effects. These three steps are the heart of lksctp.
  811. */
  812. state_fn = sctp_sm_lookup_event(event_type, state, subtype);
  813. sctp_init_cmd_seq(&commands);
  814. DEBUG_PRE;
  815. status = (*state_fn->fn)(ep, asoc, subtype, event_arg, &commands);
  816. DEBUG_POST;
  817. error = sctp_side_effects(event_type, subtype, state,
  818. ep, asoc, event_arg, status,
  819. &commands, gfp);
  820. DEBUG_POST_SFX;
  821. return error;
  822. }
  823. #undef DEBUG_PRE
  824. #undef DEBUG_POST
  825. /*****************************************************************
  826. * This the master state function side effect processing function.
  827. *****************************************************************/
  828. static int sctp_side_effects(sctp_event_t event_type, sctp_subtype_t subtype,
  829. sctp_state_t state,
  830. struct sctp_endpoint *ep,
  831. struct sctp_association *asoc,
  832. void *event_arg,
  833. sctp_disposition_t status,
  834. sctp_cmd_seq_t *commands,
  835. gfp_t gfp)
  836. {
  837. int error;
  838. /* FIXME - Most of the dispositions left today would be categorized
  839. * as "exceptional" dispositions. For those dispositions, it
  840. * may not be proper to run through any of the commands at all.
  841. * For example, the command interpreter might be run only with
  842. * disposition SCTP_DISPOSITION_CONSUME.
  843. */
  844. if (0 != (error = sctp_cmd_interpreter(event_type, subtype, state,
  845. ep, asoc,
  846. event_arg, status,
  847. commands, gfp)))
  848. goto bail;
  849. switch (status) {
  850. case SCTP_DISPOSITION_DISCARD:
  851. SCTP_DEBUG_PRINTK("Ignored sctp protocol event - state %d, "
  852. "event_type %d, event_id %d\n",
  853. state, event_type, subtype.chunk);
  854. break;
  855. case SCTP_DISPOSITION_NOMEM:
  856. /* We ran out of memory, so we need to discard this
  857. * packet.
  858. */
  859. /* BUG--we should now recover some memory, probably by
  860. * reneging...
  861. */
  862. error = -ENOMEM;
  863. break;
  864. case SCTP_DISPOSITION_DELETE_TCB:
  865. /* This should now be a command. */
  866. break;
  867. case SCTP_DISPOSITION_CONSUME:
  868. case SCTP_DISPOSITION_ABORT:
  869. /*
  870. * We should no longer have much work to do here as the
  871. * real work has been done as explicit commands above.
  872. */
  873. break;
  874. case SCTP_DISPOSITION_VIOLATION:
  875. if (net_ratelimit())
  876. printk(KERN_ERR "sctp protocol violation state %d "
  877. "chunkid %d\n", state, subtype.chunk);
  878. break;
  879. case SCTP_DISPOSITION_NOT_IMPL:
  880. printk(KERN_WARNING "sctp unimplemented feature in state %d, "
  881. "event_type %d, event_id %d\n",
  882. state, event_type, subtype.chunk);
  883. break;
  884. case SCTP_DISPOSITION_BUG:
  885. printk(KERN_ERR "sctp bug in state %d, "
  886. "event_type %d, event_id %d\n",
  887. state, event_type, subtype.chunk);
  888. BUG();
  889. break;
  890. default:
  891. printk(KERN_ERR "sctp impossible disposition %d "
  892. "in state %d, event_type %d, event_id %d\n",
  893. status, state, event_type, subtype.chunk);
  894. BUG();
  895. break;
  896. }
  897. bail:
  898. return error;
  899. }
  900. /********************************************************************
  901. * 2nd Level Abstractions
  902. ********************************************************************/
  903. /* This is the side-effect interpreter. */
  904. static int sctp_cmd_interpreter(sctp_event_t event_type,
  905. sctp_subtype_t subtype,
  906. sctp_state_t state,
  907. struct sctp_endpoint *ep,
  908. struct sctp_association *asoc,
  909. void *event_arg,
  910. sctp_disposition_t status,
  911. sctp_cmd_seq_t *commands,
  912. gfp_t gfp)
  913. {
  914. int error = 0;
  915. int force;
  916. sctp_cmd_t *cmd;
  917. struct sctp_chunk *new_obj;
  918. struct sctp_chunk *chunk = NULL;
  919. struct sctp_packet *packet;
  920. struct list_head *pos;
  921. struct timer_list *timer;
  922. unsigned long timeout;
  923. struct sctp_transport *t;
  924. struct sctp_sackhdr sackh;
  925. int local_cork = 0;
  926. if (SCTP_EVENT_T_TIMEOUT != event_type)
  927. chunk = (struct sctp_chunk *) event_arg;
  928. /* Note: This whole file is a huge candidate for rework.
  929. * For example, each command could either have its own handler, so
  930. * the loop would look like:
  931. * while (cmds)
  932. * cmd->handle(x, y, z)
  933. * --jgrimm
  934. */
  935. while (NULL != (cmd = sctp_next_cmd(commands))) {
  936. switch (cmd->verb) {
  937. case SCTP_CMD_NOP:
  938. /* Do nothing. */
  939. break;
  940. case SCTP_CMD_NEW_ASOC:
  941. /* Register a new association. */
  942. if (local_cork) {
  943. sctp_outq_uncork(&asoc->outqueue);
  944. local_cork = 0;
  945. }
  946. asoc = cmd->obj.ptr;
  947. /* Register with the endpoint. */
  948. sctp_endpoint_add_asoc(ep, asoc);
  949. sctp_hash_established(asoc);
  950. break;
  951. case SCTP_CMD_UPDATE_ASSOC:
  952. sctp_assoc_update(asoc, cmd->obj.ptr);
  953. break;
  954. case SCTP_CMD_PURGE_OUTQUEUE:
  955. sctp_outq_teardown(&asoc->outqueue);
  956. break;
  957. case SCTP_CMD_DELETE_TCB:
  958. if (local_cork) {
  959. sctp_outq_uncork(&asoc->outqueue);
  960. local_cork = 0;
  961. }
  962. /* Delete the current association. */
  963. sctp_cmd_delete_tcb(commands, asoc);
  964. asoc = NULL;
  965. break;
  966. case SCTP_CMD_NEW_STATE:
  967. /* Enter a new state. */
  968. sctp_cmd_new_state(commands, asoc, cmd->obj.state);
  969. break;
  970. case SCTP_CMD_REPORT_TSN:
  971. /* Record the arrival of a TSN. */
  972. sctp_tsnmap_mark(&asoc->peer.tsn_map, cmd->obj.u32);
  973. break;
  974. case SCTP_CMD_REPORT_FWDTSN:
  975. /* Move the Cumulattive TSN Ack ahead. */
  976. sctp_tsnmap_skip(&asoc->peer.tsn_map, cmd->obj.u32);
  977. /* purge the fragmentation queue */
  978. sctp_ulpq_reasm_flushtsn(&asoc->ulpq, cmd->obj.u32);
  979. /* Abort any in progress partial delivery. */
  980. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  981. break;
  982. case SCTP_CMD_PROCESS_FWDTSN:
  983. sctp_cmd_process_fwdtsn(&asoc->ulpq, cmd->obj.ptr);
  984. break;
  985. case SCTP_CMD_GEN_SACK:
  986. /* Generate a Selective ACK.
  987. * The argument tells us whether to just count
  988. * the packet and MAYBE generate a SACK, or
  989. * force a SACK out.
  990. */
  991. force = cmd->obj.i32;
  992. error = sctp_gen_sack(asoc, force, commands);
  993. break;
  994. case SCTP_CMD_PROCESS_SACK:
  995. /* Process an inbound SACK. */
  996. error = sctp_cmd_process_sack(commands, asoc,
  997. cmd->obj.ptr);
  998. break;
  999. case SCTP_CMD_GEN_INIT_ACK:
  1000. /* Generate an INIT ACK chunk. */
  1001. new_obj = sctp_make_init_ack(asoc, chunk, GFP_ATOMIC,
  1002. 0);
  1003. if (!new_obj)
  1004. goto nomem;
  1005. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1006. SCTP_CHUNK(new_obj));
  1007. break;
  1008. case SCTP_CMD_PEER_INIT:
  1009. /* Process a unified INIT from the peer.
  1010. * Note: Only used during INIT-ACK processing. If
  1011. * there is an error just return to the outter
  1012. * layer which will bail.
  1013. */
  1014. error = sctp_cmd_process_init(commands, asoc, chunk,
  1015. cmd->obj.ptr, gfp);
  1016. break;
  1017. case SCTP_CMD_GEN_COOKIE_ECHO:
  1018. /* Generate a COOKIE ECHO chunk. */
  1019. new_obj = sctp_make_cookie_echo(asoc, chunk);
  1020. if (!new_obj) {
  1021. if (cmd->obj.ptr)
  1022. sctp_chunk_free(cmd->obj.ptr);
  1023. goto nomem;
  1024. }
  1025. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1026. SCTP_CHUNK(new_obj));
  1027. /* If there is an ERROR chunk to be sent along with
  1028. * the COOKIE_ECHO, send it, too.
  1029. */
  1030. if (cmd->obj.ptr)
  1031. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1032. SCTP_CHUNK(cmd->obj.ptr));
  1033. /* FIXME - Eventually come up with a cleaner way to
  1034. * enabling COOKIE-ECHO + DATA bundling during
  1035. * multihoming stale cookie scenarios, the following
  1036. * command plays with asoc->peer.retran_path to
  1037. * avoid the problem of sending the COOKIE-ECHO and
  1038. * DATA in different paths, which could result
  1039. * in the association being ABORTed if the DATA chunk
  1040. * is processed first by the server. Checking the
  1041. * init error counter simply causes this command
  1042. * to be executed only during failed attempts of
  1043. * association establishment.
  1044. */
  1045. if ((asoc->peer.retran_path !=
  1046. asoc->peer.primary_path) &&
  1047. (asoc->init_err_counter > 0)) {
  1048. sctp_add_cmd_sf(commands,
  1049. SCTP_CMD_FORCE_PRIM_RETRAN,
  1050. SCTP_NULL());
  1051. }
  1052. break;
  1053. case SCTP_CMD_GEN_SHUTDOWN:
  1054. /* Generate SHUTDOWN when in SHUTDOWN_SENT state.
  1055. * Reset error counts.
  1056. */
  1057. asoc->overall_error_count = 0;
  1058. /* Generate a SHUTDOWN chunk. */
  1059. new_obj = sctp_make_shutdown(asoc, chunk);
  1060. if (!new_obj)
  1061. goto nomem;
  1062. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1063. SCTP_CHUNK(new_obj));
  1064. break;
  1065. case SCTP_CMD_CHUNK_ULP:
  1066. /* Send a chunk to the sockets layer. */
  1067. SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
  1068. "chunk_up:", cmd->obj.ptr,
  1069. "ulpq:", &asoc->ulpq);
  1070. sctp_ulpq_tail_data(&asoc->ulpq, cmd->obj.ptr,
  1071. GFP_ATOMIC);
  1072. break;
  1073. case SCTP_CMD_EVENT_ULP:
  1074. /* Send a notification to the sockets layer. */
  1075. SCTP_DEBUG_PRINTK("sm_sideff: %s %p, %s %p.\n",
  1076. "event_up:",cmd->obj.ptr,
  1077. "ulpq:",&asoc->ulpq);
  1078. sctp_ulpq_tail_event(&asoc->ulpq, cmd->obj.ptr);
  1079. break;
  1080. case SCTP_CMD_REPLY:
  1081. /* If an caller has not already corked, do cork. */
  1082. if (!asoc->outqueue.cork) {
  1083. sctp_outq_cork(&asoc->outqueue);
  1084. local_cork = 1;
  1085. }
  1086. /* Send a chunk to our peer. */
  1087. error = sctp_outq_tail(&asoc->outqueue, cmd->obj.ptr);
  1088. break;
  1089. case SCTP_CMD_SEND_PKT:
  1090. /* Send a full packet to our peer. */
  1091. packet = cmd->obj.ptr;
  1092. sctp_packet_transmit(packet);
  1093. sctp_ootb_pkt_free(packet);
  1094. break;
  1095. case SCTP_CMD_T1_RETRAN:
  1096. /* Mark a transport for retransmission. */
  1097. sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  1098. SCTP_RTXR_T1_RTX);
  1099. break;
  1100. case SCTP_CMD_RETRAN:
  1101. /* Mark a transport for retransmission. */
  1102. sctp_retransmit(&asoc->outqueue, cmd->obj.transport,
  1103. SCTP_RTXR_T3_RTX);
  1104. break;
  1105. case SCTP_CMD_TRANSMIT:
  1106. /* Kick start transmission. */
  1107. error = sctp_outq_uncork(&asoc->outqueue);
  1108. local_cork = 0;
  1109. break;
  1110. case SCTP_CMD_ECN_CE:
  1111. /* Do delayed CE processing. */
  1112. sctp_do_ecn_ce_work(asoc, cmd->obj.u32);
  1113. break;
  1114. case SCTP_CMD_ECN_ECNE:
  1115. /* Do delayed ECNE processing. */
  1116. new_obj = sctp_do_ecn_ecne_work(asoc, cmd->obj.u32,
  1117. chunk);
  1118. if (new_obj)
  1119. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1120. SCTP_CHUNK(new_obj));
  1121. break;
  1122. case SCTP_CMD_ECN_CWR:
  1123. /* Do delayed CWR processing. */
  1124. sctp_do_ecn_cwr_work(asoc, cmd->obj.u32);
  1125. break;
  1126. case SCTP_CMD_SETUP_T2:
  1127. sctp_cmd_setup_t2(commands, asoc, cmd->obj.ptr);
  1128. break;
  1129. case SCTP_CMD_TIMER_START:
  1130. timer = &asoc->timers[cmd->obj.to];
  1131. timeout = asoc->timeouts[cmd->obj.to];
  1132. BUG_ON(!timeout);
  1133. timer->expires = jiffies + timeout;
  1134. sctp_association_hold(asoc);
  1135. add_timer(timer);
  1136. break;
  1137. case SCTP_CMD_TIMER_RESTART:
  1138. timer = &asoc->timers[cmd->obj.to];
  1139. timeout = asoc->timeouts[cmd->obj.to];
  1140. if (!mod_timer(timer, jiffies + timeout))
  1141. sctp_association_hold(asoc);
  1142. break;
  1143. case SCTP_CMD_TIMER_STOP:
  1144. timer = &asoc->timers[cmd->obj.to];
  1145. if (timer_pending(timer) && del_timer(timer))
  1146. sctp_association_put(asoc);
  1147. break;
  1148. case SCTP_CMD_INIT_CHOOSE_TRANSPORT:
  1149. chunk = cmd->obj.ptr;
  1150. t = sctp_assoc_choose_init_transport(asoc);
  1151. asoc->init_last_sent_to = t;
  1152. chunk->transport = t;
  1153. t->init_sent_count++;
  1154. break;
  1155. case SCTP_CMD_INIT_RESTART:
  1156. /* Do the needed accounting and updates
  1157. * associated with restarting an initialization
  1158. * timer. Only multiply the timeout by two if
  1159. * all transports have been tried at the current
  1160. * timeout.
  1161. */
  1162. t = asoc->init_last_sent_to;
  1163. asoc->init_err_counter++;
  1164. if (t->init_sent_count > (asoc->init_cycle + 1)) {
  1165. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] *= 2;
  1166. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] >
  1167. asoc->max_init_timeo) {
  1168. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
  1169. asoc->max_init_timeo;
  1170. }
  1171. asoc->init_cycle++;
  1172. SCTP_DEBUG_PRINTK(
  1173. "T1 INIT Timeout adjustment"
  1174. " init_err_counter: %d"
  1175. " cycle: %d"
  1176. " timeout: %ld\n",
  1177. asoc->init_err_counter,
  1178. asoc->init_cycle,
  1179. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT]);
  1180. }
  1181. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  1182. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  1183. break;
  1184. case SCTP_CMD_COOKIEECHO_RESTART:
  1185. /* Do the needed accounting and updates
  1186. * associated with restarting an initialization
  1187. * timer. Only multiply the timeout by two if
  1188. * all transports have been tried at the current
  1189. * timeout.
  1190. */
  1191. asoc->init_err_counter++;
  1192. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] *= 2;
  1193. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] >
  1194. asoc->max_init_timeo) {
  1195. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
  1196. asoc->max_init_timeo;
  1197. }
  1198. SCTP_DEBUG_PRINTK(
  1199. "T1 COOKIE Timeout adjustment"
  1200. " init_err_counter: %d"
  1201. " timeout: %ld\n",
  1202. asoc->init_err_counter,
  1203. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE]);
  1204. /* If we've sent any data bundled with
  1205. * COOKIE-ECHO we need to resend.
  1206. */
  1207. list_for_each(pos, &asoc->peer.transport_addr_list) {
  1208. t = list_entry(pos, struct sctp_transport,
  1209. transports);
  1210. sctp_retransmit_mark(&asoc->outqueue, t,
  1211. SCTP_RTXR_T1_RTX);
  1212. }
  1213. sctp_add_cmd_sf(commands,
  1214. SCTP_CMD_TIMER_RESTART,
  1215. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  1216. break;
  1217. case SCTP_CMD_INIT_FAILED:
  1218. sctp_cmd_init_failed(commands, asoc, cmd->obj.err);
  1219. break;
  1220. case SCTP_CMD_ASSOC_FAILED:
  1221. sctp_cmd_assoc_failed(commands, asoc, event_type,
  1222. subtype, chunk, cmd->obj.err);
  1223. break;
  1224. case SCTP_CMD_INIT_COUNTER_INC:
  1225. asoc->init_err_counter++;
  1226. break;
  1227. case SCTP_CMD_INIT_COUNTER_RESET:
  1228. asoc->init_err_counter = 0;
  1229. asoc->init_cycle = 0;
  1230. break;
  1231. case SCTP_CMD_REPORT_DUP:
  1232. sctp_tsnmap_mark_dup(&asoc->peer.tsn_map,
  1233. cmd->obj.u32);
  1234. break;
  1235. case SCTP_CMD_REPORT_BAD_TAG:
  1236. SCTP_DEBUG_PRINTK("vtag mismatch!\n");
  1237. break;
  1238. case SCTP_CMD_STRIKE:
  1239. /* Mark one strike against a transport. */
  1240. sctp_do_8_2_transport_strike(asoc, cmd->obj.transport);
  1241. break;
  1242. case SCTP_CMD_TRANSPORT_RESET:
  1243. t = cmd->obj.transport;
  1244. sctp_cmd_transport_reset(commands, asoc, t);
  1245. break;
  1246. case SCTP_CMD_TRANSPORT_ON:
  1247. t = cmd->obj.transport;
  1248. sctp_cmd_transport_on(commands, asoc, t, chunk);
  1249. break;
  1250. case SCTP_CMD_HB_TIMERS_START:
  1251. sctp_cmd_hb_timers_start(commands, asoc);
  1252. break;
  1253. case SCTP_CMD_HB_TIMER_UPDATE:
  1254. t = cmd->obj.transport;
  1255. sctp_cmd_hb_timer_update(commands, asoc, t);
  1256. break;
  1257. case SCTP_CMD_HB_TIMERS_STOP:
  1258. sctp_cmd_hb_timers_stop(commands, asoc);
  1259. break;
  1260. case SCTP_CMD_REPORT_ERROR:
  1261. error = cmd->obj.error;
  1262. break;
  1263. case SCTP_CMD_PROCESS_CTSN:
  1264. /* Dummy up a SACK for processing. */
  1265. sackh.cum_tsn_ack = cmd->obj.be32;
  1266. sackh.a_rwnd = 0;
  1267. sackh.num_gap_ack_blocks = 0;
  1268. sackh.num_dup_tsns = 0;
  1269. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK,
  1270. SCTP_SACKH(&sackh));
  1271. break;
  1272. case SCTP_CMD_DISCARD_PACKET:
  1273. /* We need to discard the whole packet. */
  1274. chunk->pdiscard = 1;
  1275. break;
  1276. case SCTP_CMD_RTO_PENDING:
  1277. t = cmd->obj.transport;
  1278. t->rto_pending = 1;
  1279. break;
  1280. case SCTP_CMD_PART_DELIVER:
  1281. sctp_ulpq_partial_delivery(&asoc->ulpq, cmd->obj.ptr,
  1282. GFP_ATOMIC);
  1283. break;
  1284. case SCTP_CMD_RENEGE:
  1285. sctp_ulpq_renege(&asoc->ulpq, cmd->obj.ptr,
  1286. GFP_ATOMIC);
  1287. break;
  1288. case SCTP_CMD_SETUP_T4:
  1289. sctp_cmd_setup_t4(commands, asoc, cmd->obj.ptr);
  1290. break;
  1291. case SCTP_CMD_PROCESS_OPERR:
  1292. sctp_cmd_process_operr(commands, asoc, chunk);
  1293. break;
  1294. case SCTP_CMD_CLEAR_INIT_TAG:
  1295. asoc->peer.i.init_tag = 0;
  1296. break;
  1297. case SCTP_CMD_DEL_NON_PRIMARY:
  1298. sctp_cmd_del_non_primary(asoc);
  1299. break;
  1300. case SCTP_CMD_T3_RTX_TIMERS_STOP:
  1301. sctp_cmd_t3_rtx_timers_stop(commands, asoc);
  1302. break;
  1303. case SCTP_CMD_FORCE_PRIM_RETRAN:
  1304. t = asoc->peer.retran_path;
  1305. asoc->peer.retran_path = asoc->peer.primary_path;
  1306. error = sctp_outq_uncork(&asoc->outqueue);
  1307. local_cork = 0;
  1308. asoc->peer.retran_path = t;
  1309. break;
  1310. case SCTP_CMD_SET_SK_ERR:
  1311. sctp_cmd_set_sk_err(asoc, cmd->obj.error);
  1312. break;
  1313. case SCTP_CMD_ASSOC_CHANGE:
  1314. sctp_cmd_assoc_change(commands, asoc,
  1315. cmd->obj.u8);
  1316. break;
  1317. case SCTP_CMD_ADAPTATION_IND:
  1318. sctp_cmd_adaptation_ind(commands, asoc);
  1319. break;
  1320. case SCTP_CMD_ASSOC_SHKEY:
  1321. error = sctp_auth_asoc_init_active_key(asoc,
  1322. GFP_ATOMIC);
  1323. break;
  1324. default:
  1325. printk(KERN_WARNING "Impossible command: %u, %p\n",
  1326. cmd->verb, cmd->obj.ptr);
  1327. break;
  1328. }
  1329. if (error)
  1330. break;
  1331. }
  1332. out:
  1333. if (local_cork)
  1334. sctp_outq_uncork(&asoc->outqueue);
  1335. return error;
  1336. nomem:
  1337. error = -ENOMEM;
  1338. goto out;
  1339. }