chunk.c 10 KB

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