associola.c 49 KB

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