sm_sideeffect.c 44 KB

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