associola.c 36 KB

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