output.c 22 KB

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