output.c 22 KB

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