output.c 23 KB

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