sm_sideeffect.c 44 KB

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