chunk.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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, write to
  22. * the Free Software Foundation, 59 Temple Place - Suite 330,
  23. * Boston, MA 02111-1307, USA.
  24. *
  25. * Please send any bug reports or fixes you make to the
  26. * email address(es):
  27. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  28. *
  29. * Or submit a bug report through the following website:
  30. * http://www.sf.net/projects/lksctp
  31. *
  32. * Written or modified by:
  33. * Jon Grimm <jgrimm@us.ibm.com>
  34. * Sridhar Samudrala <sri@us.ibm.com>
  35. *
  36. * Any bugs reported given to us we will try to fix... any fixes shared will
  37. * be incorporated into the next SCTP release.
  38. */
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/net.h>
  42. #include <linux/inet.h>
  43. #include <linux/skbuff.h>
  44. #include <net/sock.h>
  45. #include <net/sctp/sctp.h>
  46. #include <net/sctp/sm.h>
  47. /* This file is mostly in anticipation of future work, but initially
  48. * populate with fragment tracking for an outbound message.
  49. */
  50. /* Initialize datamsg from memory. */
  51. static void sctp_datamsg_init(struct sctp_datamsg *msg)
  52. {
  53. atomic_set(&msg->refcnt, 1);
  54. msg->send_failed = 0;
  55. msg->send_error = 0;
  56. msg->can_abandon = 0;
  57. msg->expires_at = 0;
  58. INIT_LIST_HEAD(&msg->chunks);
  59. }
  60. /* Allocate and initialize datamsg. */
  61. SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
  62. {
  63. struct sctp_datamsg *msg;
  64. msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
  65. if (msg) {
  66. sctp_datamsg_init(msg);
  67. SCTP_DBG_OBJCNT_INC(datamsg);
  68. }
  69. return msg;
  70. }
  71. /* Final destructruction of datamsg memory. */
  72. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  73. {
  74. struct list_head *pos, *temp;
  75. struct sctp_chunk *chunk;
  76. struct sctp_sock *sp;
  77. struct sctp_ulpevent *ev;
  78. struct sctp_association *asoc = NULL;
  79. int error = 0, notify;
  80. /* If we failed, we may need to notify. */
  81. notify = msg->send_failed ? -1 : 0;
  82. /* Release all references. */
  83. list_for_each_safe(pos, temp, &msg->chunks) {
  84. list_del_init(pos);
  85. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  86. /* Check whether we _really_ need to notify. */
  87. if (notify < 0) {
  88. asoc = chunk->asoc;
  89. if (msg->send_error)
  90. error = msg->send_error;
  91. else
  92. error = asoc->outqueue.error;
  93. sp = sctp_sk(asoc->base.sk);
  94. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  95. &sp->subscribe);
  96. }
  97. /* Generate a SEND FAILED event only if enabled. */
  98. if (notify > 0) {
  99. int sent;
  100. if (chunk->has_tsn)
  101. sent = SCTP_DATA_SENT;
  102. else
  103. sent = SCTP_DATA_UNSENT;
  104. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  105. error, GFP_ATOMIC);
  106. if (ev)
  107. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  108. }
  109. sctp_chunk_put(chunk);
  110. }
  111. SCTP_DBG_OBJCNT_DEC(datamsg);
  112. kfree(msg);
  113. }
  114. /* Hold a reference. */
  115. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  116. {
  117. atomic_inc(&msg->refcnt);
  118. }
  119. /* Release a reference. */
  120. void sctp_datamsg_put(struct sctp_datamsg *msg)
  121. {
  122. if (atomic_dec_and_test(&msg->refcnt))
  123. sctp_datamsg_destroy(msg);
  124. }
  125. /* Assign a chunk to this datamsg. */
  126. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  127. {
  128. sctp_datamsg_hold(msg);
  129. chunk->msg = msg;
  130. }
  131. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  132. * down any such message into smaller chunks. Opportunistically, fragment
  133. * the chunks down to the current MTU constraints. We may get refragmented
  134. * later if the PMTU changes, but it is _much better_ to fragment immediately
  135. * with a reasonable guess than always doing our fragmentation on the
  136. * soft-interrupt.
  137. */
  138. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  139. struct sctp_sndrcvinfo *sinfo,
  140. struct msghdr *msgh, int msg_len)
  141. {
  142. int max, whole, i, offset, over, err;
  143. int len, first_len;
  144. struct sctp_chunk *chunk;
  145. struct sctp_datamsg *msg;
  146. struct list_head *pos, *temp;
  147. __u8 frag;
  148. msg = sctp_datamsg_new(GFP_KERNEL);
  149. if (!msg)
  150. return NULL;
  151. /* Note: Calculate this outside of the loop, so that all fragments
  152. * have the same expiration.
  153. */
  154. if (sinfo->sinfo_timetolive) {
  155. /* sinfo_timetolive is in milliseconds */
  156. msg->expires_at = jiffies +
  157. msecs_to_jiffies(sinfo->sinfo_timetolive);
  158. msg->can_abandon = 1;
  159. SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n",
  160. __func__, msg, msg->expires_at, jiffies);
  161. }
  162. max = asoc->frag_point;
  163. /* If the the peer requested that we authenticate DATA chunks
  164. * we need to accound for bundling of the AUTH chunks along with
  165. * DATA.
  166. */
  167. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  168. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  169. if (hmac_desc)
  170. max -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
  171. hmac_desc->hmac_len);
  172. }
  173. whole = 0;
  174. first_len = max;
  175. /* Encourage Cookie-ECHO bundling. */
  176. if (asoc->state < SCTP_STATE_COOKIE_ECHOED) {
  177. whole = msg_len / (max - SCTP_ARBITRARY_COOKIE_ECHO_LEN);
  178. /* Account for the DATA to be bundled with the COOKIE-ECHO. */
  179. if (whole) {
  180. first_len = max - SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  181. msg_len -= first_len;
  182. whole = 1;
  183. }
  184. }
  185. /* How many full sized? How many bytes leftover? */
  186. whole += msg_len / max;
  187. over = msg_len % max;
  188. offset = 0;
  189. if ((whole > 1) || (whole && over))
  190. SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
  191. /* Create chunks for all the full sized DATA chunks. */
  192. for (i=0, len=first_len; i < whole; i++) {
  193. frag = SCTP_DATA_MIDDLE_FRAG;
  194. if (0 == i)
  195. frag |= SCTP_DATA_FIRST_FRAG;
  196. if ((i == (whole - 1)) && !over)
  197. frag |= SCTP_DATA_LAST_FRAG;
  198. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
  199. if (!chunk)
  200. goto errout;
  201. err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
  202. if (err < 0)
  203. goto errout;
  204. offset += len;
  205. /* Put the chunk->skb back into the form expected by send. */
  206. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  207. - (__u8 *)chunk->skb->data);
  208. sctp_datamsg_assign(msg, chunk);
  209. list_add_tail(&chunk->frag_list, &msg->chunks);
  210. /* The first chunk, the first chunk was likely short
  211. * to allow bundling, so reset to full size.
  212. */
  213. if (0 == i)
  214. len = max;
  215. }
  216. /* .. now the leftover bytes. */
  217. if (over) {
  218. if (!whole)
  219. frag = SCTP_DATA_NOT_FRAG;
  220. else
  221. frag = SCTP_DATA_LAST_FRAG;
  222. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
  223. if (!chunk)
  224. goto errout;
  225. err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
  226. /* Put the chunk->skb back into the form expected by send. */
  227. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  228. - (__u8 *)chunk->skb->data);
  229. if (err < 0)
  230. goto errout;
  231. sctp_datamsg_assign(msg, chunk);
  232. list_add_tail(&chunk->frag_list, &msg->chunks);
  233. }
  234. return msg;
  235. errout:
  236. list_for_each_safe(pos, temp, &msg->chunks) {
  237. list_del_init(pos);
  238. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  239. sctp_chunk_free(chunk);
  240. }
  241. sctp_datamsg_put(msg);
  242. return NULL;
  243. }
  244. /* Check whether this message has expired. */
  245. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  246. {
  247. struct sctp_datamsg *msg = chunk->msg;
  248. if (!msg->can_abandon)
  249. return 0;
  250. if (time_after(jiffies, msg->expires_at))
  251. return 1;
  252. return 0;
  253. }
  254. /* This chunk (and consequently entire message) has failed in its sending. */
  255. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  256. {
  257. chunk->msg->send_failed = 1;
  258. chunk->msg->send_error = error;
  259. }