output.c 23 KB

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