sm_sideeffect.c 45 KB

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