chunk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. atomic_set(&msg->refcnt, 1);
  49. msg->send_failed = 0;
  50. msg->send_error = 0;
  51. msg->can_abandon = 0;
  52. msg->can_delay = 1;
  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. sctp_ulpq_tail_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. atomic_inc(&msg->refcnt);
  124. }
  125. /* Release a reference. */
  126. void sctp_datamsg_put(struct sctp_datamsg *msg)
  127. {
  128. if (atomic_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. int max, whole, i, offset, over, err;
  149. int len, first_len;
  150. int max_data;
  151. struct sctp_chunk *chunk;
  152. struct sctp_datamsg *msg;
  153. struct list_head *pos, *temp;
  154. size_t msg_len = iov_iter_count(from);
  155. __u8 frag;
  156. msg = sctp_datamsg_new(GFP_KERNEL);
  157. if (!msg)
  158. return ERR_PTR(-ENOMEM);
  159. /* Note: Calculate this outside of the loop, so that all fragments
  160. * have the same expiration.
  161. */
  162. if (sinfo->sinfo_timetolive) {
  163. /* sinfo_timetolive is in milliseconds */
  164. msg->expires_at = jiffies +
  165. msecs_to_jiffies(sinfo->sinfo_timetolive);
  166. msg->can_abandon = 1;
  167. pr_debug("%s: msg:%p expires_at:%ld jiffies:%ld\n", __func__,
  168. msg, msg->expires_at, jiffies);
  169. }
  170. /* This is the biggest possible DATA chunk that can fit into
  171. * the packet
  172. */
  173. max_data = (asoc->pathmtu -
  174. sctp_sk(asoc->base.sk)->pf->af->net_header_len -
  175. sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk)) & ~3;
  176. max = asoc->frag_point;
  177. /* If the the peer requested that we authenticate DATA chunks
  178. * we need to account for bundling of the AUTH chunks along with
  179. * DATA.
  180. */
  181. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  182. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  183. if (hmac_desc)
  184. max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
  185. hmac_desc->hmac_len);
  186. }
  187. /* Now, check if we need to reduce our max */
  188. if (max > max_data)
  189. max = max_data;
  190. whole = 0;
  191. first_len = max;
  192. /* Check to see if we have a pending SACK and try to let it be bundled
  193. * with this message. Do this if we don't have any data queued already.
  194. * To check that, look at out_qlen and retransmit list.
  195. * NOTE: we will not reduce to account for SACK, if the message would
  196. * not have been fragmented.
  197. */
  198. if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
  199. asoc->outqueue.out_qlen == 0 &&
  200. list_empty(&asoc->outqueue.retransmit) &&
  201. msg_len > max)
  202. max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t));
  203. /* Encourage Cookie-ECHO bundling. */
  204. if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
  205. max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  206. /* Now that we adjusted completely, reset first_len */
  207. if (first_len > max_data)
  208. first_len = max_data;
  209. /* Account for a different sized first fragment */
  210. if (msg_len >= first_len) {
  211. msg_len -= first_len;
  212. whole = 1;
  213. msg->can_delay = 0;
  214. }
  215. /* How many full sized? How many bytes leftover? */
  216. whole += msg_len / max;
  217. over = msg_len % max;
  218. offset = 0;
  219. if ((whole > 1) || (whole && over))
  220. SCTP_INC_STATS_USER(sock_net(asoc->base.sk), SCTP_MIB_FRAGUSRMSGS);
  221. /* Create chunks for all the full sized DATA chunks. */
  222. for (i = 0, len = first_len; i < whole; i++) {
  223. frag = SCTP_DATA_MIDDLE_FRAG;
  224. if (0 == i)
  225. frag |= SCTP_DATA_FIRST_FRAG;
  226. if ((i == (whole - 1)) && !over) {
  227. frag |= SCTP_DATA_LAST_FRAG;
  228. /* The application requests to set the I-bit of the
  229. * last DATA chunk of a user message when providing
  230. * the user message to the SCTP implementation.
  231. */
  232. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  233. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  234. frag |= SCTP_DATA_SACK_IMM;
  235. }
  236. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
  237. if (!chunk) {
  238. err = -ENOMEM;
  239. goto errout;
  240. }
  241. err = sctp_user_addto_chunk(chunk, len, from);
  242. if (err < 0)
  243. goto errout_chunk_free;
  244. /* Put the chunk->skb back into the form expected by send. */
  245. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  246. - (__u8 *)chunk->skb->data);
  247. sctp_datamsg_assign(msg, chunk);
  248. list_add_tail(&chunk->frag_list, &msg->chunks);
  249. /* The first chunk, the first chunk was likely short
  250. * to allow bundling, so reset to full size.
  251. */
  252. if (0 == i)
  253. len = max;
  254. }
  255. /* .. now the leftover bytes. */
  256. if (over) {
  257. if (!whole)
  258. frag = SCTP_DATA_NOT_FRAG;
  259. else
  260. frag = SCTP_DATA_LAST_FRAG;
  261. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  262. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  263. frag |= SCTP_DATA_SACK_IMM;
  264. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
  265. if (!chunk) {
  266. err = -ENOMEM;
  267. goto errout;
  268. }
  269. err = sctp_user_addto_chunk(chunk, over, from);
  270. /* Put the chunk->skb back into the form expected by send. */
  271. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  272. - (__u8 *)chunk->skb->data);
  273. if (err < 0)
  274. goto errout_chunk_free;
  275. sctp_datamsg_assign(msg, chunk);
  276. list_add_tail(&chunk->frag_list, &msg->chunks);
  277. }
  278. return msg;
  279. errout_chunk_free:
  280. sctp_chunk_free(chunk);
  281. errout:
  282. list_for_each_safe(pos, temp, &msg->chunks) {
  283. list_del_init(pos);
  284. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  285. sctp_chunk_free(chunk);
  286. }
  287. sctp_datamsg_put(msg);
  288. return ERR_PTR(err);
  289. }
  290. /* Check whether this message has expired. */
  291. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  292. {
  293. struct sctp_datamsg *msg = chunk->msg;
  294. if (!msg->can_abandon)
  295. return 0;
  296. if (time_after(jiffies, msg->expires_at))
  297. return 1;
  298. return 0;
  299. }
  300. /* This chunk (and consequently entire message) has failed in its sending. */
  301. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  302. {
  303. chunk->msg->send_failed = 1;
  304. chunk->msg->send_error = error;
  305. }