sm_sideeffect.c 49 KB

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