output.c 23 KB

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