output.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
  56. struct sctp_chunk *chunk);
  57. static enum sctp_xmit 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 enum sctp_xmit 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. /* sctp_packet_transmit() relies on this to reset size to the
  67. * current overhead after sending packets.
  68. */
  69. packet->size = packet->overhead;
  70. packet->has_cookie_echo = 0;
  71. packet->has_sack = 0;
  72. packet->has_data = 0;
  73. packet->has_auth = 0;
  74. packet->ipfragok = 0;
  75. packet->auth = NULL;
  76. }
  77. /* Config a packet.
  78. * This appears to be a followup set of initializations.
  79. */
  80. void sctp_packet_config(struct sctp_packet *packet, __u32 vtag,
  81. int ecn_capable)
  82. {
  83. struct sctp_transport *tp = packet->transport;
  84. struct sctp_association *asoc = tp->asoc;
  85. struct sctp_sock *sp = NULL;
  86. struct sock *sk;
  87. pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
  88. packet->vtag = vtag;
  89. /* do the following jobs only once for a flush schedule */
  90. if (!sctp_packet_empty(packet))
  91. return;
  92. /* set packet max_size with pathmtu, then calculate overhead */
  93. packet->max_size = tp->pathmtu;
  94. if (asoc) {
  95. sk = asoc->base.sk;
  96. sp = sctp_sk(sk);
  97. }
  98. packet->overhead = sctp_mtu_payload(sp, 0, 0);
  99. packet->size = packet->overhead;
  100. if (!asoc)
  101. return;
  102. /* update dst or transport pathmtu if in need */
  103. if (!sctp_transport_dst_check(tp)) {
  104. sctp_transport_route(tp, NULL, sp);
  105. if (asoc->param_flags & SPP_PMTUD_ENABLE)
  106. sctp_assoc_sync_pmtu(asoc);
  107. } else if (!sctp_transport_pmtu_check(tp)) {
  108. if (asoc->param_flags & SPP_PMTUD_ENABLE)
  109. sctp_assoc_sync_pmtu(asoc);
  110. }
  111. if (asoc->pmtu_pending) {
  112. if (asoc->param_flags & SPP_PMTUD_ENABLE)
  113. sctp_assoc_sync_pmtu(asoc);
  114. asoc->pmtu_pending = 0;
  115. }
  116. /* If there a is a prepend chunk stick it on the list before
  117. * any other chunks get appended.
  118. */
  119. if (ecn_capable) {
  120. struct sctp_chunk *chunk = sctp_get_ecne_prepend(asoc);
  121. if (chunk)
  122. sctp_packet_append_chunk(packet, chunk);
  123. }
  124. if (!tp->dst)
  125. return;
  126. /* set packet max_size with gso_max_size if gso is enabled*/
  127. rcu_read_lock();
  128. if (__sk_dst_get(sk) != tp->dst) {
  129. dst_hold(tp->dst);
  130. sk_setup_caps(sk, tp->dst);
  131. }
  132. packet->max_size = sk_can_gso(sk) ? tp->dst->dev->gso_max_size
  133. : asoc->pathmtu;
  134. rcu_read_unlock();
  135. }
  136. /* Initialize the packet structure. */
  137. void sctp_packet_init(struct sctp_packet *packet,
  138. struct sctp_transport *transport,
  139. __u16 sport, __u16 dport)
  140. {
  141. pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
  142. packet->transport = transport;
  143. packet->source_port = sport;
  144. packet->destination_port = dport;
  145. INIT_LIST_HEAD(&packet->chunk_list);
  146. /* The overhead will be calculated by sctp_packet_config() */
  147. packet->overhead = 0;
  148. sctp_packet_reset(packet);
  149. packet->vtag = 0;
  150. }
  151. /* Free a packet. */
  152. void sctp_packet_free(struct sctp_packet *packet)
  153. {
  154. struct sctp_chunk *chunk, *tmp;
  155. pr_debug("%s: packet:%p\n", __func__, packet);
  156. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  157. list_del_init(&chunk->list);
  158. sctp_chunk_free(chunk);
  159. }
  160. }
  161. /* This routine tries to append the chunk to the offered packet. If adding
  162. * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
  163. * is not present in the packet, it transmits the input packet.
  164. * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
  165. * as it can fit in the packet, but any more data that does not fit in this
  166. * packet can be sent only after receiving the COOKIE_ACK.
  167. */
  168. enum sctp_xmit sctp_packet_transmit_chunk(struct sctp_packet *packet,
  169. struct sctp_chunk *chunk,
  170. int one_packet, gfp_t gfp)
  171. {
  172. enum sctp_xmit retval;
  173. pr_debug("%s: packet:%p size:%zu chunk:%p size:%d\n", __func__,
  174. packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
  175. switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
  176. case SCTP_XMIT_PMTU_FULL:
  177. if (!packet->has_cookie_echo) {
  178. int error = 0;
  179. error = sctp_packet_transmit(packet, gfp);
  180. if (error < 0)
  181. chunk->skb->sk->sk_err = -error;
  182. /* If we have an empty packet, then we can NOT ever
  183. * return PMTU_FULL.
  184. */
  185. if (!one_packet)
  186. retval = sctp_packet_append_chunk(packet,
  187. chunk);
  188. }
  189. break;
  190. case SCTP_XMIT_RWND_FULL:
  191. case SCTP_XMIT_OK:
  192. case SCTP_XMIT_DELAY:
  193. break;
  194. }
  195. return retval;
  196. }
  197. /* Try to bundle an auth chunk into the packet. */
  198. static enum sctp_xmit sctp_packet_bundle_auth(struct sctp_packet *pkt,
  199. struct sctp_chunk *chunk)
  200. {
  201. struct sctp_association *asoc = pkt->transport->asoc;
  202. enum sctp_xmit retval = SCTP_XMIT_OK;
  203. struct sctp_chunk *auth;
  204. /* if we don't have an association, we can't do authentication */
  205. if (!asoc)
  206. return retval;
  207. /* See if this is an auth chunk we are bundling or if
  208. * auth is already bundled.
  209. */
  210. if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
  211. return retval;
  212. /* if the peer did not request this chunk to be authenticated,
  213. * don't do it
  214. */
  215. if (!chunk->auth)
  216. return retval;
  217. auth = sctp_make_auth(asoc, chunk->shkey->key_id);
  218. if (!auth)
  219. return retval;
  220. auth->shkey = chunk->shkey;
  221. sctp_auth_shkey_hold(auth->shkey);
  222. retval = __sctp_packet_append_chunk(pkt, auth);
  223. if (retval != SCTP_XMIT_OK)
  224. sctp_chunk_free(auth);
  225. return retval;
  226. }
  227. /* Try to bundle a SACK with the packet. */
  228. static enum sctp_xmit sctp_packet_bundle_sack(struct sctp_packet *pkt,
  229. struct sctp_chunk *chunk)
  230. {
  231. enum sctp_xmit retval = SCTP_XMIT_OK;
  232. /* If sending DATA and haven't aleady bundled a SACK, try to
  233. * bundle one in to the packet.
  234. */
  235. if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
  236. !pkt->has_cookie_echo) {
  237. struct sctp_association *asoc;
  238. struct timer_list *timer;
  239. asoc = pkt->transport->asoc;
  240. timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
  241. /* If the SACK timer is running, we have a pending SACK */
  242. if (timer_pending(timer)) {
  243. struct sctp_chunk *sack;
  244. if (pkt->transport->sack_generation !=
  245. pkt->transport->asoc->peer.sack_generation)
  246. return retval;
  247. asoc->a_rwnd = asoc->rwnd;
  248. sack = sctp_make_sack(asoc);
  249. if (sack) {
  250. retval = __sctp_packet_append_chunk(pkt, sack);
  251. if (retval != SCTP_XMIT_OK) {
  252. sctp_chunk_free(sack);
  253. goto out;
  254. }
  255. asoc->peer.sack_needed = 0;
  256. if (del_timer(timer))
  257. sctp_association_put(asoc);
  258. }
  259. }
  260. }
  261. out:
  262. return retval;
  263. }
  264. /* Append a chunk to the offered packet reporting back any inability to do
  265. * so.
  266. */
  267. static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
  268. struct sctp_chunk *chunk)
  269. {
  270. __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
  271. enum sctp_xmit retval = SCTP_XMIT_OK;
  272. /* Check to see if this chunk will fit into the packet */
  273. retval = sctp_packet_will_fit(packet, chunk, chunk_len);
  274. if (retval != SCTP_XMIT_OK)
  275. goto finish;
  276. /* We believe that this chunk is OK to add to the packet */
  277. switch (chunk->chunk_hdr->type) {
  278. case SCTP_CID_DATA:
  279. case SCTP_CID_I_DATA:
  280. /* Account for the data being in the packet */
  281. sctp_packet_append_data(packet, chunk);
  282. /* Disallow SACK bundling after DATA. */
  283. packet->has_sack = 1;
  284. /* Disallow AUTH bundling after DATA */
  285. packet->has_auth = 1;
  286. /* Let it be knows that packet has DATA in it */
  287. packet->has_data = 1;
  288. /* timestamp the chunk for rtx purposes */
  289. chunk->sent_at = jiffies;
  290. /* Mainly used for prsctp RTX policy */
  291. chunk->sent_count++;
  292. break;
  293. case SCTP_CID_COOKIE_ECHO:
  294. packet->has_cookie_echo = 1;
  295. break;
  296. case SCTP_CID_SACK:
  297. packet->has_sack = 1;
  298. if (chunk->asoc)
  299. chunk->asoc->stats.osacks++;
  300. break;
  301. case SCTP_CID_AUTH:
  302. packet->has_auth = 1;
  303. packet->auth = chunk;
  304. break;
  305. }
  306. /* It is OK to send this chunk. */
  307. list_add_tail(&chunk->list, &packet->chunk_list);
  308. packet->size += chunk_len;
  309. chunk->transport = packet->transport;
  310. finish:
  311. return retval;
  312. }
  313. /* Append a chunk to the offered packet reporting back any inability to do
  314. * so.
  315. */
  316. enum sctp_xmit sctp_packet_append_chunk(struct sctp_packet *packet,
  317. struct sctp_chunk *chunk)
  318. {
  319. enum sctp_xmit retval = SCTP_XMIT_OK;
  320. pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
  321. /* Data chunks are special. Before seeing what else we can
  322. * bundle into this packet, check to see if we are allowed to
  323. * send this DATA.
  324. */
  325. if (sctp_chunk_is_data(chunk)) {
  326. retval = sctp_packet_can_append_data(packet, chunk);
  327. if (retval != SCTP_XMIT_OK)
  328. goto finish;
  329. }
  330. /* Try to bundle AUTH chunk */
  331. retval = sctp_packet_bundle_auth(packet, chunk);
  332. if (retval != SCTP_XMIT_OK)
  333. goto finish;
  334. /* Try to bundle SACK chunk */
  335. retval = sctp_packet_bundle_sack(packet, chunk);
  336. if (retval != SCTP_XMIT_OK)
  337. goto finish;
  338. retval = __sctp_packet_append_chunk(packet, chunk);
  339. finish:
  340. return retval;
  341. }
  342. static void sctp_packet_gso_append(struct sk_buff *head, struct sk_buff *skb)
  343. {
  344. if (SCTP_OUTPUT_CB(head)->last == head)
  345. skb_shinfo(head)->frag_list = skb;
  346. else
  347. SCTP_OUTPUT_CB(head)->last->next = skb;
  348. SCTP_OUTPUT_CB(head)->last = skb;
  349. head->truesize += skb->truesize;
  350. head->data_len += skb->len;
  351. head->len += skb->len;
  352. refcount_add(skb->truesize, &head->sk->sk_wmem_alloc);
  353. __skb_header_release(skb);
  354. }
  355. static int sctp_packet_pack(struct sctp_packet *packet,
  356. struct sk_buff *head, int gso, gfp_t gfp)
  357. {
  358. struct sctp_transport *tp = packet->transport;
  359. struct sctp_auth_chunk *auth = NULL;
  360. struct sctp_chunk *chunk, *tmp;
  361. int pkt_count = 0, pkt_size;
  362. struct sock *sk = head->sk;
  363. struct sk_buff *nskb;
  364. int auth_len = 0;
  365. if (gso) {
  366. skb_shinfo(head)->gso_type = sk->sk_gso_type;
  367. SCTP_OUTPUT_CB(head)->last = head;
  368. } else {
  369. nskb = head;
  370. pkt_size = packet->size;
  371. goto merge;
  372. }
  373. do {
  374. /* calculate the pkt_size and alloc nskb */
  375. pkt_size = packet->overhead;
  376. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list,
  377. list) {
  378. int padded = SCTP_PAD4(chunk->skb->len);
  379. if (chunk == packet->auth)
  380. auth_len = padded;
  381. else if (auth_len + padded + packet->overhead >
  382. tp->pathmtu)
  383. return 0;
  384. else if (pkt_size + padded > tp->pathmtu)
  385. break;
  386. pkt_size += padded;
  387. }
  388. nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
  389. if (!nskb)
  390. return 0;
  391. skb_reserve(nskb, packet->overhead + MAX_HEADER);
  392. merge:
  393. /* merge chunks into nskb and append nskb into head list */
  394. pkt_size -= packet->overhead;
  395. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  396. int padding;
  397. list_del_init(&chunk->list);
  398. if (sctp_chunk_is_data(chunk)) {
  399. if (!sctp_chunk_retransmitted(chunk) &&
  400. !tp->rto_pending) {
  401. chunk->rtt_in_progress = 1;
  402. tp->rto_pending = 1;
  403. }
  404. }
  405. padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
  406. if (padding)
  407. skb_put_zero(chunk->skb, padding);
  408. if (chunk == packet->auth)
  409. auth = (struct sctp_auth_chunk *)
  410. skb_tail_pointer(nskb);
  411. skb_put_data(nskb, chunk->skb->data, chunk->skb->len);
  412. pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
  413. chunk,
  414. sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
  415. chunk->has_tsn ? "TSN" : "No TSN",
  416. chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
  417. ntohs(chunk->chunk_hdr->length), chunk->skb->len,
  418. chunk->rtt_in_progress);
  419. pkt_size -= SCTP_PAD4(chunk->skb->len);
  420. if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
  421. sctp_chunk_free(chunk);
  422. if (!pkt_size)
  423. break;
  424. }
  425. if (auth) {
  426. sctp_auth_calculate_hmac(tp->asoc, nskb, auth,
  427. packet->auth->shkey, gfp);
  428. /* free auth if no more chunks, or add it back */
  429. if (list_empty(&packet->chunk_list))
  430. sctp_chunk_free(packet->auth);
  431. else
  432. list_add(&packet->auth->list,
  433. &packet->chunk_list);
  434. }
  435. if (gso)
  436. sctp_packet_gso_append(head, nskb);
  437. pkt_count++;
  438. } while (!list_empty(&packet->chunk_list));
  439. if (gso) {
  440. memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
  441. sizeof(struct inet6_skb_parm)));
  442. skb_shinfo(head)->gso_segs = pkt_count;
  443. skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
  444. rcu_read_lock();
  445. if (skb_dst(head) != tp->dst) {
  446. dst_hold(tp->dst);
  447. sk_setup_caps(sk, tp->dst);
  448. }
  449. rcu_read_unlock();
  450. goto chksum;
  451. }
  452. if (sctp_checksum_disable)
  453. return 1;
  454. if (!(skb_dst(head)->dev->features & NETIF_F_SCTP_CRC) ||
  455. dst_xfrm(skb_dst(head)) || packet->ipfragok) {
  456. struct sctphdr *sh =
  457. (struct sctphdr *)skb_transport_header(head);
  458. sh->checksum = sctp_compute_cksum(head, 0);
  459. } else {
  460. chksum:
  461. head->ip_summed = CHECKSUM_PARTIAL;
  462. head->csum_not_inet = 1;
  463. head->csum_start = skb_transport_header(head) - head->head;
  464. head->csum_offset = offsetof(struct sctphdr, checksum);
  465. }
  466. return pkt_count;
  467. }
  468. /* All packets are sent to the network through this function from
  469. * sctp_outq_tail().
  470. *
  471. * The return value is always 0 for now.
  472. */
  473. int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
  474. {
  475. struct sctp_transport *tp = packet->transport;
  476. struct sctp_association *asoc = tp->asoc;
  477. struct sctp_chunk *chunk, *tmp;
  478. int pkt_count, gso = 0;
  479. struct dst_entry *dst;
  480. struct sk_buff *head;
  481. struct sctphdr *sh;
  482. struct sock *sk;
  483. pr_debug("%s: packet:%p\n", __func__, packet);
  484. if (list_empty(&packet->chunk_list))
  485. return 0;
  486. chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
  487. sk = chunk->skb->sk;
  488. /* check gso */
  489. if (packet->size > tp->pathmtu && !packet->ipfragok) {
  490. if (!sk_can_gso(sk)) {
  491. pr_err_once("Trying to GSO but underlying device doesn't support it.");
  492. goto out;
  493. }
  494. gso = 1;
  495. }
  496. /* alloc head skb */
  497. head = alloc_skb((gso ? packet->overhead : packet->size) +
  498. MAX_HEADER, gfp);
  499. if (!head)
  500. goto out;
  501. skb_reserve(head, packet->overhead + MAX_HEADER);
  502. skb_set_owner_w(head, sk);
  503. /* set sctp header */
  504. sh = skb_push(head, sizeof(struct sctphdr));
  505. skb_reset_transport_header(head);
  506. sh->source = htons(packet->source_port);
  507. sh->dest = htons(packet->destination_port);
  508. sh->vtag = htonl(packet->vtag);
  509. sh->checksum = 0;
  510. /* drop packet if no dst */
  511. dst = dst_clone(tp->dst);
  512. if (!dst) {
  513. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  514. kfree_skb(head);
  515. goto out;
  516. }
  517. skb_dst_set(head, dst);
  518. /* pack up chunks */
  519. pkt_count = sctp_packet_pack(packet, head, gso, gfp);
  520. if (!pkt_count) {
  521. kfree_skb(head);
  522. goto out;
  523. }
  524. pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
  525. /* start autoclose timer */
  526. if (packet->has_data && sctp_state(asoc, ESTABLISHED) &&
  527. asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  528. struct timer_list *timer =
  529. &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  530. unsigned long timeout =
  531. asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
  532. if (!mod_timer(timer, jiffies + timeout))
  533. sctp_association_hold(asoc);
  534. }
  535. /* sctp xmit */
  536. tp->af_specific->ecn_capable(sk);
  537. if (asoc) {
  538. asoc->stats.opackets += pkt_count;
  539. if (asoc->peer.last_sent_to != tp)
  540. asoc->peer.last_sent_to = tp;
  541. }
  542. head->ignore_df = packet->ipfragok;
  543. if (tp->dst_pending_confirm)
  544. skb_set_dst_pending_confirm(head, 1);
  545. /* neighbour should be confirmed on successful transmission or
  546. * positive error
  547. */
  548. if (tp->af_specific->sctp_xmit(head, tp) >= 0 &&
  549. tp->dst_pending_confirm)
  550. tp->dst_pending_confirm = 0;
  551. out:
  552. list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
  553. list_del_init(&chunk->list);
  554. if (!sctp_chunk_is_data(chunk))
  555. sctp_chunk_free(chunk);
  556. }
  557. sctp_packet_reset(packet);
  558. return 0;
  559. }
  560. /********************************************************************
  561. * 2nd Level Abstractions
  562. ********************************************************************/
  563. /* This private function check to see if a chunk can be added */
  564. static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
  565. struct sctp_chunk *chunk)
  566. {
  567. size_t datasize, rwnd, inflight, flight_size;
  568. struct sctp_transport *transport = packet->transport;
  569. struct sctp_association *asoc = transport->asoc;
  570. struct sctp_outq *q = &asoc->outqueue;
  571. /* RFC 2960 6.1 Transmission of DATA Chunks
  572. *
  573. * A) At any given time, the data sender MUST NOT transmit new data to
  574. * any destination transport address if its peer's rwnd indicates
  575. * that the peer has no buffer space (i.e. rwnd is 0, see Section
  576. * 6.2.1). However, regardless of the value of rwnd (including if it
  577. * is 0), the data sender can always have one DATA chunk in flight to
  578. * the receiver if allowed by cwnd (see rule B below). This rule
  579. * allows the sender to probe for a change in rwnd that the sender
  580. * missed due to the SACK having been lost in transit from the data
  581. * receiver to the data sender.
  582. */
  583. rwnd = asoc->peer.rwnd;
  584. inflight = q->outstanding_bytes;
  585. flight_size = transport->flight_size;
  586. datasize = sctp_data_size(chunk);
  587. if (datasize > rwnd && inflight > 0)
  588. /* We have (at least) one data chunk in flight,
  589. * so we can't fall back to rule 6.1 B).
  590. */
  591. return SCTP_XMIT_RWND_FULL;
  592. /* RFC 2960 6.1 Transmission of DATA Chunks
  593. *
  594. * B) At any given time, the sender MUST NOT transmit new data
  595. * to a given transport address if it has cwnd or more bytes
  596. * of data outstanding to that transport address.
  597. */
  598. /* RFC 7.2.4 & the Implementers Guide 2.8.
  599. *
  600. * 3) ...
  601. * When a Fast Retransmit is being performed the sender SHOULD
  602. * ignore the value of cwnd and SHOULD NOT delay retransmission.
  603. */
  604. if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
  605. flight_size >= transport->cwnd)
  606. return SCTP_XMIT_RWND_FULL;
  607. /* Nagle's algorithm to solve small-packet problem:
  608. * Inhibit the sending of new chunks when new outgoing data arrives
  609. * if any previously transmitted data on the connection remains
  610. * unacknowledged.
  611. */
  612. if ((sctp_sk(asoc->base.sk)->nodelay || inflight == 0) &&
  613. !asoc->force_delay)
  614. /* Nothing unacked */
  615. return SCTP_XMIT_OK;
  616. if (!sctp_packet_empty(packet))
  617. /* Append to packet */
  618. return SCTP_XMIT_OK;
  619. if (!sctp_state(asoc, ESTABLISHED))
  620. return SCTP_XMIT_OK;
  621. /* Check whether this chunk and all the rest of pending data will fit
  622. * or delay in hopes of bundling a full sized packet.
  623. */
  624. if (chunk->skb->len + q->out_qlen > transport->pathmtu -
  625. packet->overhead - sctp_datachk_len(&chunk->asoc->stream) - 4)
  626. /* Enough data queued to fill a packet */
  627. return SCTP_XMIT_OK;
  628. /* Don't delay large message writes that may have been fragmented */
  629. if (!chunk->msg->can_delay)
  630. return SCTP_XMIT_OK;
  631. /* Defer until all data acked or packet full */
  632. return SCTP_XMIT_DELAY;
  633. }
  634. /* This private function does management things when adding DATA chunk */
  635. static void sctp_packet_append_data(struct sctp_packet *packet,
  636. struct sctp_chunk *chunk)
  637. {
  638. struct sctp_transport *transport = packet->transport;
  639. size_t datasize = sctp_data_size(chunk);
  640. struct sctp_association *asoc = transport->asoc;
  641. u32 rwnd = asoc->peer.rwnd;
  642. /* Keep track of how many bytes are in flight over this transport. */
  643. transport->flight_size += datasize;
  644. /* Keep track of how many bytes are in flight to the receiver. */
  645. asoc->outqueue.outstanding_bytes += datasize;
  646. /* Update our view of the receiver's rwnd. */
  647. if (datasize < rwnd)
  648. rwnd -= datasize;
  649. else
  650. rwnd = 0;
  651. asoc->peer.rwnd = rwnd;
  652. sctp_chunk_assign_tsn(chunk);
  653. asoc->stream.si->assign_number(chunk);
  654. }
  655. static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
  656. struct sctp_chunk *chunk,
  657. u16 chunk_len)
  658. {
  659. enum sctp_xmit retval = SCTP_XMIT_OK;
  660. size_t psize, pmtu, maxsize;
  661. /* Don't bundle in this packet if this chunk's auth key doesn't
  662. * match other chunks already enqueued on this packet. Also,
  663. * don't bundle the chunk with auth key if other chunks in this
  664. * packet don't have auth key.
  665. */
  666. if ((packet->auth && chunk->shkey != packet->auth->shkey) ||
  667. (!packet->auth && chunk->shkey &&
  668. chunk->chunk_hdr->type != SCTP_CID_AUTH))
  669. return SCTP_XMIT_PMTU_FULL;
  670. psize = packet->size;
  671. if (packet->transport->asoc)
  672. pmtu = packet->transport->asoc->pathmtu;
  673. else
  674. pmtu = packet->transport->pathmtu;
  675. /* Decide if we need to fragment or resubmit later. */
  676. if (psize + chunk_len > pmtu) {
  677. /* It's OK to fragment 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 packet doesn't have any data in it yet and data
  682. * requires authentication.
  683. */
  684. if (sctp_packet_empty(packet) ||
  685. (!packet->has_data && chunk->auth)) {
  686. /* We no longer do re-fragmentation.
  687. * Just fragment at the IP layer, if we
  688. * actually hit this condition
  689. */
  690. packet->ipfragok = 1;
  691. goto out;
  692. }
  693. /* Similarly, if this chunk was built before a PMTU
  694. * reduction, we have to fragment it at IP level now. So
  695. * if the packet already contains something, we need to
  696. * flush.
  697. */
  698. maxsize = pmtu - packet->overhead;
  699. if (packet->auth)
  700. maxsize -= SCTP_PAD4(packet->auth->skb->len);
  701. if (chunk_len > maxsize)
  702. retval = SCTP_XMIT_PMTU_FULL;
  703. /* It is also okay to fragment if the chunk we are
  704. * adding is a control chunk, but only if current packet
  705. * is not a GSO one otherwise it causes fragmentation of
  706. * a large frame. So in this case we allow the
  707. * fragmentation by forcing it to be in a new packet.
  708. */
  709. if (!sctp_chunk_is_data(chunk) && packet->has_data)
  710. retval = SCTP_XMIT_PMTU_FULL;
  711. if (psize + chunk_len > packet->max_size)
  712. /* Hit GSO/PMTU limit, gotta flush */
  713. retval = SCTP_XMIT_PMTU_FULL;
  714. if (!packet->transport->burst_limited &&
  715. psize + chunk_len > (packet->transport->cwnd >> 1))
  716. /* Do not allow a single GSO packet to use more
  717. * than half of cwnd.
  718. */
  719. retval = SCTP_XMIT_PMTU_FULL;
  720. if (packet->transport->burst_limited &&
  721. psize + chunk_len > (packet->transport->burst_limited >> 1))
  722. /* Do not allow a single GSO packet to use more
  723. * than half of original cwnd.
  724. */
  725. retval = SCTP_XMIT_PMTU_FULL;
  726. /* Otherwise it will fit in the GSO packet */
  727. }
  728. out:
  729. return retval;
  730. }