sm_sideeffect.c 48 KB

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