chunk.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. return msg;
  69. }
  70. /* Final destructruction of datamsg memory. */
  71. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  72. {
  73. struct list_head *pos, *temp;
  74. struct sctp_chunk *chunk;
  75. struct sctp_sock *sp;
  76. struct sctp_ulpevent *ev;
  77. struct sctp_association *asoc = NULL;
  78. int error = 0, notify;
  79. /* If we failed, we may need to notify. */
  80. notify = msg->send_failed ? -1 : 0;
  81. /* Release all references. */
  82. list_for_each_safe(pos, temp, &msg->chunks) {
  83. list_del_init(pos);
  84. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  85. /* Check whether we _really_ need to notify. */
  86. if (notify < 0) {
  87. asoc = chunk->asoc;
  88. if (msg->send_error)
  89. error = msg->send_error;
  90. else
  91. error = asoc->outqueue.error;
  92. sp = sctp_sk(asoc->base.sk);
  93. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  94. &sp->subscribe);
  95. }
  96. /* Generate a SEND FAILED event only if enabled. */
  97. if (notify > 0) {
  98. int sent;
  99. if (chunk->has_tsn)
  100. sent = SCTP_DATA_SENT;
  101. else
  102. sent = SCTP_DATA_UNSENT;
  103. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  104. error, GFP_ATOMIC);
  105. if (ev)
  106. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  107. }
  108. sctp_chunk_put(chunk);
  109. }
  110. SCTP_DBG_OBJCNT_DEC(datamsg);
  111. kfree(msg);
  112. }
  113. /* Hold a reference. */
  114. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  115. {
  116. atomic_inc(&msg->refcnt);
  117. }
  118. /* Release a reference. */
  119. void sctp_datamsg_put(struct sctp_datamsg *msg)
  120. {
  121. if (atomic_dec_and_test(&msg->refcnt))
  122. sctp_datamsg_destroy(msg);
  123. }
  124. /* Assign a chunk to this datamsg. */
  125. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  126. {
  127. sctp_datamsg_hold(msg);
  128. chunk->msg = msg;
  129. }
  130. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  131. * down any such message into smaller chunks. Opportunistically, fragment
  132. * the chunks down to the current MTU constraints. We may get refragmented
  133. * later if the PMTU changes, but it is _much better_ to fragment immediately
  134. * with a reasonable guess than always doing our fragmentation on the
  135. * soft-interrupt.
  136. */
  137. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  138. struct sctp_sndrcvinfo *sinfo,
  139. struct msghdr *msgh, int msg_len)
  140. {
  141. int max, whole, i, offset, over, err;
  142. int len, first_len;
  143. struct sctp_chunk *chunk;
  144. struct sctp_datamsg *msg;
  145. struct list_head *pos, *temp;
  146. __u8 frag;
  147. msg = sctp_datamsg_new(GFP_KERNEL);
  148. if (!msg)
  149. return NULL;
  150. /* Note: Calculate this outside of the loop, so that all fragments
  151. * have the same expiration.
  152. */
  153. if (sinfo->sinfo_timetolive) {
  154. /* sinfo_timetolive is in milliseconds */
  155. msg->expires_at = jiffies +
  156. msecs_to_jiffies(sinfo->sinfo_timetolive);
  157. msg->can_abandon = 1;
  158. SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n",
  159. __func__, msg, msg->expires_at, jiffies);
  160. }
  161. max = asoc->frag_point;
  162. /* If the the peer requested that we authenticate DATA chunks
  163. * we need to accound for bundling of the AUTH chunks along with
  164. * DATA.
  165. */
  166. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  167. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  168. if (hmac_desc)
  169. max -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
  170. hmac_desc->hmac_len);
  171. }
  172. whole = 0;
  173. first_len = max;
  174. /* Encourage Cookie-ECHO bundling. */
  175. if (asoc->state < SCTP_STATE_COOKIE_ECHOED) {
  176. whole = msg_len / (max - SCTP_ARBITRARY_COOKIE_ECHO_LEN);
  177. /* Account for the DATA to be bundled with the COOKIE-ECHO. */
  178. if (whole) {
  179. first_len = max - SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  180. msg_len -= first_len;
  181. whole = 1;
  182. }
  183. }
  184. /* How many full sized? How many bytes leftover? */
  185. whole += msg_len / max;
  186. over = msg_len % max;
  187. offset = 0;
  188. if ((whole > 1) || (whole && over))
  189. SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
  190. /* Create chunks for all the full sized DATA chunks. */
  191. for (i=0, len=first_len; i < whole; i++) {
  192. frag = SCTP_DATA_MIDDLE_FRAG;
  193. if (0 == i)
  194. frag |= SCTP_DATA_FIRST_FRAG;
  195. if ((i == (whole - 1)) && !over)
  196. frag |= SCTP_DATA_LAST_FRAG;
  197. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
  198. if (!chunk)
  199. goto errout;
  200. err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
  201. if (err < 0)
  202. goto errout;
  203. offset += len;
  204. /* Put the chunk->skb back into the form expected by send. */
  205. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  206. - (__u8 *)chunk->skb->data);
  207. sctp_datamsg_assign(msg, chunk);
  208. list_add_tail(&chunk->frag_list, &msg->chunks);
  209. /* The first chunk, the first chunk was likely short
  210. * to allow bundling, so reset to full size.
  211. */
  212. if (0 == i)
  213. len = max;
  214. }
  215. /* .. now the leftover bytes. */
  216. if (over) {
  217. if (!whole)
  218. frag = SCTP_DATA_NOT_FRAG;
  219. else
  220. frag = SCTP_DATA_LAST_FRAG;
  221. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
  222. if (!chunk)
  223. goto errout;
  224. err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
  225. /* Put the chunk->skb back into the form expected by send. */
  226. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  227. - (__u8 *)chunk->skb->data);
  228. if (err < 0)
  229. goto errout;
  230. sctp_datamsg_assign(msg, chunk);
  231. list_add_tail(&chunk->frag_list, &msg->chunks);
  232. }
  233. return msg;
  234. errout:
  235. list_for_each_safe(pos, temp, &msg->chunks) {
  236. list_del_init(pos);
  237. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  238. sctp_chunk_free(chunk);
  239. }
  240. sctp_datamsg_put(msg);
  241. return NULL;
  242. }
  243. /* Check whether this message has expired. */
  244. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  245. {
  246. struct sctp_datamsg *msg = chunk->msg;
  247. if (!msg->can_abandon)
  248. return 0;
  249. if (time_after(jiffies, msg->expires_at))
  250. return 1;
  251. return 0;
  252. }
  253. /* This chunk (and consequently entire message) has failed in its sending. */
  254. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  255. {
  256. chunk->msg->send_failed = 1;
  257. chunk->msg->send_error = error;
  258. }