sm_sideeffect.c 43 KB

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