chunk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_obj(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. const struct sctp_hmac *hmac_desc =
  163. sctp_auth_asoc_get_hmac(asoc);
  164. if (hmac_desc)
  165. max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
  166. hmac_desc->hmac_len);
  167. if (sinfo->sinfo_tsn &&
  168. sinfo->sinfo_ssn != asoc->active_key_id) {
  169. shkey = sctp_auth_get_shkey(asoc, sinfo->sinfo_ssn);
  170. if (!shkey) {
  171. err = -EINVAL;
  172. goto errout;
  173. }
  174. } else {
  175. shkey = asoc->shkey;
  176. }
  177. }
  178. /* Set first_len and then account for possible bundles on first frag */
  179. first_len = max_data;
  180. /* Check to see if we have a pending SACK and try to let it be bundled
  181. * with this message. Do this if we don't have any data queued already.
  182. * To check that, look at out_qlen and retransmit list.
  183. * NOTE: we will not reduce to account for SACK, if the message would
  184. * not have been fragmented.
  185. */
  186. if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
  187. asoc->outqueue.out_qlen == 0 &&
  188. list_empty(&asoc->outqueue.retransmit) &&
  189. msg_len > max_data)
  190. first_len -= SCTP_PAD4(sizeof(struct sctp_sack_chunk));
  191. /* Encourage Cookie-ECHO bundling. */
  192. if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
  193. first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  194. /* Account for a different sized first fragment */
  195. if (msg_len >= first_len) {
  196. msg->can_delay = 0;
  197. if (msg_len > first_len)
  198. SCTP_INC_STATS(asoc->base.net,
  199. SCTP_MIB_FRAGUSRMSGS);
  200. } else {
  201. /* Which may be the only one... */
  202. first_len = msg_len;
  203. }
  204. /* Create chunks for all DATA chunks. */
  205. for (remaining = msg_len; remaining; remaining -= len) {
  206. u8 frag = SCTP_DATA_MIDDLE_FRAG;
  207. if (remaining == msg_len) {
  208. /* First frag, which may also be the last */
  209. frag |= SCTP_DATA_FIRST_FRAG;
  210. len = first_len;
  211. } else {
  212. /* Middle frags */
  213. len = max_data;
  214. }
  215. if (len >= remaining) {
  216. /* Last frag, which may also be the first */
  217. len = remaining;
  218. frag |= SCTP_DATA_LAST_FRAG;
  219. /* The application requests to set the I-bit of the
  220. * last DATA chunk of a user message when providing
  221. * the user message to the SCTP implementation.
  222. */
  223. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  224. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  225. frag |= SCTP_DATA_SACK_IMM;
  226. }
  227. chunk = asoc->stream.si->make_datafrag(asoc, sinfo, len, frag,
  228. GFP_KERNEL);
  229. if (!chunk) {
  230. err = -ENOMEM;
  231. goto errout;
  232. }
  233. err = sctp_user_addto_chunk(chunk, len, from);
  234. if (err < 0)
  235. goto errout_chunk_free;
  236. chunk->shkey = shkey;
  237. /* Put the chunk->skb back into the form expected by send. */
  238. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
  239. chunk->skb->data);
  240. sctp_datamsg_assign(msg, chunk);
  241. list_add_tail(&chunk->frag_list, &msg->chunks);
  242. }
  243. return msg;
  244. errout_chunk_free:
  245. sctp_chunk_free(chunk);
  246. errout:
  247. list_for_each_safe(pos, temp, &msg->chunks) {
  248. list_del_init(pos);
  249. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  250. sctp_chunk_free(chunk);
  251. }
  252. sctp_datamsg_put(msg);
  253. return ERR_PTR(err);
  254. }
  255. /* Check whether this message has expired. */
  256. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  257. {
  258. if (!chunk->asoc->peer.prsctp_capable)
  259. return 0;
  260. if (chunk->msg->abandoned)
  261. return 1;
  262. if (!chunk->has_tsn &&
  263. !(chunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG))
  264. return 0;
  265. if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
  266. time_after(jiffies, chunk->msg->expires_at)) {
  267. struct sctp_stream_out *streamout =
  268. SCTP_SO(&chunk->asoc->stream,
  269. chunk->sinfo.sinfo_stream);
  270. if (chunk->sent_count) {
  271. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  272. streamout->ext->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  273. } else {
  274. chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  275. streamout->ext->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  276. }
  277. chunk->msg->abandoned = 1;
  278. return 1;
  279. } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
  280. chunk->sent_count > chunk->sinfo.sinfo_timetolive) {
  281. struct sctp_stream_out *streamout =
  282. SCTP_SO(&chunk->asoc->stream,
  283. chunk->sinfo.sinfo_stream);
  284. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  285. streamout->ext->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  286. chunk->msg->abandoned = 1;
  287. return 1;
  288. } else if (!SCTP_PR_POLICY(chunk->sinfo.sinfo_flags) &&
  289. chunk->msg->expires_at &&
  290. time_after(jiffies, chunk->msg->expires_at)) {
  291. chunk->msg->abandoned = 1;
  292. return 1;
  293. }
  294. /* PRIO policy is processed by sendmsg, not here */
  295. return 0;
  296. }
  297. /* This chunk (and consequently entire message) has failed in its sending. */
  298. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  299. {
  300. chunk->msg->send_failed = 1;
  301. chunk->msg->send_error = error;
  302. }