output.c 23 KB

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