associola.c 50 KB

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