sm_sideeffect.c 41 KB

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