associola.c 49 KB

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