associola.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. * Copyright (c) 2001 La Monte H.P. Yarroll
  7. *
  8. * This file is part of the SCTP kernel implementation
  9. *
  10. * This module provides the abstraction for an SCTP association.
  11. *
  12. * This SCTP implementation is free software;
  13. * you can redistribute it and/or modify it under the terms of
  14. * the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2, or (at your option)
  16. * any later version.
  17. *
  18. * This SCTP implementation is distributed in the hope that it
  19. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  20. * ************************
  21. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with GNU CC; see the file COPYING. If not, write to
  26. * the Free Software Foundation, 59 Temple Place - Suite 330,
  27. * Boston, MA 02111-1307, USA.
  28. *
  29. * Please send any bug reports or fixes you make to the
  30. * email address(es):
  31. * lksctp developers <linux-sctp@vger.kernel.org>
  32. *
  33. * Written or modified by:
  34. * La Monte H.P. Yarroll <piggy@acm.org>
  35. * Karl Knutson <karl@athena.chicago.il.us>
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * Xingang Guo <xingang.guo@intel.com>
  38. * Hui Huang <hui.huang@nokia.com>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. * Daisy Chang <daisyc@us.ibm.com>
  41. * Ryan Layer <rmlayer@us.ibm.com>
  42. * Kevin Gao <kevin.gao@intel.com>
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/types.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/poll.h>
  48. #include <linux/init.h>
  49. #include <linux/slab.h>
  50. #include <linux/in.h>
  51. #include <net/ipv6.h>
  52. #include <net/sctp/sctp.h>
  53. #include <net/sctp/sm.h>
  54. /* Forward declarations for internal functions. */
  55. static void sctp_assoc_bh_rcv(struct work_struct *work);
  56. static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc);
  57. static void sctp_assoc_free_asconf_queue(struct sctp_association *asoc);
  58. /* 1st Level Abstractions. */
  59. /* Initialize a new association from provided memory. */
  60. static struct sctp_association *sctp_association_init(struct sctp_association *asoc,
  61. const struct sctp_endpoint *ep,
  62. const struct sock *sk,
  63. sctp_scope_t scope,
  64. gfp_t gfp)
  65. {
  66. struct net *net = sock_net(sk);
  67. struct sctp_sock *sp;
  68. int i;
  69. sctp_paramhdr_t *p;
  70. int err;
  71. /* Retrieve the SCTP per socket area. */
  72. sp = sctp_sk((struct sock *)sk);
  73. /* Discarding const is appropriate here. */
  74. asoc->ep = (struct sctp_endpoint *)ep;
  75. asoc->base.sk = (struct sock *)sk;
  76. sctp_endpoint_hold(asoc->ep);
  77. sock_hold(asoc->base.sk);
  78. /* Initialize the common base substructure. */
  79. asoc->base.type = SCTP_EP_TYPE_ASSOCIATION;
  80. /* Initialize the object handling fields. */
  81. atomic_set(&asoc->base.refcnt, 1);
  82. asoc->base.dead = false;
  83. /* Initialize the bind addr area. */
  84. sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port);
  85. asoc->state = SCTP_STATE_CLOSED;
  86. asoc->cookie_life = ms_to_ktime(sp->assocparams.sasoc_cookie_life);
  87. asoc->frag_point = 0;
  88. asoc->user_frag = sp->user_frag;
  89. /* Set the association max_retrans and RTO values from the
  90. * socket values.
  91. */
  92. asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt;
  93. asoc->pf_retrans = net->sctp.pf_retrans;
  94. asoc->rto_initial = msecs_to_jiffies(sp->rtoinfo.srto_initial);
  95. asoc->rto_max = msecs_to_jiffies(sp->rtoinfo.srto_max);
  96. asoc->rto_min = msecs_to_jiffies(sp->rtoinfo.srto_min);
  97. asoc->overall_error_count = 0;
  98. /* Initialize the association's heartbeat interval based on the
  99. * sock configured value.
  100. */
  101. asoc->hbinterval = msecs_to_jiffies(sp->hbinterval);
  102. /* Initialize path max retrans value. */
  103. asoc->pathmaxrxt = sp->pathmaxrxt;
  104. /* Initialize default path MTU. */
  105. asoc->pathmtu = sp->pathmtu;
  106. /* Set association default SACK delay */
  107. asoc->sackdelay = msecs_to_jiffies(sp->sackdelay);
  108. asoc->sackfreq = sp->sackfreq;
  109. /* Set the association default flags controlling
  110. * Heartbeat, SACK delay, and Path MTU Discovery.
  111. */
  112. asoc->param_flags = sp->param_flags;
  113. /* Initialize the maximum mumber of new data packets that can be sent
  114. * in a burst.
  115. */
  116. asoc->max_burst = sp->max_burst;
  117. /* initialize association timers */
  118. asoc->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
  119. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = asoc->rto_initial;
  120. asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = asoc->rto_initial;
  121. asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = asoc->rto_initial;
  122. asoc->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
  123. asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
  124. /* sctpimpguide Section 2.12.2
  125. * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
  126. * recommended value of 5 times 'RTO.Max'.
  127. */
  128. asoc->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
  129. = 5 * asoc->rto_max;
  130. asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
  131. asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = asoc->sackdelay;
  132. asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
  133. min_t(unsigned long, sp->autoclose, net->sctp.max_autoclose) * HZ;
  134. /* Initializes the timers */
  135. for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i)
  136. setup_timer(&asoc->timers[i], sctp_timer_events[i],
  137. (unsigned long)asoc);
  138. /* Pull default initialization values from the sock options.
  139. * Note: This assumes that the values have already been
  140. * validated in the sock.
  141. */
  142. asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams;
  143. asoc->c.sinit_num_ostreams = sp->initmsg.sinit_num_ostreams;
  144. asoc->max_init_attempts = sp->initmsg.sinit_max_attempts;
  145. asoc->max_init_timeo =
  146. msecs_to_jiffies(sp->initmsg.sinit_max_init_timeo);
  147. /* Allocate storage for the ssnmap after the inbound and outbound
  148. * streams have been negotiated during Init.
  149. */
  150. asoc->ssnmap = NULL;
  151. /* Set the local window size for receive.
  152. * This is also the rcvbuf space per association.
  153. * RFC 6 - A SCTP receiver MUST be able to receive a minimum of
  154. * 1500 bytes in one SCTP packet.
  155. */
  156. if ((sk->sk_rcvbuf/2) < SCTP_DEFAULT_MINWINDOW)
  157. asoc->rwnd = SCTP_DEFAULT_MINWINDOW;
  158. else
  159. asoc->rwnd = sk->sk_rcvbuf/2;
  160. asoc->a_rwnd = asoc->rwnd;
  161. asoc->rwnd_over = 0;
  162. asoc->rwnd_press = 0;
  163. /* Use my own max window until I learn something better. */
  164. asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW;
  165. /* Set the sndbuf size for transmit. */
  166. asoc->sndbuf_used = 0;
  167. /* Initialize the receive memory counter */
  168. atomic_set(&asoc->rmem_alloc, 0);
  169. init_waitqueue_head(&asoc->wait);
  170. asoc->c.my_vtag = sctp_generate_tag(ep);
  171. asoc->peer.i.init_tag = 0; /* INIT needs a vtag of 0. */
  172. asoc->c.peer_vtag = 0;
  173. asoc->c.my_ttag = 0;
  174. asoc->c.peer_ttag = 0;
  175. asoc->c.my_port = ep->base.bind_addr.port;
  176. asoc->c.initial_tsn = sctp_generate_tsn(ep);
  177. asoc->next_tsn = asoc->c.initial_tsn;
  178. asoc->ctsn_ack_point = asoc->next_tsn - 1;
  179. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  180. asoc->highest_sacked = asoc->ctsn_ack_point;
  181. asoc->last_cwr_tsn = asoc->ctsn_ack_point;
  182. asoc->unack_data = 0;
  183. /* ADDIP Section 4.1 Asconf Chunk Procedures
  184. *
  185. * When an endpoint has an ASCONF signaled change to be sent to the
  186. * remote endpoint it should do the following:
  187. * ...
  188. * A2) a serial number should be assigned to the chunk. The serial
  189. * number SHOULD be a monotonically increasing number. The serial
  190. * numbers SHOULD be initialized at the start of the
  191. * association to the same value as the initial TSN.
  192. */
  193. asoc->addip_serial = asoc->c.initial_tsn;
  194. INIT_LIST_HEAD(&asoc->addip_chunk_list);
  195. INIT_LIST_HEAD(&asoc->asconf_ack_list);
  196. /* Make an empty list of remote transport addresses. */
  197. INIT_LIST_HEAD(&asoc->peer.transport_addr_list);
  198. asoc->peer.transport_count = 0;
  199. /* RFC 2960 5.1 Normal Establishment of an Association
  200. *
  201. * After the reception of the first data chunk in an
  202. * association the endpoint must immediately respond with a
  203. * sack to acknowledge the data chunk. Subsequent
  204. * acknowledgements should be done as described in Section
  205. * 6.2.
  206. *
  207. * [We implement this by telling a new association that it
  208. * already received one packet.]
  209. */
  210. asoc->peer.sack_needed = 1;
  211. asoc->peer.sack_cnt = 0;
  212. asoc->peer.sack_generation = 1;
  213. /* Assume that the peer will tell us if he recognizes ASCONF
  214. * as part of INIT exchange.
  215. * The sctp_addip_noauth option is there for backward compatibilty
  216. * and will revert old behavior.
  217. */
  218. asoc->peer.asconf_capable = 0;
  219. if (net->sctp.addip_noauth)
  220. asoc->peer.asconf_capable = 1;
  221. asoc->asconf_addr_del_pending = NULL;
  222. asoc->src_out_of_asoc_ok = 0;
  223. asoc->new_transport = NULL;
  224. /* Create an input queue. */
  225. sctp_inq_init(&asoc->base.inqueue);
  226. sctp_inq_set_th_handler(&asoc->base.inqueue, sctp_assoc_bh_rcv);
  227. /* Create an output queue. */
  228. sctp_outq_init(asoc, &asoc->outqueue);
  229. if (!sctp_ulpq_init(&asoc->ulpq, asoc))
  230. goto fail_init;
  231. memset(&asoc->peer.tsn_map, 0, sizeof(struct sctp_tsnmap));
  232. asoc->need_ecne = 0;
  233. asoc->assoc_id = 0;
  234. /* Assume that peer would support both address types unless we are
  235. * told otherwise.
  236. */
  237. asoc->peer.ipv4_address = 1;
  238. if (asoc->base.sk->sk_family == PF_INET6)
  239. asoc->peer.ipv6_address = 1;
  240. INIT_LIST_HEAD(&asoc->asocs);
  241. asoc->autoclose = sp->autoclose;
  242. asoc->default_stream = sp->default_stream;
  243. asoc->default_ppid = sp->default_ppid;
  244. asoc->default_flags = sp->default_flags;
  245. asoc->default_context = sp->default_context;
  246. asoc->default_timetolive = sp->default_timetolive;
  247. asoc->default_rcv_context = sp->default_rcv_context;
  248. /* SCTP_GET_ASSOC_STATS COUNTERS */
  249. memset(&asoc->stats, 0, sizeof(struct sctp_priv_assoc_stats));
  250. /* AUTH related initializations */
  251. INIT_LIST_HEAD(&asoc->endpoint_shared_keys);
  252. err = sctp_auth_asoc_copy_shkeys(ep, asoc, gfp);
  253. if (err)
  254. goto fail_init;
  255. asoc->active_key_id = ep->active_key_id;
  256. asoc->asoc_shared_key = NULL;
  257. asoc->default_hmac_id = 0;
  258. /* Save the hmacs and chunks list into this association */
  259. if (ep->auth_hmacs_list)
  260. memcpy(asoc->c.auth_hmacs, ep->auth_hmacs_list,
  261. ntohs(ep->auth_hmacs_list->param_hdr.length));
  262. if (ep->auth_chunk_list)
  263. memcpy(asoc->c.auth_chunks, ep->auth_chunk_list,
  264. ntohs(ep->auth_chunk_list->param_hdr.length));
  265. /* Get the AUTH random number for this association */
  266. p = (sctp_paramhdr_t *)asoc->c.auth_random;
  267. p->type = SCTP_PARAM_RANDOM;
  268. p->length = htons(sizeof(sctp_paramhdr_t) + SCTP_AUTH_RANDOM_LENGTH);
  269. get_random_bytes(p+1, SCTP_AUTH_RANDOM_LENGTH);
  270. return asoc;
  271. fail_init:
  272. sock_put(asoc->base.sk);
  273. sctp_endpoint_put(asoc->ep);
  274. return NULL;
  275. }
  276. /* Allocate and initialize a new association */
  277. struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep,
  278. const struct sock *sk,
  279. sctp_scope_t scope,
  280. gfp_t gfp)
  281. {
  282. struct sctp_association *asoc;
  283. asoc = kzalloc(sizeof(*asoc), gfp);
  284. if (!asoc)
  285. goto fail;
  286. if (!sctp_association_init(asoc, ep, sk, scope, gfp))
  287. goto fail_init;
  288. SCTP_DBG_OBJCNT_INC(assoc);
  289. pr_debug("Created asoc %p\n", asoc);
  290. return asoc;
  291. fail_init:
  292. kfree(asoc);
  293. fail:
  294. return NULL;
  295. }
  296. /* Free this association if possible. There may still be users, so
  297. * the actual deallocation may be delayed.
  298. */
  299. void sctp_association_free(struct sctp_association *asoc)
  300. {
  301. struct sock *sk = asoc->base.sk;
  302. struct sctp_transport *transport;
  303. struct list_head *pos, *temp;
  304. int i;
  305. /* Only real associations count against the endpoint, so
  306. * don't bother for if this is a temporary association.
  307. */
  308. if (!asoc->temp) {
  309. list_del(&asoc->asocs);
  310. /* Decrement the backlog value for a TCP-style listening
  311. * socket.
  312. */
  313. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  314. sk->sk_ack_backlog--;
  315. }
  316. /* Mark as dead, so other users can know this structure is
  317. * going away.
  318. */
  319. asoc->base.dead = true;
  320. /* Dispose of any data lying around in the outqueue. */
  321. sctp_outq_free(&asoc->outqueue);
  322. /* Dispose of any pending messages for the upper layer. */
  323. sctp_ulpq_free(&asoc->ulpq);
  324. /* Dispose of any pending chunks on the inqueue. */
  325. sctp_inq_free(&asoc->base.inqueue);
  326. sctp_tsnmap_free(&asoc->peer.tsn_map);
  327. /* Free ssnmap storage. */
  328. sctp_ssnmap_free(asoc->ssnmap);
  329. /* Clean up the bound address list. */
  330. sctp_bind_addr_free(&asoc->base.bind_addr);
  331. /* Do we need to go through all of our timers and
  332. * delete them? To be safe we will try to delete all, but we
  333. * should be able to go through and make a guess based
  334. * on our state.
  335. */
  336. for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
  337. if (del_timer(&asoc->timers[i]))
  338. sctp_association_put(asoc);
  339. }
  340. /* Free peer's cached cookie. */
  341. kfree(asoc->peer.cookie);
  342. kfree(asoc->peer.peer_random);
  343. kfree(asoc->peer.peer_chunks);
  344. kfree(asoc->peer.peer_hmacs);
  345. /* Release the transport structures. */
  346. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  347. transport = list_entry(pos, struct sctp_transport, transports);
  348. list_del_rcu(pos);
  349. sctp_transport_free(transport);
  350. }
  351. asoc->peer.transport_count = 0;
  352. sctp_asconf_queue_teardown(asoc);
  353. /* Free pending address space being deleted */
  354. if (asoc->asconf_addr_del_pending != NULL)
  355. kfree(asoc->asconf_addr_del_pending);
  356. /* AUTH - Free the endpoint shared keys */
  357. sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
  358. /* AUTH - Free the association shared key */
  359. sctp_auth_key_put(asoc->asoc_shared_key);
  360. sctp_association_put(asoc);
  361. }
  362. /* Cleanup and free up an association. */
  363. static void sctp_association_destroy(struct sctp_association *asoc)
  364. {
  365. if (unlikely(!asoc->base.dead)) {
  366. WARN(1, "Attempt to destroy undead association %p!\n", asoc);
  367. return;
  368. }
  369. sctp_endpoint_put(asoc->ep);
  370. sock_put(asoc->base.sk);
  371. if (asoc->assoc_id != 0) {
  372. spin_lock_bh(&sctp_assocs_id_lock);
  373. idr_remove(&sctp_assocs_id, asoc->assoc_id);
  374. spin_unlock_bh(&sctp_assocs_id_lock);
  375. }
  376. WARN_ON(atomic_read(&asoc->rmem_alloc));
  377. kfree(asoc);
  378. SCTP_DBG_OBJCNT_DEC(assoc);
  379. }
  380. /* Change the primary destination address for the peer. */
  381. void sctp_assoc_set_primary(struct sctp_association *asoc,
  382. struct sctp_transport *transport)
  383. {
  384. int changeover = 0;
  385. /* it's a changeover only if we already have a primary path
  386. * that we are changing
  387. */
  388. if (asoc->peer.primary_path != NULL &&
  389. asoc->peer.primary_path != transport)
  390. changeover = 1 ;
  391. asoc->peer.primary_path = transport;
  392. /* Set a default msg_name for events. */
  393. memcpy(&asoc->peer.primary_addr, &transport->ipaddr,
  394. sizeof(union sctp_addr));
  395. /* If the primary path is changing, assume that the
  396. * user wants to use this new path.
  397. */
  398. if ((transport->state == SCTP_ACTIVE) ||
  399. (transport->state == SCTP_UNKNOWN))
  400. asoc->peer.active_path = transport;
  401. /*
  402. * SFR-CACC algorithm:
  403. * Upon the receipt of a request to change the primary
  404. * destination address, on the data structure for the new
  405. * primary destination, the sender MUST do the following:
  406. *
  407. * 1) If CHANGEOVER_ACTIVE is set, then there was a switch
  408. * to this destination address earlier. The sender MUST set
  409. * CYCLING_CHANGEOVER to indicate that this switch is a
  410. * double switch to the same destination address.
  411. *
  412. * Really, only bother is we have data queued or outstanding on
  413. * the association.
  414. */
  415. if (!asoc->outqueue.outstanding_bytes && !asoc->outqueue.out_qlen)
  416. return;
  417. if (transport->cacc.changeover_active)
  418. transport->cacc.cycling_changeover = changeover;
  419. /* 2) The sender MUST set CHANGEOVER_ACTIVE to indicate that
  420. * a changeover has occurred.
  421. */
  422. transport->cacc.changeover_active = changeover;
  423. /* 3) The sender MUST store the next TSN to be sent in
  424. * next_tsn_at_change.
  425. */
  426. transport->cacc.next_tsn_at_change = asoc->next_tsn;
  427. }
  428. /* Remove a transport from an association. */
  429. void sctp_assoc_rm_peer(struct sctp_association *asoc,
  430. struct sctp_transport *peer)
  431. {
  432. struct list_head *pos;
  433. struct sctp_transport *transport;
  434. pr_debug("%s: association:%p addr:%pISpc\n",
  435. __func__, asoc, &peer->ipaddr.sa);
  436. /* If we are to remove the current retran_path, update it
  437. * to the next peer before removing this peer from the list.
  438. */
  439. if (asoc->peer.retran_path == peer)
  440. sctp_assoc_update_retran_path(asoc);
  441. /* Remove this peer from the list. */
  442. list_del_rcu(&peer->transports);
  443. /* Get the first transport of asoc. */
  444. pos = asoc->peer.transport_addr_list.next;
  445. transport = list_entry(pos, struct sctp_transport, transports);
  446. /* Update any entries that match the peer to be deleted. */
  447. if (asoc->peer.primary_path == peer)
  448. sctp_assoc_set_primary(asoc, transport);
  449. if (asoc->peer.active_path == peer)
  450. asoc->peer.active_path = transport;
  451. if (asoc->peer.retran_path == peer)
  452. asoc->peer.retran_path = transport;
  453. if (asoc->peer.last_data_from == peer)
  454. asoc->peer.last_data_from = transport;
  455. /* If we remove the transport an INIT was last sent to, set it to
  456. * NULL. Combined with the update of the retran path above, this
  457. * will cause the next INIT to be sent to the next available
  458. * transport, maintaining the cycle.
  459. */
  460. if (asoc->init_last_sent_to == peer)
  461. asoc->init_last_sent_to = NULL;
  462. /* If we remove the transport an SHUTDOWN was last sent to, set it
  463. * to NULL. Combined with the update of the retran path above, this
  464. * will cause the next SHUTDOWN to be sent to the next available
  465. * transport, maintaining the cycle.
  466. */
  467. if (asoc->shutdown_last_sent_to == peer)
  468. asoc->shutdown_last_sent_to = NULL;
  469. /* If we remove the transport an ASCONF was last sent to, set it to
  470. * NULL.
  471. */
  472. if (asoc->addip_last_asconf &&
  473. asoc->addip_last_asconf->transport == peer)
  474. asoc->addip_last_asconf->transport = NULL;
  475. /* If we have something on the transmitted list, we have to
  476. * save it off. The best place is the active path.
  477. */
  478. if (!list_empty(&peer->transmitted)) {
  479. struct sctp_transport *active = asoc->peer.active_path;
  480. struct sctp_chunk *ch;
  481. /* Reset the transport of each chunk on this list */
  482. list_for_each_entry(ch, &peer->transmitted,
  483. transmitted_list) {
  484. ch->transport = NULL;
  485. ch->rtt_in_progress = 0;
  486. }
  487. list_splice_tail_init(&peer->transmitted,
  488. &active->transmitted);
  489. /* Start a T3 timer here in case it wasn't running so
  490. * that these migrated packets have a chance to get
  491. * retrnasmitted.
  492. */
  493. if (!timer_pending(&active->T3_rtx_timer))
  494. if (!mod_timer(&active->T3_rtx_timer,
  495. jiffies + active->rto))
  496. sctp_transport_hold(active);
  497. }
  498. asoc->peer.transport_count--;
  499. sctp_transport_free(peer);
  500. }
  501. /* Add a transport address to an association. */
  502. struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
  503. const union sctp_addr *addr,
  504. const gfp_t gfp,
  505. const int peer_state)
  506. {
  507. struct net *net = sock_net(asoc->base.sk);
  508. struct sctp_transport *peer;
  509. struct sctp_sock *sp;
  510. unsigned short port;
  511. sp = sctp_sk(asoc->base.sk);
  512. /* AF_INET and AF_INET6 share common port field. */
  513. port = ntohs(addr->v4.sin_port);
  514. pr_debug("%s: association:%p addr:%pISpc state:%d\n", __func__,
  515. asoc, &addr->sa, peer_state);
  516. /* Set the port if it has not been set yet. */
  517. if (0 == asoc->peer.port)
  518. asoc->peer.port = port;
  519. /* Check to see if this is a duplicate. */
  520. peer = sctp_assoc_lookup_paddr(asoc, addr);
  521. if (peer) {
  522. /* An UNKNOWN state is only set on transports added by
  523. * user in sctp_connectx() call. Such transports should be
  524. * considered CONFIRMED per RFC 4960, Section 5.4.
  525. */
  526. if (peer->state == SCTP_UNKNOWN) {
  527. peer->state = SCTP_ACTIVE;
  528. }
  529. return peer;
  530. }
  531. peer = sctp_transport_new(net, addr, gfp);
  532. if (!peer)
  533. return NULL;
  534. sctp_transport_set_owner(peer, asoc);
  535. /* Initialize the peer's heartbeat interval based on the
  536. * association configured value.
  537. */
  538. peer->hbinterval = asoc->hbinterval;
  539. /* Set the path max_retrans. */
  540. peer->pathmaxrxt = asoc->pathmaxrxt;
  541. /* And the partial failure retrnas threshold */
  542. peer->pf_retrans = asoc->pf_retrans;
  543. /* Initialize the peer's SACK delay timeout based on the
  544. * association configured value.
  545. */
  546. peer->sackdelay = asoc->sackdelay;
  547. peer->sackfreq = asoc->sackfreq;
  548. /* Enable/disable heartbeat, SACK delay, and path MTU discovery
  549. * based on association setting.
  550. */
  551. peer->param_flags = asoc->param_flags;
  552. sctp_transport_route(peer, NULL, sp);
  553. /* Initialize the pmtu of the transport. */
  554. if (peer->param_flags & SPP_PMTUD_DISABLE) {
  555. if (asoc->pathmtu)
  556. peer->pathmtu = asoc->pathmtu;
  557. else
  558. peer->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
  559. }
  560. /* If this is the first transport addr on this association,
  561. * initialize the association PMTU to the peer's PMTU.
  562. * If not and the current association PMTU is higher than the new
  563. * peer's PMTU, reset the association PMTU to the new peer's PMTU.
  564. */
  565. if (asoc->pathmtu)
  566. asoc->pathmtu = min_t(int, peer->pathmtu, asoc->pathmtu);
  567. else
  568. asoc->pathmtu = peer->pathmtu;
  569. pr_debug("%s: association:%p PMTU set to %d\n", __func__, asoc,
  570. asoc->pathmtu);
  571. peer->pmtu_pending = 0;
  572. asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
  573. /* The asoc->peer.port might not be meaningful yet, but
  574. * initialize the packet structure anyway.
  575. */
  576. sctp_packet_init(&peer->packet, peer, asoc->base.bind_addr.port,
  577. asoc->peer.port);
  578. /* 7.2.1 Slow-Start
  579. *
  580. * o The initial cwnd before DATA transmission or after a sufficiently
  581. * long idle period MUST be set to
  582. * min(4*MTU, max(2*MTU, 4380 bytes))
  583. *
  584. * o The initial value of ssthresh MAY be arbitrarily high
  585. * (for example, implementations MAY use the size of the
  586. * receiver advertised window).
  587. */
  588. peer->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
  589. /* At this point, we may not have the receiver's advertised window,
  590. * so initialize ssthresh to the default value and it will be set
  591. * later when we process the INIT.
  592. */
  593. peer->ssthresh = SCTP_DEFAULT_MAXWINDOW;
  594. peer->partial_bytes_acked = 0;
  595. peer->flight_size = 0;
  596. peer->burst_limited = 0;
  597. /* Set the transport's RTO.initial value */
  598. peer->rto = asoc->rto_initial;
  599. sctp_max_rto(asoc, peer);
  600. /* Set the peer's active state. */
  601. peer->state = peer_state;
  602. /* Attach the remote transport to our asoc. */
  603. list_add_tail_rcu(&peer->transports, &asoc->peer.transport_addr_list);
  604. asoc->peer.transport_count++;
  605. /* If we do not yet have a primary path, set one. */
  606. if (!asoc->peer.primary_path) {
  607. sctp_assoc_set_primary(asoc, peer);
  608. asoc->peer.retran_path = peer;
  609. }
  610. if (asoc->peer.active_path == asoc->peer.retran_path &&
  611. peer->state != SCTP_UNCONFIRMED) {
  612. asoc->peer.retran_path = peer;
  613. }
  614. return peer;
  615. }
  616. /* Delete a transport address from an association. */
  617. void sctp_assoc_del_peer(struct sctp_association *asoc,
  618. const union sctp_addr *addr)
  619. {
  620. struct list_head *pos;
  621. struct list_head *temp;
  622. struct sctp_transport *transport;
  623. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  624. transport = list_entry(pos, struct sctp_transport, transports);
  625. if (sctp_cmp_addr_exact(addr, &transport->ipaddr)) {
  626. /* Do book keeping for removing the peer and free it. */
  627. sctp_assoc_rm_peer(asoc, transport);
  628. break;
  629. }
  630. }
  631. }
  632. /* Lookup a transport by address. */
  633. struct sctp_transport *sctp_assoc_lookup_paddr(
  634. const struct sctp_association *asoc,
  635. const union sctp_addr *address)
  636. {
  637. struct sctp_transport *t;
  638. /* Cycle through all transports searching for a peer address. */
  639. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  640. transports) {
  641. if (sctp_cmp_addr_exact(address, &t->ipaddr))
  642. return t;
  643. }
  644. return NULL;
  645. }
  646. /* Remove all transports except a give one */
  647. void sctp_assoc_del_nonprimary_peers(struct sctp_association *asoc,
  648. struct sctp_transport *primary)
  649. {
  650. struct sctp_transport *temp;
  651. struct sctp_transport *t;
  652. list_for_each_entry_safe(t, temp, &asoc->peer.transport_addr_list,
  653. transports) {
  654. /* if the current transport is not the primary one, delete it */
  655. if (t != primary)
  656. sctp_assoc_rm_peer(asoc, t);
  657. }
  658. }
  659. /* Engage in transport control operations.
  660. * Mark the transport up or down and send a notification to the user.
  661. * Select and update the new active and retran paths.
  662. */
  663. void sctp_assoc_control_transport(struct sctp_association *asoc,
  664. struct sctp_transport *transport,
  665. sctp_transport_cmd_t command,
  666. sctp_sn_error_t error)
  667. {
  668. struct sctp_transport *t = NULL;
  669. struct sctp_transport *first;
  670. struct sctp_transport *second;
  671. struct sctp_ulpevent *event;
  672. struct sockaddr_storage addr;
  673. int spc_state = 0;
  674. bool ulp_notify = true;
  675. /* Record the transition on the transport. */
  676. switch (command) {
  677. case SCTP_TRANSPORT_UP:
  678. /* If we are moving from UNCONFIRMED state due
  679. * to heartbeat success, report the SCTP_ADDR_CONFIRMED
  680. * state to the user, otherwise report SCTP_ADDR_AVAILABLE.
  681. */
  682. if (SCTP_UNCONFIRMED == transport->state &&
  683. SCTP_HEARTBEAT_SUCCESS == error)
  684. spc_state = SCTP_ADDR_CONFIRMED;
  685. else
  686. spc_state = SCTP_ADDR_AVAILABLE;
  687. /* Don't inform ULP about transition from PF to
  688. * active state and set cwnd to 1 MTU, see SCTP
  689. * Quick failover draft section 5.1, point 5
  690. */
  691. if (transport->state == SCTP_PF) {
  692. ulp_notify = false;
  693. transport->cwnd = asoc->pathmtu;
  694. }
  695. transport->state = SCTP_ACTIVE;
  696. break;
  697. case SCTP_TRANSPORT_DOWN:
  698. /* If the transport was never confirmed, do not transition it
  699. * to inactive state. Also, release the cached route since
  700. * there may be a better route next time.
  701. */
  702. if (transport->state != SCTP_UNCONFIRMED)
  703. transport->state = SCTP_INACTIVE;
  704. else {
  705. dst_release(transport->dst);
  706. transport->dst = NULL;
  707. }
  708. spc_state = SCTP_ADDR_UNREACHABLE;
  709. break;
  710. case SCTP_TRANSPORT_PF:
  711. transport->state = SCTP_PF;
  712. ulp_notify = false;
  713. break;
  714. default:
  715. return;
  716. }
  717. /* Generate and send a SCTP_PEER_ADDR_CHANGE notification to the
  718. * user.
  719. */
  720. if (ulp_notify) {
  721. memset(&addr, 0, sizeof(struct sockaddr_storage));
  722. memcpy(&addr, &transport->ipaddr,
  723. transport->af_specific->sockaddr_len);
  724. event = sctp_ulpevent_make_peer_addr_change(asoc, &addr,
  725. 0, spc_state, error, GFP_ATOMIC);
  726. if (event)
  727. sctp_ulpq_tail_event(&asoc->ulpq, event);
  728. }
  729. /* Select new active and retran paths. */
  730. /* Look for the two most recently used active transports.
  731. *
  732. * This code produces the wrong ordering whenever jiffies
  733. * rolls over, but we still get usable transports, so we don't
  734. * worry about it.
  735. */
  736. first = NULL; second = NULL;
  737. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  738. transports) {
  739. if ((t->state == SCTP_INACTIVE) ||
  740. (t->state == SCTP_UNCONFIRMED) ||
  741. (t->state == SCTP_PF))
  742. continue;
  743. if (!first || t->last_time_heard > first->last_time_heard) {
  744. second = first;
  745. first = t;
  746. }
  747. if (!second || t->last_time_heard > second->last_time_heard)
  748. second = t;
  749. }
  750. /* RFC 2960 6.4 Multi-Homed SCTP Endpoints
  751. *
  752. * By default, an endpoint should always transmit to the
  753. * primary path, unless the SCTP user explicitly specifies the
  754. * destination transport address (and possibly source
  755. * transport address) to use.
  756. *
  757. * [If the primary is active but not most recent, bump the most
  758. * recently used transport.]
  759. */
  760. if (((asoc->peer.primary_path->state == SCTP_ACTIVE) ||
  761. (asoc->peer.primary_path->state == SCTP_UNKNOWN)) &&
  762. first != asoc->peer.primary_path) {
  763. second = first;
  764. first = asoc->peer.primary_path;
  765. }
  766. /* If we failed to find a usable transport, just camp on the
  767. * primary, even if it is inactive.
  768. */
  769. if (!first) {
  770. first = asoc->peer.primary_path;
  771. second = asoc->peer.primary_path;
  772. }
  773. /* Set the active and retran transports. */
  774. asoc->peer.active_path = first;
  775. asoc->peer.retran_path = second;
  776. }
  777. /* Hold a reference to an association. */
  778. void sctp_association_hold(struct sctp_association *asoc)
  779. {
  780. atomic_inc(&asoc->base.refcnt);
  781. }
  782. /* Release a reference to an association and cleanup
  783. * if there are no more references.
  784. */
  785. void sctp_association_put(struct sctp_association *asoc)
  786. {
  787. if (atomic_dec_and_test(&asoc->base.refcnt))
  788. sctp_association_destroy(asoc);
  789. }
  790. /* Allocate the next TSN, Transmission Sequence Number, for the given
  791. * association.
  792. */
  793. __u32 sctp_association_get_next_tsn(struct sctp_association *asoc)
  794. {
  795. /* From Section 1.6 Serial Number Arithmetic:
  796. * Transmission Sequence Numbers wrap around when they reach
  797. * 2**32 - 1. That is, the next TSN a DATA chunk MUST use
  798. * after transmitting TSN = 2*32 - 1 is TSN = 0.
  799. */
  800. __u32 retval = asoc->next_tsn;
  801. asoc->next_tsn++;
  802. asoc->unack_data++;
  803. return retval;
  804. }
  805. /* Compare two addresses to see if they match. Wildcard addresses
  806. * only match themselves.
  807. */
  808. int sctp_cmp_addr_exact(const union sctp_addr *ss1,
  809. const union sctp_addr *ss2)
  810. {
  811. struct sctp_af *af;
  812. af = sctp_get_af_specific(ss1->sa.sa_family);
  813. if (unlikely(!af))
  814. return 0;
  815. return af->cmp_addr(ss1, ss2);
  816. }
  817. /* Return an ecne chunk to get prepended to a packet.
  818. * Note: We are sly and return a shared, prealloced chunk. FIXME:
  819. * No we don't, but we could/should.
  820. */
  821. struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc)
  822. {
  823. struct sctp_chunk *chunk;
  824. /* Send ECNE if needed.
  825. * Not being able to allocate a chunk here is not deadly.
  826. */
  827. if (asoc->need_ecne)
  828. chunk = sctp_make_ecne(asoc, asoc->last_ecne_tsn);
  829. else
  830. chunk = NULL;
  831. return chunk;
  832. }
  833. /*
  834. * Find which transport this TSN was sent on.
  835. */
  836. struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc,
  837. __u32 tsn)
  838. {
  839. struct sctp_transport *active;
  840. struct sctp_transport *match;
  841. struct sctp_transport *transport;
  842. struct sctp_chunk *chunk;
  843. __be32 key = htonl(tsn);
  844. match = NULL;
  845. /*
  846. * FIXME: In general, find a more efficient data structure for
  847. * searching.
  848. */
  849. /*
  850. * The general strategy is to search each transport's transmitted
  851. * list. Return which transport this TSN lives on.
  852. *
  853. * Let's be hopeful and check the active_path first.
  854. * Another optimization would be to know if there is only one
  855. * outbound path and not have to look for the TSN at all.
  856. *
  857. */
  858. active = asoc->peer.active_path;
  859. list_for_each_entry(chunk, &active->transmitted,
  860. transmitted_list) {
  861. if (key == chunk->subh.data_hdr->tsn) {
  862. match = active;
  863. goto out;
  864. }
  865. }
  866. /* If not found, go search all the other transports. */
  867. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  868. transports) {
  869. if (transport == active)
  870. continue;
  871. list_for_each_entry(chunk, &transport->transmitted,
  872. transmitted_list) {
  873. if (key == chunk->subh.data_hdr->tsn) {
  874. match = transport;
  875. goto out;
  876. }
  877. }
  878. }
  879. out:
  880. return match;
  881. }
  882. /* Is this the association we are looking for? */
  883. struct sctp_transport *sctp_assoc_is_match(struct sctp_association *asoc,
  884. struct net *net,
  885. const union sctp_addr *laddr,
  886. const union sctp_addr *paddr)
  887. {
  888. struct sctp_transport *transport;
  889. if ((htons(asoc->base.bind_addr.port) == laddr->v4.sin_port) &&
  890. (htons(asoc->peer.port) == paddr->v4.sin_port) &&
  891. net_eq(sock_net(asoc->base.sk), net)) {
  892. transport = sctp_assoc_lookup_paddr(asoc, paddr);
  893. if (!transport)
  894. goto out;
  895. if (sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
  896. sctp_sk(asoc->base.sk)))
  897. goto out;
  898. }
  899. transport = NULL;
  900. out:
  901. return transport;
  902. }
  903. /* Do delayed input processing. This is scheduled by sctp_rcv(). */
  904. static void sctp_assoc_bh_rcv(struct work_struct *work)
  905. {
  906. struct sctp_association *asoc =
  907. container_of(work, struct sctp_association,
  908. base.inqueue.immediate);
  909. struct net *net = sock_net(asoc->base.sk);
  910. struct sctp_endpoint *ep;
  911. struct sctp_chunk *chunk;
  912. struct sctp_inq *inqueue;
  913. int state;
  914. sctp_subtype_t subtype;
  915. int error = 0;
  916. /* The association should be held so we should be safe. */
  917. ep = asoc->ep;
  918. inqueue = &asoc->base.inqueue;
  919. sctp_association_hold(asoc);
  920. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  921. state = asoc->state;
  922. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  923. /* SCTP-AUTH, Section 6.3:
  924. * The receiver has a list of chunk types which it expects
  925. * to be received only after an AUTH-chunk. This list has
  926. * been sent to the peer during the association setup. It
  927. * MUST silently discard these chunks if they are not placed
  928. * after an AUTH chunk in the packet.
  929. */
  930. if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
  931. continue;
  932. /* Remember where the last DATA chunk came from so we
  933. * know where to send the SACK.
  934. */
  935. if (sctp_chunk_is_data(chunk))
  936. asoc->peer.last_data_from = chunk->transport;
  937. else {
  938. SCTP_INC_STATS(net, SCTP_MIB_INCTRLCHUNKS);
  939. asoc->stats.ictrlchunks++;
  940. if (chunk->chunk_hdr->type == SCTP_CID_SACK)
  941. asoc->stats.isacks++;
  942. }
  943. if (chunk->transport)
  944. chunk->transport->last_time_heard = jiffies;
  945. /* Run through the state machine. */
  946. error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype,
  947. state, ep, asoc, chunk, GFP_ATOMIC);
  948. /* Check to see if the association is freed in response to
  949. * the incoming chunk. If so, get out of the while loop.
  950. */
  951. if (asoc->base.dead)
  952. break;
  953. /* If there is an error on chunk, discard this packet. */
  954. if (error && chunk)
  955. chunk->pdiscard = 1;
  956. }
  957. sctp_association_put(asoc);
  958. }
  959. /* This routine moves an association from its old sk to a new sk. */
  960. void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)
  961. {
  962. struct sctp_sock *newsp = sctp_sk(newsk);
  963. struct sock *oldsk = assoc->base.sk;
  964. /* Delete the association from the old endpoint's list of
  965. * associations.
  966. */
  967. list_del_init(&assoc->asocs);
  968. /* Decrement the backlog value for a TCP-style socket. */
  969. if (sctp_style(oldsk, TCP))
  970. oldsk->sk_ack_backlog--;
  971. /* Release references to the old endpoint and the sock. */
  972. sctp_endpoint_put(assoc->ep);
  973. sock_put(assoc->base.sk);
  974. /* Get a reference to the new endpoint. */
  975. assoc->ep = newsp->ep;
  976. sctp_endpoint_hold(assoc->ep);
  977. /* Get a reference to the new sock. */
  978. assoc->base.sk = newsk;
  979. sock_hold(assoc->base.sk);
  980. /* Add the association to the new endpoint's list of associations. */
  981. sctp_endpoint_add_asoc(newsp->ep, assoc);
  982. }
  983. /* Update an association (possibly from unexpected COOKIE-ECHO processing). */
  984. void sctp_assoc_update(struct sctp_association *asoc,
  985. struct sctp_association *new)
  986. {
  987. struct sctp_transport *trans;
  988. struct list_head *pos, *temp;
  989. /* Copy in new parameters of peer. */
  990. asoc->c = new->c;
  991. asoc->peer.rwnd = new->peer.rwnd;
  992. asoc->peer.sack_needed = new->peer.sack_needed;
  993. asoc->peer.i = new->peer.i;
  994. sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  995. asoc->peer.i.initial_tsn, GFP_ATOMIC);
  996. /* Remove any peer addresses not present in the new association. */
  997. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  998. trans = list_entry(pos, struct sctp_transport, transports);
  999. if (!sctp_assoc_lookup_paddr(new, &trans->ipaddr)) {
  1000. sctp_assoc_rm_peer(asoc, trans);
  1001. continue;
  1002. }
  1003. if (asoc->state >= SCTP_STATE_ESTABLISHED)
  1004. sctp_transport_reset(trans);
  1005. }
  1006. /* If the case is A (association restart), use
  1007. * initial_tsn as next_tsn. If the case is B, use
  1008. * current next_tsn in case data sent to peer
  1009. * has been discarded and needs retransmission.
  1010. */
  1011. if (asoc->state >= SCTP_STATE_ESTABLISHED) {
  1012. asoc->next_tsn = new->next_tsn;
  1013. asoc->ctsn_ack_point = new->ctsn_ack_point;
  1014. asoc->adv_peer_ack_point = new->adv_peer_ack_point;
  1015. /* Reinitialize SSN for both local streams
  1016. * and peer's streams.
  1017. */
  1018. sctp_ssnmap_clear(asoc->ssnmap);
  1019. /* Flush the ULP reassembly and ordered queue.
  1020. * Any data there will now be stale and will
  1021. * cause problems.
  1022. */
  1023. sctp_ulpq_flush(&asoc->ulpq);
  1024. /* reset the overall association error count so
  1025. * that the restarted association doesn't get torn
  1026. * down on the next retransmission timer.
  1027. */
  1028. asoc->overall_error_count = 0;
  1029. } else {
  1030. /* Add any peer addresses from the new association. */
  1031. list_for_each_entry(trans, &new->peer.transport_addr_list,
  1032. transports) {
  1033. if (!sctp_assoc_lookup_paddr(asoc, &trans->ipaddr))
  1034. sctp_assoc_add_peer(asoc, &trans->ipaddr,
  1035. GFP_ATOMIC, trans->state);
  1036. }
  1037. asoc->ctsn_ack_point = asoc->next_tsn - 1;
  1038. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  1039. if (!asoc->ssnmap) {
  1040. /* Move the ssnmap. */
  1041. asoc->ssnmap = new->ssnmap;
  1042. new->ssnmap = NULL;
  1043. }
  1044. if (!asoc->assoc_id) {
  1045. /* get a new association id since we don't have one
  1046. * yet.
  1047. */
  1048. sctp_assoc_set_id(asoc, GFP_ATOMIC);
  1049. }
  1050. }
  1051. /* SCTP-AUTH: Save the peer parameters from the new assocaitions
  1052. * and also move the association shared keys over
  1053. */
  1054. kfree(asoc->peer.peer_random);
  1055. asoc->peer.peer_random = new->peer.peer_random;
  1056. new->peer.peer_random = NULL;
  1057. kfree(asoc->peer.peer_chunks);
  1058. asoc->peer.peer_chunks = new->peer.peer_chunks;
  1059. new->peer.peer_chunks = NULL;
  1060. kfree(asoc->peer.peer_hmacs);
  1061. asoc->peer.peer_hmacs = new->peer.peer_hmacs;
  1062. new->peer.peer_hmacs = NULL;
  1063. sctp_auth_key_put(asoc->asoc_shared_key);
  1064. sctp_auth_asoc_init_active_key(asoc, GFP_ATOMIC);
  1065. }
  1066. /* Update the retran path for sending a retransmitted packet.
  1067. * Round-robin through the active transports, else round-robin
  1068. * through the inactive transports as this is the next best thing
  1069. * we can try.
  1070. */
  1071. void sctp_assoc_update_retran_path(struct sctp_association *asoc)
  1072. {
  1073. struct sctp_transport *t, *next;
  1074. struct list_head *head = &asoc->peer.transport_addr_list;
  1075. struct list_head *pos;
  1076. if (asoc->peer.transport_count == 1)
  1077. return;
  1078. /* Find the next transport in a round-robin fashion. */
  1079. t = asoc->peer.retran_path;
  1080. pos = &t->transports;
  1081. next = NULL;
  1082. while (1) {
  1083. /* Skip the head. */
  1084. if (pos->next == head)
  1085. pos = head->next;
  1086. else
  1087. pos = pos->next;
  1088. t = list_entry(pos, struct sctp_transport, transports);
  1089. /* We have exhausted the list, but didn't find any
  1090. * other active transports. If so, use the next
  1091. * transport.
  1092. */
  1093. if (t == asoc->peer.retran_path) {
  1094. t = next;
  1095. break;
  1096. }
  1097. /* Try to find an active transport. */
  1098. if ((t->state == SCTP_ACTIVE) ||
  1099. (t->state == SCTP_UNKNOWN)) {
  1100. break;
  1101. } else {
  1102. /* Keep track of the next transport in case
  1103. * we don't find any active transport.
  1104. */
  1105. if (t->state != SCTP_UNCONFIRMED && !next)
  1106. next = t;
  1107. }
  1108. }
  1109. if (t)
  1110. asoc->peer.retran_path = t;
  1111. else
  1112. t = asoc->peer.retran_path;
  1113. pr_debug("%s: association:%p addr:%pISpc\n", __func__, asoc,
  1114. &t->ipaddr.sa);
  1115. }
  1116. /* Choose the transport for sending retransmit packet. */
  1117. struct sctp_transport *sctp_assoc_choose_alter_transport(
  1118. struct sctp_association *asoc, struct sctp_transport *last_sent_to)
  1119. {
  1120. /* If this is the first time packet is sent, use the active path,
  1121. * else use the retran path. If the last packet was sent over the
  1122. * retran path, update the retran path and use it.
  1123. */
  1124. if (!last_sent_to)
  1125. return asoc->peer.active_path;
  1126. else {
  1127. if (last_sent_to == asoc->peer.retran_path)
  1128. sctp_assoc_update_retran_path(asoc);
  1129. return asoc->peer.retran_path;
  1130. }
  1131. }
  1132. /* Update the association's pmtu and frag_point by going through all the
  1133. * transports. This routine is called when a transport's PMTU has changed.
  1134. */
  1135. void sctp_assoc_sync_pmtu(struct sock *sk, struct sctp_association *asoc)
  1136. {
  1137. struct sctp_transport *t;
  1138. __u32 pmtu = 0;
  1139. if (!asoc)
  1140. return;
  1141. /* Get the lowest pmtu of all the transports. */
  1142. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  1143. transports) {
  1144. if (t->pmtu_pending && t->dst) {
  1145. sctp_transport_update_pmtu(sk, t, dst_mtu(t->dst));
  1146. t->pmtu_pending = 0;
  1147. }
  1148. if (!pmtu || (t->pathmtu < pmtu))
  1149. pmtu = t->pathmtu;
  1150. }
  1151. if (pmtu) {
  1152. asoc->pathmtu = pmtu;
  1153. asoc->frag_point = sctp_frag_point(asoc, pmtu);
  1154. }
  1155. pr_debug("%s: asoc:%p, pmtu:%d, frag_point:%d\n", __func__, asoc,
  1156. asoc->pathmtu, asoc->frag_point);
  1157. }
  1158. /* Should we send a SACK to update our peer? */
  1159. static inline int sctp_peer_needs_update(struct sctp_association *asoc)
  1160. {
  1161. struct net *net = sock_net(asoc->base.sk);
  1162. switch (asoc->state) {
  1163. case SCTP_STATE_ESTABLISHED:
  1164. case SCTP_STATE_SHUTDOWN_PENDING:
  1165. case SCTP_STATE_SHUTDOWN_RECEIVED:
  1166. case SCTP_STATE_SHUTDOWN_SENT:
  1167. if ((asoc->rwnd > asoc->a_rwnd) &&
  1168. ((asoc->rwnd - asoc->a_rwnd) >= max_t(__u32,
  1169. (asoc->base.sk->sk_rcvbuf >> net->sctp.rwnd_upd_shift),
  1170. asoc->pathmtu)))
  1171. return 1;
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. return 0;
  1177. }
  1178. /* Increase asoc's rwnd by len and send any window update SACK if needed. */
  1179. void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned int len)
  1180. {
  1181. struct sctp_chunk *sack;
  1182. struct timer_list *timer;
  1183. if (asoc->rwnd_over) {
  1184. if (asoc->rwnd_over >= len) {
  1185. asoc->rwnd_over -= len;
  1186. } else {
  1187. asoc->rwnd += (len - asoc->rwnd_over);
  1188. asoc->rwnd_over = 0;
  1189. }
  1190. } else {
  1191. asoc->rwnd += len;
  1192. }
  1193. /* If we had window pressure, start recovering it
  1194. * once our rwnd had reached the accumulated pressure
  1195. * threshold. The idea is to recover slowly, but up
  1196. * to the initial advertised window.
  1197. */
  1198. if (asoc->rwnd_press && asoc->rwnd >= asoc->rwnd_press) {
  1199. int change = min(asoc->pathmtu, asoc->rwnd_press);
  1200. asoc->rwnd += change;
  1201. asoc->rwnd_press -= change;
  1202. }
  1203. pr_debug("%s: asoc:%p rwnd increased by %d to (%u, %u) - %u\n",
  1204. __func__, asoc, len, asoc->rwnd, asoc->rwnd_over,
  1205. asoc->a_rwnd);
  1206. /* Send a window update SACK if the rwnd has increased by at least the
  1207. * minimum of the association's PMTU and half of the receive buffer.
  1208. * The algorithm used is similar to the one described in
  1209. * Section 4.2.3.3 of RFC 1122.
  1210. */
  1211. if (sctp_peer_needs_update(asoc)) {
  1212. asoc->a_rwnd = asoc->rwnd;
  1213. pr_debug("%s: sending window update SACK- asoc:%p rwnd:%u "
  1214. "a_rwnd:%u\n", __func__, asoc, asoc->rwnd,
  1215. asoc->a_rwnd);
  1216. sack = sctp_make_sack(asoc);
  1217. if (!sack)
  1218. return;
  1219. asoc->peer.sack_needed = 0;
  1220. sctp_outq_tail(&asoc->outqueue, sack);
  1221. /* Stop the SACK timer. */
  1222. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
  1223. if (del_timer(timer))
  1224. sctp_association_put(asoc);
  1225. }
  1226. }
  1227. /* Decrease asoc's rwnd by len. */
  1228. void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned int len)
  1229. {
  1230. int rx_count;
  1231. int over = 0;
  1232. if (unlikely(!asoc->rwnd || asoc->rwnd_over))
  1233. pr_debug("%s: association:%p has asoc->rwnd:%u, "
  1234. "asoc->rwnd_over:%u!\n", __func__, asoc,
  1235. asoc->rwnd, asoc->rwnd_over);
  1236. if (asoc->ep->rcvbuf_policy)
  1237. rx_count = atomic_read(&asoc->rmem_alloc);
  1238. else
  1239. rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
  1240. /* If we've reached or overflowed our receive buffer, announce
  1241. * a 0 rwnd if rwnd would still be positive. Store the
  1242. * the pottential pressure overflow so that the window can be restored
  1243. * back to original value.
  1244. */
  1245. if (rx_count >= asoc->base.sk->sk_rcvbuf)
  1246. over = 1;
  1247. if (asoc->rwnd >= len) {
  1248. asoc->rwnd -= len;
  1249. if (over) {
  1250. asoc->rwnd_press += asoc->rwnd;
  1251. asoc->rwnd = 0;
  1252. }
  1253. } else {
  1254. asoc->rwnd_over = len - asoc->rwnd;
  1255. asoc->rwnd = 0;
  1256. }
  1257. pr_debug("%s: asoc:%p rwnd decreased by %d to (%u, %u, %u)\n",
  1258. __func__, asoc, len, asoc->rwnd, asoc->rwnd_over,
  1259. asoc->rwnd_press);
  1260. }
  1261. /* Build the bind address list for the association based on info from the
  1262. * local endpoint and the remote peer.
  1263. */
  1264. int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc,
  1265. sctp_scope_t scope, gfp_t gfp)
  1266. {
  1267. int flags;
  1268. /* Use scoping rules to determine the subset of addresses from
  1269. * the endpoint.
  1270. */
  1271. flags = (PF_INET6 == asoc->base.sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0;
  1272. if (asoc->peer.ipv4_address)
  1273. flags |= SCTP_ADDR4_PEERSUPP;
  1274. if (asoc->peer.ipv6_address)
  1275. flags |= SCTP_ADDR6_PEERSUPP;
  1276. return sctp_bind_addr_copy(sock_net(asoc->base.sk),
  1277. &asoc->base.bind_addr,
  1278. &asoc->ep->base.bind_addr,
  1279. scope, gfp, flags);
  1280. }
  1281. /* Build the association's bind address list from the cookie. */
  1282. int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc,
  1283. struct sctp_cookie *cookie,
  1284. gfp_t gfp)
  1285. {
  1286. int var_size2 = ntohs(cookie->peer_init->chunk_hdr.length);
  1287. int var_size3 = cookie->raw_addr_list_len;
  1288. __u8 *raw = (__u8 *)cookie->peer_init + var_size2;
  1289. return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3,
  1290. asoc->ep->base.bind_addr.port, gfp);
  1291. }
  1292. /* Lookup laddr in the bind address list of an association. */
  1293. int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
  1294. const union sctp_addr *laddr)
  1295. {
  1296. int found = 0;
  1297. if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) &&
  1298. sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
  1299. sctp_sk(asoc->base.sk)))
  1300. found = 1;
  1301. return found;
  1302. }
  1303. /* Set an association id for a given association */
  1304. int sctp_assoc_set_id(struct sctp_association *asoc, gfp_t gfp)
  1305. {
  1306. bool preload = gfp & __GFP_WAIT;
  1307. int ret;
  1308. /* If the id is already assigned, keep it. */
  1309. if (asoc->assoc_id)
  1310. return 0;
  1311. if (preload)
  1312. idr_preload(gfp);
  1313. spin_lock_bh(&sctp_assocs_id_lock);
  1314. /* 0 is not a valid assoc_id, must be >= 1 */
  1315. ret = idr_alloc_cyclic(&sctp_assocs_id, asoc, 1, 0, GFP_NOWAIT);
  1316. spin_unlock_bh(&sctp_assocs_id_lock);
  1317. if (preload)
  1318. idr_preload_end();
  1319. if (ret < 0)
  1320. return ret;
  1321. asoc->assoc_id = (sctp_assoc_t)ret;
  1322. return 0;
  1323. }
  1324. /* Free the ASCONF queue */
  1325. static void sctp_assoc_free_asconf_queue(struct sctp_association *asoc)
  1326. {
  1327. struct sctp_chunk *asconf;
  1328. struct sctp_chunk *tmp;
  1329. list_for_each_entry_safe(asconf, tmp, &asoc->addip_chunk_list, list) {
  1330. list_del_init(&asconf->list);
  1331. sctp_chunk_free(asconf);
  1332. }
  1333. }
  1334. /* Free asconf_ack cache */
  1335. static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc)
  1336. {
  1337. struct sctp_chunk *ack;
  1338. struct sctp_chunk *tmp;
  1339. list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list,
  1340. transmitted_list) {
  1341. list_del_init(&ack->transmitted_list);
  1342. sctp_chunk_free(ack);
  1343. }
  1344. }
  1345. /* Clean up the ASCONF_ACK queue */
  1346. void sctp_assoc_clean_asconf_ack_cache(const struct sctp_association *asoc)
  1347. {
  1348. struct sctp_chunk *ack;
  1349. struct sctp_chunk *tmp;
  1350. /* We can remove all the entries from the queue up to
  1351. * the "Peer-Sequence-Number".
  1352. */
  1353. list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list,
  1354. transmitted_list) {
  1355. if (ack->subh.addip_hdr->serial ==
  1356. htonl(asoc->peer.addip_serial))
  1357. break;
  1358. list_del_init(&ack->transmitted_list);
  1359. sctp_chunk_free(ack);
  1360. }
  1361. }
  1362. /* Find the ASCONF_ACK whose serial number matches ASCONF */
  1363. struct sctp_chunk *sctp_assoc_lookup_asconf_ack(
  1364. const struct sctp_association *asoc,
  1365. __be32 serial)
  1366. {
  1367. struct sctp_chunk *ack;
  1368. /* Walk through the list of cached ASCONF-ACKs and find the
  1369. * ack chunk whose serial number matches that of the request.
  1370. */
  1371. list_for_each_entry(ack, &asoc->asconf_ack_list, transmitted_list) {
  1372. if (ack->subh.addip_hdr->serial == serial) {
  1373. sctp_chunk_hold(ack);
  1374. return ack;
  1375. }
  1376. }
  1377. return NULL;
  1378. }
  1379. void sctp_asconf_queue_teardown(struct sctp_association *asoc)
  1380. {
  1381. /* Free any cached ASCONF_ACK chunk. */
  1382. sctp_assoc_free_asconf_acks(asoc);
  1383. /* Free the ASCONF queue. */
  1384. sctp_assoc_free_asconf_queue(asoc);
  1385. /* Free any cached ASCONF chunk. */
  1386. if (asoc->addip_last_asconf)
  1387. sctp_chunk_free(asoc->addip_last_asconf);
  1388. }