chunk.c 9.2 KB

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