sm_sideeffect.c 46 KB

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