associola.c 37 KB

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