chunk.c 9.1 KB

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