output.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. *
  6. * This file is part of the SCTP kernel implementation
  7. *
  8. * These functions handle output processing.
  9. *
  10. * This SCTP implementation is free software;
  11. * you can redistribute it and/or modify it under the terms of
  12. * the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This SCTP implementation is distributed in the hope that it
  17. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  18. * ************************
  19. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. * See the GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with GNU CC; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 59 Temple Place - Suite 330,
  25. * Boston, MA 02111-1307, USA.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  30. *
  31. * Or submit a bug report through the following website:
  32. * http://www.sf.net/projects/lksctp
  33. *
  34. * Written or modified by:
  35. * La Monte H.P. Yarroll <piggy@acm.org>
  36. * Karl Knutson <karl@athena.chicago.il.us>
  37. * Jon Grimm <jgrimm@austin.ibm.com>
  38. * Sridhar Samudrala <sri@us.ibm.com>
  39. *
  40. * Any bugs reported given to us we will try to fix... any fixes shared will
  41. * be incorporated into the next SCTP release.
  42. */
  43. #include <linux/types.h>
  44. #include <linux/kernel.h>
  45. #include <linux/wait.h>
  46. #include <linux/time.h>
  47. #include <linux/ip.h>
  48. #include <linux/ipv6.h>
  49. #include <linux/init.h>
  50. #include <net/inet_ecn.h>
  51. #include <net/icmp.h>
  52. #ifndef TEST_FRAME
  53. #include <net/tcp.h>
  54. #endif /* TEST_FRAME (not defined) */
  55. #include <linux/socket.h> /* for sa_family_t */
  56. #include <net/sock.h>
  57. #include <net/sctp/sctp.h>
  58. #include <net/sctp/sm.h>
  59. #include <net/sctp/checksum.h>
  60. /* Forward declarations for private helpers. */
  61. static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
  62. struct sctp_chunk *chunk);
  63. /* Config a packet.
  64. * This appears to be a followup set of initializations.
  65. */
  66. struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
  67. __u32 vtag, int ecn_capable)
  68. {
  69. struct sctp_chunk *chunk = NULL;
  70. SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __func__,
  71. packet, vtag);
  72. packet->vtag = vtag;
  73. packet->has_cookie_echo = 0;
  74. packet->has_sack = 0;
  75. packet->has_auth = 0;
  76. packet->has_data = 0;
  77. packet->ipfragok = 0;
  78. packet->auth = NULL;
  79. if (ecn_capable && sctp_packet_empty(packet)) {
  80. chunk = sctp_get_ecne_prepend(packet->transport->asoc);
  81. /* If there a is a prepend chunk stick it on the list before
  82. * any other chunks get appended.
  83. */
  84. if (chunk)
  85. sctp_packet_append_chunk(packet, chunk);
  86. }
  87. return packet;
  88. }
  89. /* Initialize the packet structure. */
  90. struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
  91. struct sctp_transport *transport,
  92. __u16 sport, __u16 dport)
  93. {
  94. struct sctp_association *asoc = transport->asoc;
  95. size_t overhead;
  96. SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __func__,
  97. packet, transport);
  98. packet->transport = transport;
  99. packet->source_port = sport;
  100. packet->destination_port = dport;
  101. INIT_LIST_HEAD(&packet->chunk_list);
  102. if (asoc) {
  103. struct sctp_sock *sp = sctp_sk(asoc->base.sk);
  104. overhead = sp->pf->af->net_header_len;
  105. } else {
  106. overhead = sizeof(struct ipv6hdr);
  107. }
  108. overhead += sizeof(struct sctphdr);
  109. packet->overhead = overhead;
  110. packet->size = overhead;
  111. packet->vtag = 0;
  112. packet->has_cookie_echo = 0;
  113. packet->has_sack = 0;
  114. packet->has_auth = 0;
  115. packet->has_data = 0;
  116. packet->ipfragok = 0;
  117. packet->malloced = 0;
  118. packet->auth = NULL;
  119. return packet;
  120. }
  121. /* Free a packet. */
  122. void sctp_packet_free(struct sctp_packet *packet)
  123. {
  124. struct sctp_chunk *chunk, *tmp;
  125. SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
  126. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  127. list_del_init(&chunk->list);
  128. sctp_chunk_free(chunk);
  129. }
  130. if (packet->malloced)
  131. kfree(packet);
  132. }
  133. /* This routine tries to append the chunk to the offered packet. If adding
  134. * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
  135. * is not present in the packet, it transmits the input packet.
  136. * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
  137. * as it can fit in the packet, but any more data that does not fit in this
  138. * packet can be sent only after receiving the COOKIE_ACK.
  139. */
  140. sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
  141. struct sctp_chunk *chunk)
  142. {
  143. sctp_xmit_t retval;
  144. int error = 0;
  145. SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__,
  146. packet, chunk);
  147. switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
  148. case SCTP_XMIT_PMTU_FULL:
  149. if (!packet->has_cookie_echo) {
  150. error = sctp_packet_transmit(packet);
  151. if (error < 0)
  152. chunk->skb->sk->sk_err = -error;
  153. /* If we have an empty packet, then we can NOT ever
  154. * return PMTU_FULL.
  155. */
  156. retval = sctp_packet_append_chunk(packet, chunk);
  157. }
  158. break;
  159. case SCTP_XMIT_RWND_FULL:
  160. case SCTP_XMIT_OK:
  161. case SCTP_XMIT_NAGLE_DELAY:
  162. break;
  163. }
  164. return retval;
  165. }
  166. /* Try to bundle an auth chunk into the packet. */
  167. static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
  168. struct sctp_chunk *chunk)
  169. {
  170. struct sctp_association *asoc = pkt->transport->asoc;
  171. struct sctp_chunk *auth;
  172. sctp_xmit_t retval = SCTP_XMIT_OK;
  173. /* if we don't have an association, we can't do authentication */
  174. if (!asoc)
  175. return retval;
  176. /* See if this is an auth chunk we are bundling or if
  177. * auth is already bundled.
  178. */
  179. if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
  180. return retval;
  181. /* if the peer did not request this chunk to be authenticated,
  182. * don't do it
  183. */
  184. if (!chunk->auth)
  185. return retval;
  186. auth = sctp_make_auth(asoc);
  187. if (!auth)
  188. return retval;
  189. retval = sctp_packet_append_chunk(pkt, auth);
  190. return retval;
  191. }
  192. /* Try to bundle a SACK with the packet. */
  193. static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
  194. struct sctp_chunk *chunk)
  195. {
  196. sctp_xmit_t retval = SCTP_XMIT_OK;
  197. /* If sending DATA and haven't aleady bundled a SACK, try to
  198. * bundle one in to the packet.
  199. */
  200. if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
  201. !pkt->has_cookie_echo) {
  202. struct sctp_association *asoc;
  203. asoc = pkt->transport->asoc;
  204. if (asoc->a_rwnd > asoc->rwnd) {
  205. struct sctp_chunk *sack;
  206. asoc->a_rwnd = asoc->rwnd;
  207. sack = sctp_make_sack(asoc);
  208. if (sack) {
  209. struct timer_list *timer;
  210. retval = sctp_packet_append_chunk(pkt, sack);
  211. asoc->peer.sack_needed = 0;
  212. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
  213. if (timer_pending(timer) && del_timer(timer))
  214. sctp_association_put(asoc);
  215. }
  216. }
  217. }
  218. return retval;
  219. }
  220. /* Append a chunk to the offered packet reporting back any inability to do
  221. * so.
  222. */
  223. sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
  224. struct sctp_chunk *chunk)
  225. {
  226. sctp_xmit_t retval = SCTP_XMIT_OK;
  227. __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
  228. size_t psize;
  229. size_t pmtu;
  230. int too_big;
  231. SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__, packet,
  232. chunk);
  233. /* Try to bundle AUTH chunk */
  234. retval = sctp_packet_bundle_auth(packet, chunk);
  235. if (retval != SCTP_XMIT_OK)
  236. goto finish;
  237. /* Try to bundle SACK chunk */
  238. retval = sctp_packet_bundle_sack(packet, chunk);
  239. if (retval != SCTP_XMIT_OK)
  240. goto finish;
  241. psize = packet->size;
  242. pmtu = ((packet->transport->asoc) ?
  243. (packet->transport->asoc->pathmtu) :
  244. (packet->transport->pathmtu));
  245. too_big = (psize + chunk_len > pmtu);
  246. /* Decide if we need to fragment or resubmit later. */
  247. if (too_big) {
  248. /* It's OK to fragmet at IP level if any one of the following
  249. * is true:
  250. * 1. The packet is empty (meaning this chunk is greater
  251. * the MTU)
  252. * 2. The chunk we are adding is a control chunk
  253. * 3. The packet doesn't have any data in it yet and data
  254. * requires authentication.
  255. */
  256. if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||
  257. (!packet->has_data && chunk->auth)) {
  258. /* We no longer do re-fragmentation.
  259. * Just fragment at the IP layer, if we
  260. * actually hit this condition
  261. */
  262. packet->ipfragok = 1;
  263. goto append;
  264. } else {
  265. retval = SCTP_XMIT_PMTU_FULL;
  266. goto finish;
  267. }
  268. }
  269. append:
  270. /* We believe that this chunk is OK to add to the packet (as
  271. * long as we have the cwnd for it).
  272. */
  273. /* DATA is a special case since we must examine both rwnd and cwnd
  274. * before we send DATA.
  275. */
  276. switch (chunk->chunk_hdr->type) {
  277. case SCTP_CID_DATA:
  278. retval = sctp_packet_append_data(packet, chunk);
  279. /* Disallow SACK bundling after DATA. */
  280. packet->has_sack = 1;
  281. /* Disallow AUTH bundling after DATA */
  282. packet->has_auth = 1;
  283. /* Let it be knows that packet has DATA in it */
  284. packet->has_data = 1;
  285. if (SCTP_XMIT_OK != retval)
  286. goto finish;
  287. break;
  288. case SCTP_CID_COOKIE_ECHO:
  289. packet->has_cookie_echo = 1;
  290. break;
  291. case SCTP_CID_SACK:
  292. packet->has_sack = 1;
  293. break;
  294. case SCTP_CID_AUTH:
  295. packet->has_auth = 1;
  296. packet->auth = chunk;
  297. break;
  298. }
  299. /* It is OK to send this chunk. */
  300. list_add_tail(&chunk->list, &packet->chunk_list);
  301. packet->size += chunk_len;
  302. chunk->transport = packet->transport;
  303. finish:
  304. return retval;
  305. }
  306. /* All packets are sent to the network through this function from
  307. * sctp_outq_tail().
  308. *
  309. * The return value is a normal kernel error return value.
  310. */
  311. int sctp_packet_transmit(struct sctp_packet *packet)
  312. {
  313. struct sctp_transport *tp = packet->transport;
  314. struct sctp_association *asoc = tp->asoc;
  315. struct sctphdr *sh;
  316. __u32 crc32 = 0;
  317. struct sk_buff *nskb;
  318. struct sctp_chunk *chunk, *tmp;
  319. struct sock *sk;
  320. int err = 0;
  321. int padding; /* How much padding do we need? */
  322. __u8 has_data = 0;
  323. struct dst_entry *dst = tp->dst;
  324. unsigned char *auth = NULL; /* pointer to auth in skb data */
  325. __u32 cksum_buf_len = sizeof(struct sctphdr);
  326. SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
  327. /* Do NOT generate a chunkless packet. */
  328. if (list_empty(&packet->chunk_list))
  329. return err;
  330. /* Set up convenience variables... */
  331. chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
  332. sk = chunk->skb->sk;
  333. /* Allocate the new skb. */
  334. nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
  335. if (!nskb)
  336. goto nomem;
  337. /* Make sure the outbound skb has enough header room reserved. */
  338. skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
  339. /* Set the owning socket so that we know where to get the
  340. * destination IP address.
  341. */
  342. skb_set_owner_w(nskb, sk);
  343. /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
  344. if (!dst || (dst->obsolete > 1)) {
  345. dst_release(dst);
  346. sctp_transport_route(tp, NULL, sctp_sk(sk));
  347. if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
  348. sctp_assoc_sync_pmtu(asoc);
  349. }
  350. }
  351. nskb->dst = dst_clone(tp->dst);
  352. if (!nskb->dst)
  353. goto no_route;
  354. dst = nskb->dst;
  355. /* Build the SCTP header. */
  356. sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
  357. sh->source = htons(packet->source_port);
  358. sh->dest = htons(packet->destination_port);
  359. /* From 6.8 Adler-32 Checksum Calculation:
  360. * After the packet is constructed (containing the SCTP common
  361. * header and one or more control or DATA chunks), the
  362. * transmitter shall:
  363. *
  364. * 1) Fill in the proper Verification Tag in the SCTP common
  365. * header and initialize the checksum field to 0's.
  366. */
  367. sh->vtag = htonl(packet->vtag);
  368. sh->checksum = 0;
  369. /**
  370. * 6.10 Bundling
  371. *
  372. * An endpoint bundles chunks by simply including multiple
  373. * chunks in one outbound SCTP packet. ...
  374. */
  375. /**
  376. * 3.2 Chunk Field Descriptions
  377. *
  378. * The total length of a chunk (including Type, Length and
  379. * Value fields) MUST be a multiple of 4 bytes. If the length
  380. * of the chunk is not a multiple of 4 bytes, the sender MUST
  381. * pad the chunk with all zero bytes and this padding is not
  382. * included in the chunk length field. The sender should
  383. * never pad with more than 3 bytes.
  384. *
  385. * [This whole comment explains WORD_ROUND() below.]
  386. */
  387. SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
  388. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  389. list_del_init(&chunk->list);
  390. if (sctp_chunk_is_data(chunk)) {
  391. if (!chunk->has_tsn) {
  392. sctp_chunk_assign_ssn(chunk);
  393. sctp_chunk_assign_tsn(chunk);
  394. /* 6.3.1 C4) When data is in flight and when allowed
  395. * by rule C5, a new RTT measurement MUST be made each
  396. * round trip. Furthermore, new RTT measurements
  397. * SHOULD be made no more than once per round-trip
  398. * for a given destination transport address.
  399. */
  400. if (!tp->rto_pending) {
  401. chunk->rtt_in_progress = 1;
  402. tp->rto_pending = 1;
  403. }
  404. } else
  405. chunk->resent = 1;
  406. chunk->sent_at = jiffies;
  407. has_data = 1;
  408. }
  409. padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
  410. if (padding)
  411. memset(skb_put(chunk->skb, padding), 0, padding);
  412. /* if this is the auth chunk that we are adding,
  413. * store pointer where it will be added and put
  414. * the auth into the packet.
  415. */
  416. if (chunk == packet->auth)
  417. auth = skb_tail_pointer(nskb);
  418. cksum_buf_len += chunk->skb->len;
  419. memcpy(skb_put(nskb, chunk->skb->len),
  420. chunk->skb->data, chunk->skb->len);
  421. SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
  422. "*** Chunk", chunk,
  423. sctp_cname(SCTP_ST_CHUNK(
  424. chunk->chunk_hdr->type)),
  425. chunk->has_tsn ? "TSN" : "No TSN",
  426. chunk->has_tsn ?
  427. ntohl(chunk->subh.data_hdr->tsn) : 0,
  428. "length", ntohs(chunk->chunk_hdr->length),
  429. "chunk->skb->len", chunk->skb->len,
  430. "rtt_in_progress", chunk->rtt_in_progress);
  431. /*
  432. * If this is a control chunk, this is our last
  433. * reference. Free data chunks after they've been
  434. * acknowledged or have failed.
  435. */
  436. if (!sctp_chunk_is_data(chunk))
  437. sctp_chunk_free(chunk);
  438. }
  439. /* SCTP-AUTH, Section 6.2
  440. * The sender MUST calculate the MAC as described in RFC2104 [2]
  441. * using the hash function H as described by the MAC Identifier and
  442. * the shared association key K based on the endpoint pair shared key
  443. * described by the shared key identifier. The 'data' used for the
  444. * computation of the AUTH-chunk is given by the AUTH chunk with its
  445. * HMAC field set to zero (as shown in Figure 6) followed by all
  446. * chunks that are placed after the AUTH chunk in the SCTP packet.
  447. */
  448. if (auth)
  449. sctp_auth_calculate_hmac(asoc, nskb,
  450. (struct sctp_auth_chunk *)auth,
  451. GFP_ATOMIC);
  452. /* 2) Calculate the Adler-32 checksum of the whole packet,
  453. * including the SCTP common header and all the
  454. * chunks.
  455. *
  456. * Note: Adler-32 is no longer applicable, as has been replaced
  457. * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
  458. */
  459. if (!(dst->dev->features & NETIF_F_NO_CSUM)) {
  460. crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);
  461. crc32 = sctp_end_cksum(crc32);
  462. }
  463. /* 3) Put the resultant value into the checksum field in the
  464. * common header, and leave the rest of the bits unchanged.
  465. */
  466. sh->checksum = htonl(crc32);
  467. /* IP layer ECN support
  468. * From RFC 2481
  469. * "The ECN-Capable Transport (ECT) bit would be set by the
  470. * data sender to indicate that the end-points of the
  471. * transport protocol are ECN-capable."
  472. *
  473. * Now setting the ECT bit all the time, as it should not cause
  474. * any problems protocol-wise even if our peer ignores it.
  475. *
  476. * Note: The works for IPv6 layer checks this bit too later
  477. * in transmission. See IP6_ECN_flow_xmit().
  478. */
  479. (*tp->af_specific->ecn_capable)(nskb->sk);
  480. /* Set up the IP options. */
  481. /* BUG: not implemented
  482. * For v4 this all lives somewhere in sk->sk_opt...
  483. */
  484. /* Dump that on IP! */
  485. if (asoc && asoc->peer.last_sent_to != tp) {
  486. /* Considering the multiple CPU scenario, this is a
  487. * "correcter" place for last_sent_to. --xguo
  488. */
  489. asoc->peer.last_sent_to = tp;
  490. }
  491. if (has_data) {
  492. struct timer_list *timer;
  493. unsigned long timeout;
  494. tp->last_time_used = jiffies;
  495. /* Restart the AUTOCLOSE timer when sending data. */
  496. if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
  497. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  498. timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  499. if (!mod_timer(timer, jiffies + timeout))
  500. sctp_association_hold(asoc);
  501. }
  502. }
  503. SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
  504. nskb->len);
  505. if (tp->param_flags & SPP_PMTUD_ENABLE)
  506. (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
  507. else
  508. (*tp->af_specific->sctp_xmit)(nskb, tp, 1);
  509. out:
  510. packet->size = packet->overhead;
  511. return err;
  512. no_route:
  513. kfree_skb(nskb);
  514. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  515. /* FIXME: Returning the 'err' will effect all the associations
  516. * associated with a socket, although only one of the paths of the
  517. * association is unreachable.
  518. * The real failure of a transport or association can be passed on
  519. * to the user via notifications. So setting this error may not be
  520. * required.
  521. */
  522. /* err = -EHOSTUNREACH; */
  523. err:
  524. /* Control chunks are unreliable so just drop them. DATA chunks
  525. * will get resent or dropped later.
  526. */
  527. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  528. list_del_init(&chunk->list);
  529. if (!sctp_chunk_is_data(chunk))
  530. sctp_chunk_free(chunk);
  531. }
  532. goto out;
  533. nomem:
  534. err = -ENOMEM;
  535. goto err;
  536. }
  537. /********************************************************************
  538. * 2nd Level Abstractions
  539. ********************************************************************/
  540. /* This private function handles the specifics of appending DATA chunks. */
  541. static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
  542. struct sctp_chunk *chunk)
  543. {
  544. sctp_xmit_t retval = SCTP_XMIT_OK;
  545. size_t datasize, rwnd, inflight;
  546. struct sctp_transport *transport = packet->transport;
  547. __u32 max_burst_bytes;
  548. struct sctp_association *asoc = transport->asoc;
  549. struct sctp_sock *sp = sctp_sk(asoc->base.sk);
  550. struct sctp_outq *q = &asoc->outqueue;
  551. /* RFC 2960 6.1 Transmission of DATA Chunks
  552. *
  553. * A) At any given time, the data sender MUST NOT transmit new data to
  554. * any destination transport address if its peer's rwnd indicates
  555. * that the peer has no buffer space (i.e. rwnd is 0, see Section
  556. * 6.2.1). However, regardless of the value of rwnd (including if it
  557. * is 0), the data sender can always have one DATA chunk in flight to
  558. * the receiver if allowed by cwnd (see rule B below). This rule
  559. * allows the sender to probe for a change in rwnd that the sender
  560. * missed due to the SACK having been lost in transit from the data
  561. * receiver to the data sender.
  562. */
  563. rwnd = asoc->peer.rwnd;
  564. inflight = asoc->outqueue.outstanding_bytes;
  565. datasize = sctp_data_size(chunk);
  566. if (datasize > rwnd) {
  567. if (inflight > 0) {
  568. /* We have (at least) one data chunk in flight,
  569. * so we can't fall back to rule 6.1 B).
  570. */
  571. retval = SCTP_XMIT_RWND_FULL;
  572. goto finish;
  573. }
  574. }
  575. /* sctpimpguide-05 2.14.2
  576. * D) When the time comes for the sender to
  577. * transmit new DATA chunks, the protocol parameter Max.Burst MUST
  578. * first be applied to limit how many new DATA chunks may be sent.
  579. * The limit is applied by adjusting cwnd as follows:
  580. * if ((flightsize + Max.Burst * MTU) < cwnd)
  581. * cwnd = flightsize + Max.Burst * MTU
  582. */
  583. max_burst_bytes = asoc->max_burst * asoc->pathmtu;
  584. if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
  585. transport->cwnd = transport->flight_size + max_burst_bytes;
  586. SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
  587. "transport: %p, cwnd: %d, "
  588. "ssthresh: %d, flight_size: %d, "
  589. "pba: %d\n",
  590. __func__, transport,
  591. transport->cwnd,
  592. transport->ssthresh,
  593. transport->flight_size,
  594. transport->partial_bytes_acked);
  595. }
  596. /* RFC 2960 6.1 Transmission of DATA Chunks
  597. *
  598. * B) At any given time, the sender MUST NOT transmit new data
  599. * to a given transport address if it has cwnd or more bytes
  600. * of data outstanding to that transport address.
  601. */
  602. /* RFC 7.2.4 & the Implementers Guide 2.8.
  603. *
  604. * 3) ...
  605. * When a Fast Retransmit is being performed the sender SHOULD
  606. * ignore the value of cwnd and SHOULD NOT delay retransmission.
  607. */
  608. if (chunk->fast_retransmit <= 0)
  609. if (transport->flight_size >= transport->cwnd) {
  610. retval = SCTP_XMIT_RWND_FULL;
  611. goto finish;
  612. }
  613. /* Nagle's algorithm to solve small-packet problem:
  614. * Inhibit the sending of new chunks when new outgoing data arrives
  615. * if any previously transmitted data on the connection remains
  616. * unacknowledged.
  617. */
  618. if (!sp->nodelay && sctp_packet_empty(packet) &&
  619. q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
  620. unsigned len = datasize + q->out_qlen;
  621. /* Check whether this chunk and all the rest of pending
  622. * data will fit or delay in hopes of bundling a full
  623. * sized packet.
  624. */
  625. if (len < asoc->frag_point) {
  626. retval = SCTP_XMIT_NAGLE_DELAY;
  627. goto finish;
  628. }
  629. }
  630. /* Keep track of how many bytes are in flight over this transport. */
  631. transport->flight_size += datasize;
  632. /* Keep track of how many bytes are in flight to the receiver. */
  633. asoc->outqueue.outstanding_bytes += datasize;
  634. /* Update our view of the receiver's rwnd. Include sk_buff overhead
  635. * while updating peer.rwnd so that it reduces the chances of a
  636. * receiver running out of receive buffer space even when receive
  637. * window is still open. This can happen when a sender is sending
  638. * sending small messages.
  639. */
  640. datasize += sizeof(struct sk_buff);
  641. if (datasize < rwnd)
  642. rwnd -= datasize;
  643. else
  644. rwnd = 0;
  645. asoc->peer.rwnd = rwnd;
  646. /* Has been accepted for transmission. */
  647. if (!asoc->peer.prsctp_capable)
  648. chunk->msg->can_abandon = 0;
  649. finish:
  650. return retval;
  651. }