sm_sideeffect.c 42 KB

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