output.c 23 KB

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