chunk.c 9.8 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. 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. /* Final destructruction of datamsg memory. */
  68. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  69. {
  70. struct list_head *pos, *temp;
  71. struct sctp_chunk *chunk;
  72. struct sctp_sock *sp;
  73. struct sctp_ulpevent *ev;
  74. struct sctp_association *asoc = NULL;
  75. int error = 0, notify;
  76. /* If we failed, we may need to notify. */
  77. notify = msg->send_failed ? -1 : 0;
  78. /* Release all references. */
  79. list_for_each_safe(pos, temp, &msg->chunks) {
  80. list_del_init(pos);
  81. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  82. /* Check whether we _really_ need to notify. */
  83. if (notify < 0) {
  84. asoc = chunk->asoc;
  85. if (msg->send_error)
  86. error = msg->send_error;
  87. else
  88. error = asoc->outqueue.error;
  89. sp = sctp_sk(asoc->base.sk);
  90. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  91. &sp->subscribe);
  92. }
  93. /* Generate a SEND FAILED event only if enabled. */
  94. if (notify > 0) {
  95. int sent;
  96. if (chunk->has_tsn)
  97. sent = SCTP_DATA_SENT;
  98. else
  99. sent = SCTP_DATA_UNSENT;
  100. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  101. error, GFP_ATOMIC);
  102. if (ev)
  103. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  104. }
  105. sctp_chunk_put(chunk);
  106. }
  107. SCTP_DBG_OBJCNT_DEC(datamsg);
  108. kfree(msg);
  109. }
  110. /* Hold a reference. */
  111. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  112. {
  113. atomic_inc(&msg->refcnt);
  114. }
  115. /* Release a reference. */
  116. void sctp_datamsg_put(struct sctp_datamsg *msg)
  117. {
  118. if (atomic_dec_and_test(&msg->refcnt))
  119. sctp_datamsg_destroy(msg);
  120. }
  121. /* Assign a chunk to this datamsg. */
  122. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  123. {
  124. sctp_datamsg_hold(msg);
  125. chunk->msg = msg;
  126. }
  127. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  128. * down any such message into smaller chunks. Opportunistically, fragment
  129. * the chunks down to the current MTU constraints. We may get refragmented
  130. * later if the PMTU changes, but it is _much better_ to fragment immediately
  131. * with a reasonable guess than always doing our fragmentation on the
  132. * soft-interrupt.
  133. */
  134. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  135. struct sctp_sndrcvinfo *sinfo,
  136. struct iov_iter *from)
  137. {
  138. int max, whole, i, offset, over, err;
  139. int len, first_len;
  140. int max_data;
  141. struct sctp_chunk *chunk;
  142. struct sctp_datamsg *msg;
  143. struct list_head *pos, *temp;
  144. size_t msg_len = iov_iter_count(from);
  145. __u8 frag;
  146. msg = sctp_datamsg_new(GFP_KERNEL);
  147. if (!msg)
  148. return ERR_PTR(-ENOMEM);
  149. /* Note: Calculate this outside of the loop, so that all fragments
  150. * have the same expiration.
  151. */
  152. if (sinfo->sinfo_timetolive) {
  153. /* sinfo_timetolive is in milliseconds */
  154. msg->expires_at = jiffies +
  155. msecs_to_jiffies(sinfo->sinfo_timetolive);
  156. msg->can_abandon = 1;
  157. pr_debug("%s: msg:%p expires_at:%ld jiffies:%ld\n", __func__,
  158. msg, msg->expires_at, jiffies);
  159. }
  160. /* This is the biggest possible DATA chunk that can fit into
  161. * the packet
  162. */
  163. max_data = (asoc->pathmtu -
  164. sctp_sk(asoc->base.sk)->pf->af->net_header_len -
  165. sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk)) & ~3;
  166. max = asoc->frag_point;
  167. /* If the the peer requested that we authenticate DATA chunks
  168. * we need to account for bundling of the AUTH chunks along with
  169. * DATA.
  170. */
  171. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  172. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  173. if (hmac_desc)
  174. max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
  175. hmac_desc->hmac_len);
  176. }
  177. /* Now, check if we need to reduce our max */
  178. if (max > max_data)
  179. max = max_data;
  180. whole = 0;
  181. first_len = max;
  182. /* Check to see if we have a pending SACK and try to let it be bundled
  183. * with this message. Do this if we don't have any data queued already.
  184. * To check that, look at out_qlen and retransmit list.
  185. * NOTE: we will not reduce to account for SACK, if the message would
  186. * not have been fragmented.
  187. */
  188. if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
  189. asoc->outqueue.out_qlen == 0 &&
  190. list_empty(&asoc->outqueue.retransmit) &&
  191. msg_len > max)
  192. max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t));
  193. /* Encourage Cookie-ECHO bundling. */
  194. if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
  195. max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  196. /* Now that we adjusted completely, reset first_len */
  197. if (first_len > max_data)
  198. first_len = max_data;
  199. /* Account for a different sized first fragment */
  200. if (msg_len >= first_len) {
  201. msg_len -= first_len;
  202. whole = 1;
  203. msg->can_delay = 0;
  204. }
  205. /* How many full sized? How many bytes leftover? */
  206. whole += msg_len / max;
  207. over = msg_len % max;
  208. offset = 0;
  209. if ((whole > 1) || (whole && over))
  210. SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_FRAGUSRMSGS);
  211. /* Create chunks for all the full sized DATA chunks. */
  212. for (i = 0, len = first_len; i < whole; i++) {
  213. frag = SCTP_DATA_MIDDLE_FRAG;
  214. if (0 == i)
  215. frag |= SCTP_DATA_FIRST_FRAG;
  216. if ((i == (whole - 1)) && !over) {
  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 = sctp_make_datafrag_empty(asoc, sinfo, len, frag,
  227. 0, 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. /* Put the chunk->skb back into the form expected by send. */
  236. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  237. - (__u8 *)chunk->skb->data);
  238. sctp_datamsg_assign(msg, chunk);
  239. list_add_tail(&chunk->frag_list, &msg->chunks);
  240. /* The first chunk, the first chunk was likely short
  241. * to allow bundling, so reset to full size.
  242. */
  243. if (0 == i)
  244. len = max;
  245. }
  246. /* .. now the leftover bytes. */
  247. if (over) {
  248. if (!whole)
  249. frag = SCTP_DATA_NOT_FRAG;
  250. else
  251. frag = SCTP_DATA_LAST_FRAG;
  252. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  253. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  254. frag |= SCTP_DATA_SACK_IMM;
  255. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag,
  256. 0, GFP_KERNEL);
  257. if (!chunk) {
  258. err = -ENOMEM;
  259. goto errout;
  260. }
  261. err = sctp_user_addto_chunk(chunk, over, from);
  262. /* Put the chunk->skb back into the form expected by send. */
  263. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  264. - (__u8 *)chunk->skb->data);
  265. if (err < 0)
  266. goto errout_chunk_free;
  267. sctp_datamsg_assign(msg, chunk);
  268. list_add_tail(&chunk->frag_list, &msg->chunks);
  269. }
  270. return msg;
  271. errout_chunk_free:
  272. sctp_chunk_free(chunk);
  273. errout:
  274. list_for_each_safe(pos, temp, &msg->chunks) {
  275. list_del_init(pos);
  276. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  277. sctp_chunk_free(chunk);
  278. }
  279. sctp_datamsg_put(msg);
  280. return ERR_PTR(err);
  281. }
  282. /* Check whether this message has expired. */
  283. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  284. {
  285. if (!chunk->asoc->prsctp_enable ||
  286. !SCTP_PR_POLICY(chunk->sinfo.sinfo_flags)) {
  287. struct sctp_datamsg *msg = chunk->msg;
  288. if (!msg->can_abandon)
  289. return 0;
  290. if (time_after(jiffies, msg->expires_at))
  291. return 1;
  292. return 0;
  293. }
  294. if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
  295. time_after(jiffies, chunk->prsctp_param)) {
  296. if (chunk->sent_count)
  297. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  298. else
  299. chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  300. return 1;
  301. } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
  302. chunk->sent_count > chunk->prsctp_param) {
  303. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  304. return 1;
  305. }
  306. /* PRIO policy is processed by sendmsg, not here */
  307. return 0;
  308. }
  309. /* This chunk (and consequently entire message) has failed in its sending. */
  310. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  311. {
  312. chunk->msg->send_failed = 1;
  313. chunk->msg->send_error = error;
  314. }