sm_sideeffect.c 42 KB

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