output.c 24 KB

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