associola.c 47 KB

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