associola.c 49 KB

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