chunk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2003, 2004
  4. *
  5. * This file is part of the SCTP kernel implementation
  6. *
  7. * This file contains the code relating the chunk abstraction.
  8. *
  9. * Please send any bug reports or fixes you make to the
  10. * email address(es):
  11. * lksctp developers <linux-sctp@vger.kernel.org>
  12. *
  13. * Written or modified by:
  14. * Jon Grimm <jgrimm@us.ibm.com>
  15. * Sridhar Samudrala <sri@us.ibm.com>
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/net.h>
  21. #include <linux/inet.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/slab.h>
  24. #include <net/sock.h>
  25. #include <net/sctp/sctp.h>
  26. #include <net/sctp/sm.h>
  27. /* This file is mostly in anticipation of future work, but initially
  28. * populate with fragment tracking for an outbound message.
  29. */
  30. /* Initialize datamsg from memory. */
  31. static void sctp_datamsg_init(struct sctp_datamsg *msg)
  32. {
  33. refcount_set(&msg->refcnt, 1);
  34. msg->send_failed = 0;
  35. msg->send_error = 0;
  36. msg->can_delay = 1;
  37. msg->abandoned = 0;
  38. msg->expires_at = 0;
  39. INIT_LIST_HEAD(&msg->chunks);
  40. }
  41. /* Allocate and initialize datamsg. */
  42. static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
  43. {
  44. struct sctp_datamsg *msg;
  45. msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
  46. if (msg) {
  47. sctp_datamsg_init(msg);
  48. SCTP_DBG_OBJCNT_INC(datamsg);
  49. }
  50. return msg;
  51. }
  52. void sctp_datamsg_free(struct sctp_datamsg *msg)
  53. {
  54. struct sctp_chunk *chunk;
  55. /* This doesn't have to be a _safe vairant because
  56. * sctp_chunk_free() only drops the refs.
  57. */
  58. list_for_each_entry(chunk, &msg->chunks, frag_list)
  59. sctp_chunk_free(chunk);
  60. sctp_datamsg_put(msg);
  61. }
  62. /* Final destructruction of datamsg memory. */
  63. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  64. {
  65. struct sctp_association *asoc = NULL;
  66. struct list_head *pos, *temp;
  67. struct sctp_chunk *chunk;
  68. struct sctp_ulpevent *ev;
  69. int error, sent;
  70. /* Release all references. */
  71. list_for_each_safe(pos, temp, &msg->chunks) {
  72. list_del_init(pos);
  73. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  74. if (!msg->send_failed) {
  75. sctp_chunk_put(chunk);
  76. continue;
  77. }
  78. asoc = chunk->asoc;
  79. error = msg->send_error ?: asoc->outqueue.error;
  80. sent = chunk->has_tsn ? SCTP_DATA_SENT : SCTP_DATA_UNSENT;
  81. if (sctp_ulpevent_type_enabled(asoc->subscribe,
  82. SCTP_SEND_FAILED)) {
  83. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  84. error, GFP_ATOMIC);
  85. if (ev)
  86. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  87. }
  88. if (sctp_ulpevent_type_enabled(asoc->subscribe,
  89. SCTP_SEND_FAILED_EVENT)) {
  90. ev = sctp_ulpevent_make_send_failed_event(asoc, chunk,
  91. sent, error,
  92. GFP_ATOMIC);
  93. if (ev)
  94. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  95. }
  96. sctp_chunk_put(chunk);
  97. }
  98. SCTP_DBG_OBJCNT_DEC(datamsg);
  99. kfree(msg);
  100. }
  101. /* Hold a reference. */
  102. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  103. {
  104. refcount_inc(&msg->refcnt);
  105. }
  106. /* Release a reference. */
  107. void sctp_datamsg_put(struct sctp_datamsg *msg)
  108. {
  109. if (refcount_dec_and_test(&msg->refcnt))
  110. sctp_datamsg_destroy(msg);
  111. }
  112. /* Assign a chunk to this datamsg. */
  113. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  114. {
  115. sctp_datamsg_hold(msg);
  116. chunk->msg = msg;
  117. }
  118. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  119. * down any such message into smaller chunks. Opportunistically, fragment
  120. * the chunks down to the current MTU constraints. We may get refragmented
  121. * later if the PMTU changes, but it is _much better_ to fragment immediately
  122. * with a reasonable guess than always doing our fragmentation on the
  123. * soft-interrupt.
  124. */
  125. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  126. struct sctp_sndrcvinfo *sinfo,
  127. struct iov_iter *from)
  128. {
  129. size_t len, first_len, max_data, remaining;
  130. size_t msg_len = iov_iter_count(from);
  131. struct sctp_shared_key *shkey = NULL;
  132. struct list_head *pos, *temp;
  133. struct sctp_chunk *chunk;
  134. struct sctp_datamsg *msg;
  135. int err;
  136. msg = sctp_datamsg_new(GFP_KERNEL);
  137. if (!msg)
  138. return ERR_PTR(-ENOMEM);
  139. /* Note: Calculate this outside of the loop, so that all fragments
  140. * have the same expiration.
  141. */
  142. if (asoc->peer.prsctp_capable && sinfo->sinfo_timetolive &&
  143. (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags) ||
  144. !SCTP_PR_POLICY(sinfo->sinfo_flags)))
  145. msg->expires_at = jiffies +
  146. msecs_to_jiffies(sinfo->sinfo_timetolive);
  147. /* This is the biggest possible DATA chunk that can fit into
  148. * the packet
  149. */
  150. max_data = asoc->frag_point;
  151. if (unlikely(!max_data)) {
  152. max_data = sctp_min_frag_point(sctp_sk(asoc->base.sk),
  153. sctp_datachk_len(&asoc->stream));
  154. pr_warn_ratelimited("%s: asoc:%p frag_point is zero, forcing max_data to default minimum (%zu)",
  155. __func__, asoc, max_data);
  156. }
  157. /* If the peer requested that we authenticate DATA chunks
  158. * we need to account for bundling of the AUTH chunks along with
  159. * DATA.
  160. */
  161. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  162. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  163. if (hmac_desc)
  164. max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
  165. hmac_desc->hmac_len);
  166. if (sinfo->sinfo_tsn &&
  167. sinfo->sinfo_ssn != asoc->active_key_id) {
  168. shkey = sctp_auth_get_shkey(asoc, sinfo->sinfo_ssn);
  169. if (!shkey) {
  170. err = -EINVAL;
  171. goto errout;
  172. }
  173. } else {
  174. shkey = asoc->shkey;
  175. }
  176. }
  177. /* Set first_len and then account for possible bundles on first frag */
  178. first_len = max_data;
  179. /* Check to see if we have a pending SACK and try to let it be bundled
  180. * with this message. Do this if we don't have any data queued already.
  181. * To check that, look at out_qlen and retransmit list.
  182. * NOTE: we will not reduce to account for SACK, if the message would
  183. * not have been fragmented.
  184. */
  185. if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
  186. asoc->outqueue.out_qlen == 0 &&
  187. list_empty(&asoc->outqueue.retransmit) &&
  188. msg_len > max_data)
  189. first_len -= SCTP_PAD4(sizeof(struct sctp_sack_chunk));
  190. /* Encourage Cookie-ECHO bundling. */
  191. if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
  192. first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  193. /* Account for a different sized first fragment */
  194. if (msg_len >= first_len) {
  195. msg->can_delay = 0;
  196. if (msg_len > first_len)
  197. SCTP_INC_STATS(asoc->base.net,
  198. SCTP_MIB_FRAGUSRMSGS);
  199. } else {
  200. /* Which may be the only one... */
  201. first_len = msg_len;
  202. }
  203. /* Create chunks for all DATA chunks. */
  204. for (remaining = msg_len; remaining; remaining -= len) {
  205. u8 frag = SCTP_DATA_MIDDLE_FRAG;
  206. if (remaining == msg_len) {
  207. /* First frag, which may also be the last */
  208. frag |= SCTP_DATA_FIRST_FRAG;
  209. len = first_len;
  210. } else {
  211. /* Middle frags */
  212. len = max_data;
  213. }
  214. if (len >= remaining) {
  215. /* Last frag, which may also be the first */
  216. len = remaining;
  217. frag |= SCTP_DATA_LAST_FRAG;
  218. /* The application requests to set the I-bit of the
  219. * last DATA chunk of a user message when providing
  220. * the user message to the SCTP implementation.
  221. */
  222. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  223. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  224. frag |= SCTP_DATA_SACK_IMM;
  225. }
  226. chunk = asoc->stream.si->make_datafrag(asoc, sinfo, len, frag,
  227. GFP_KERNEL);
  228. if (!chunk) {
  229. err = -ENOMEM;
  230. goto errout;
  231. }
  232. err = sctp_user_addto_chunk(chunk, len, from);
  233. if (err < 0)
  234. goto errout_chunk_free;
  235. chunk->shkey = shkey;
  236. /* Put the chunk->skb back into the form expected by send. */
  237. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
  238. chunk->skb->data);
  239. sctp_datamsg_assign(msg, chunk);
  240. list_add_tail(&chunk->frag_list, &msg->chunks);
  241. }
  242. return msg;
  243. errout_chunk_free:
  244. sctp_chunk_free(chunk);
  245. errout:
  246. list_for_each_safe(pos, temp, &msg->chunks) {
  247. list_del_init(pos);
  248. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  249. sctp_chunk_free(chunk);
  250. }
  251. sctp_datamsg_put(msg);
  252. return ERR_PTR(err);
  253. }
  254. /* Check whether this message has expired. */
  255. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  256. {
  257. if (!chunk->asoc->peer.prsctp_capable)
  258. return 0;
  259. if (chunk->msg->abandoned)
  260. return 1;
  261. if (!chunk->has_tsn &&
  262. !(chunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG))
  263. return 0;
  264. if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
  265. time_after(jiffies, chunk->msg->expires_at)) {
  266. struct sctp_stream_out *streamout =
  267. SCTP_SO(&chunk->asoc->stream,
  268. chunk->sinfo.sinfo_stream);
  269. if (chunk->sent_count) {
  270. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  271. streamout->ext->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  272. } else {
  273. chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  274. streamout->ext->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  275. }
  276. chunk->msg->abandoned = 1;
  277. return 1;
  278. } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
  279. chunk->sent_count > chunk->sinfo.sinfo_timetolive) {
  280. struct sctp_stream_out *streamout =
  281. SCTP_SO(&chunk->asoc->stream,
  282. chunk->sinfo.sinfo_stream);
  283. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  284. streamout->ext->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  285. chunk->msg->abandoned = 1;
  286. return 1;
  287. } else if (!SCTP_PR_POLICY(chunk->sinfo.sinfo_flags) &&
  288. chunk->msg->expires_at &&
  289. time_after(jiffies, chunk->msg->expires_at)) {
  290. chunk->msg->abandoned = 1;
  291. return 1;
  292. }
  293. /* PRIO policy is processed by sendmsg, not here */
  294. return 0;
  295. }
  296. /* This chunk (and consequently entire message) has failed in its sending. */
  297. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  298. {
  299. chunk->msg->send_failed = 1;
  300. chunk->msg->send_error = error;
  301. }