associola.c 33 KB

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