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