output.c 22 KB

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