associola.c 46 KB

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