associola.c 39 KB

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