output.c 23 KB

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