chunk.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <linux/slab.h>
  45. #include <net/sock.h>
  46. #include <net/sctp/sctp.h>
  47. #include <net/sctp/sm.h>
  48. /* This file is mostly in anticipation of future work, but initially
  49. * populate with fragment tracking for an outbound message.
  50. */
  51. /* Initialize datamsg from memory. */
  52. static void sctp_datamsg_init(struct sctp_datamsg *msg)
  53. {
  54. atomic_set(&msg->refcnt, 1);
  55. msg->send_failed = 0;
  56. msg->send_error = 0;
  57. msg->can_abandon = 0;
  58. msg->can_delay = 1;
  59. msg->expires_at = 0;
  60. INIT_LIST_HEAD(&msg->chunks);
  61. }
  62. /* Allocate and initialize datamsg. */
  63. SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
  64. {
  65. struct sctp_datamsg *msg;
  66. msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
  67. if (msg) {
  68. sctp_datamsg_init(msg);
  69. SCTP_DBG_OBJCNT_INC(datamsg);
  70. }
  71. return msg;
  72. }
  73. void sctp_datamsg_free(struct sctp_datamsg *msg)
  74. {
  75. struct sctp_chunk *chunk;
  76. /* This doesn't have to be a _safe vairant because
  77. * sctp_chunk_free() only drops the refs.
  78. */
  79. list_for_each_entry(chunk, &msg->chunks, frag_list)
  80. sctp_chunk_free(chunk);
  81. sctp_datamsg_put(msg);
  82. }
  83. /* Final destructruction of datamsg memory. */
  84. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  85. {
  86. struct list_head *pos, *temp;
  87. struct sctp_chunk *chunk;
  88. struct sctp_sock *sp;
  89. struct sctp_ulpevent *ev;
  90. struct sctp_association *asoc = NULL;
  91. int error = 0, notify;
  92. /* If we failed, we may need to notify. */
  93. notify = msg->send_failed ? -1 : 0;
  94. /* Release all references. */
  95. list_for_each_safe(pos, temp, &msg->chunks) {
  96. list_del_init(pos);
  97. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  98. /* Check whether we _really_ need to notify. */
  99. if (notify < 0) {
  100. asoc = chunk->asoc;
  101. if (msg->send_error)
  102. error = msg->send_error;
  103. else
  104. error = asoc->outqueue.error;
  105. sp = sctp_sk(asoc->base.sk);
  106. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  107. &sp->subscribe);
  108. }
  109. /* Generate a SEND FAILED event only if enabled. */
  110. if (notify > 0) {
  111. int sent;
  112. if (chunk->has_tsn)
  113. sent = SCTP_DATA_SENT;
  114. else
  115. sent = SCTP_DATA_UNSENT;
  116. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  117. error, GFP_ATOMIC);
  118. if (ev)
  119. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  120. }
  121. sctp_chunk_put(chunk);
  122. }
  123. SCTP_DBG_OBJCNT_DEC(datamsg);
  124. kfree(msg);
  125. }
  126. /* Hold a reference. */
  127. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  128. {
  129. atomic_inc(&msg->refcnt);
  130. }
  131. /* Release a reference. */
  132. void sctp_datamsg_put(struct sctp_datamsg *msg)
  133. {
  134. if (atomic_dec_and_test(&msg->refcnt))
  135. sctp_datamsg_destroy(msg);
  136. }
  137. /* Assign a chunk to this datamsg. */
  138. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  139. {
  140. sctp_datamsg_hold(msg);
  141. chunk->msg = msg;
  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. msg->can_delay = 0;
  219. }
  220. /* How many full sized? How many bytes leftover? */
  221. whole += msg_len / max;
  222. over = msg_len % max;
  223. offset = 0;
  224. if ((whole > 1) || (whole && over))
  225. SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
  226. /* Create chunks for all the full sized DATA chunks. */
  227. for (i=0, len=first_len; i < whole; i++) {
  228. frag = SCTP_DATA_MIDDLE_FRAG;
  229. if (0 == i)
  230. frag |= SCTP_DATA_FIRST_FRAG;
  231. if ((i == (whole - 1)) && !over) {
  232. frag |= SCTP_DATA_LAST_FRAG;
  233. /* The application requests to set the I-bit of the
  234. * last DATA chunk of a user message when providing
  235. * the user message to the SCTP implementation.
  236. */
  237. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  238. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  239. frag |= SCTP_DATA_SACK_IMM;
  240. }
  241. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
  242. if (!chunk)
  243. goto errout;
  244. err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
  245. if (err < 0)
  246. goto errout;
  247. offset += len;
  248. /* Put the chunk->skb back into the form expected by send. */
  249. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  250. - (__u8 *)chunk->skb->data);
  251. sctp_datamsg_assign(msg, chunk);
  252. list_add_tail(&chunk->frag_list, &msg->chunks);
  253. /* The first chunk, the first chunk was likely short
  254. * to allow bundling, so reset to full size.
  255. */
  256. if (0 == i)
  257. len = max;
  258. }
  259. /* .. now the leftover bytes. */
  260. if (over) {
  261. if (!whole)
  262. frag = SCTP_DATA_NOT_FRAG;
  263. else
  264. frag = SCTP_DATA_LAST_FRAG;
  265. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  266. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  267. frag |= SCTP_DATA_SACK_IMM;
  268. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
  269. if (!chunk)
  270. goto errout;
  271. err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
  272. /* Put the chunk->skb back into the form expected by send. */
  273. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  274. - (__u8 *)chunk->skb->data);
  275. if (err < 0)
  276. goto errout;
  277. sctp_datamsg_assign(msg, chunk);
  278. list_add_tail(&chunk->frag_list, &msg->chunks);
  279. }
  280. return msg;
  281. errout:
  282. list_for_each_safe(pos, temp, &msg->chunks) {
  283. list_del_init(pos);
  284. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  285. sctp_chunk_free(chunk);
  286. }
  287. sctp_datamsg_put(msg);
  288. return NULL;
  289. }
  290. /* Check whether this message has expired. */
  291. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  292. {
  293. struct sctp_datamsg *msg = chunk->msg;
  294. if (!msg->can_abandon)
  295. return 0;
  296. if (time_after(jiffies, msg->expires_at))
  297. return 1;
  298. return 0;
  299. }
  300. /* This chunk (and consequently entire message) has failed in its sending. */
  301. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  302. {
  303. chunk->msg->send_failed = 1;
  304. chunk->msg->send_error = error;
  305. }