chunk.c 9.8 KB

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