chunk.c 9.6 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, see
  22. * <http://www.gnu.org/licenses/>.
  23. *
  24. * Please send any bug reports or fixes you make to the
  25. * email address(es):
  26. * lksctp developers <linux-sctp@vger.kernel.org>
  27. *
  28. * Written or modified by:
  29. * Jon Grimm <jgrimm@us.ibm.com>
  30. * Sridhar Samudrala <sri@us.ibm.com>
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/net.h>
  36. #include <linux/inet.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/slab.h>
  39. #include <net/sock.h>
  40. #include <net/sctp/sctp.h>
  41. #include <net/sctp/sm.h>
  42. /* This file is mostly in anticipation of future work, but initially
  43. * populate with fragment tracking for an outbound message.
  44. */
  45. /* Initialize datamsg from memory. */
  46. static void sctp_datamsg_init(struct sctp_datamsg *msg)
  47. {
  48. refcount_set(&msg->refcnt, 1);
  49. msg->send_failed = 0;
  50. msg->send_error = 0;
  51. msg->can_delay = 1;
  52. msg->expires_at = 0;
  53. INIT_LIST_HEAD(&msg->chunks);
  54. }
  55. /* Allocate and initialize datamsg. */
  56. static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
  57. {
  58. struct sctp_datamsg *msg;
  59. msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
  60. if (msg) {
  61. sctp_datamsg_init(msg);
  62. SCTP_DBG_OBJCNT_INC(datamsg);
  63. }
  64. return msg;
  65. }
  66. void sctp_datamsg_free(struct sctp_datamsg *msg)
  67. {
  68. struct sctp_chunk *chunk;
  69. /* This doesn't have to be a _safe vairant because
  70. * sctp_chunk_free() only drops the refs.
  71. */
  72. list_for_each_entry(chunk, &msg->chunks, frag_list)
  73. sctp_chunk_free(chunk);
  74. sctp_datamsg_put(msg);
  75. }
  76. /* Final destructruction of datamsg memory. */
  77. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  78. {
  79. struct list_head *pos, *temp;
  80. struct sctp_chunk *chunk;
  81. struct sctp_sock *sp;
  82. struct sctp_ulpevent *ev;
  83. struct sctp_association *asoc = NULL;
  84. int error = 0, notify;
  85. /* If we failed, we may need to notify. */
  86. notify = msg->send_failed ? -1 : 0;
  87. /* Release all references. */
  88. list_for_each_safe(pos, temp, &msg->chunks) {
  89. list_del_init(pos);
  90. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  91. /* Check whether we _really_ need to notify. */
  92. if (notify < 0) {
  93. asoc = chunk->asoc;
  94. if (msg->send_error)
  95. error = msg->send_error;
  96. else
  97. error = asoc->outqueue.error;
  98. sp = sctp_sk(asoc->base.sk);
  99. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  100. &sp->subscribe);
  101. }
  102. /* Generate a SEND FAILED event only if enabled. */
  103. if (notify > 0) {
  104. int sent;
  105. if (chunk->has_tsn)
  106. sent = SCTP_DATA_SENT;
  107. else
  108. sent = SCTP_DATA_UNSENT;
  109. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  110. error, GFP_ATOMIC);
  111. if (ev)
  112. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  113. }
  114. sctp_chunk_put(chunk);
  115. }
  116. SCTP_DBG_OBJCNT_DEC(datamsg);
  117. kfree(msg);
  118. }
  119. /* Hold a reference. */
  120. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  121. {
  122. refcount_inc(&msg->refcnt);
  123. }
  124. /* Release a reference. */
  125. void sctp_datamsg_put(struct sctp_datamsg *msg)
  126. {
  127. if (refcount_dec_and_test(&msg->refcnt))
  128. sctp_datamsg_destroy(msg);
  129. }
  130. /* Assign a chunk to this datamsg. */
  131. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  132. {
  133. sctp_datamsg_hold(msg);
  134. chunk->msg = msg;
  135. }
  136. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  137. * down any such message into smaller chunks. Opportunistically, fragment
  138. * the chunks down to the current MTU constraints. We may get refragmented
  139. * later if the PMTU changes, but it is _much better_ to fragment immediately
  140. * with a reasonable guess than always doing our fragmentation on the
  141. * soft-interrupt.
  142. */
  143. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  144. struct sctp_sndrcvinfo *sinfo,
  145. struct iov_iter *from)
  146. {
  147. size_t len, first_len, max_data, remaining;
  148. size_t msg_len = iov_iter_count(from);
  149. struct list_head *pos, *temp;
  150. struct sctp_chunk *chunk;
  151. struct sctp_datamsg *msg;
  152. int err;
  153. msg = sctp_datamsg_new(GFP_KERNEL);
  154. if (!msg)
  155. return ERR_PTR(-ENOMEM);
  156. /* Note: Calculate this outside of the loop, so that all fragments
  157. * have the same expiration.
  158. */
  159. if (asoc->peer.prsctp_capable && sinfo->sinfo_timetolive &&
  160. (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags) ||
  161. !SCTP_PR_POLICY(sinfo->sinfo_flags)))
  162. msg->expires_at = jiffies +
  163. msecs_to_jiffies(sinfo->sinfo_timetolive);
  164. /* This is the biggest possible DATA chunk that can fit into
  165. * the packet
  166. */
  167. max_data = asoc->pathmtu -
  168. sctp_sk(asoc->base.sk)->pf->af->net_header_len -
  169. sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
  170. max_data = SCTP_TRUNC4(max_data);
  171. /* If the the peer requested that we authenticate DATA chunks
  172. * we need to account for bundling of the AUTH chunks along with
  173. * DATA.
  174. */
  175. if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
  176. struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  177. if (hmac_desc)
  178. max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
  179. hmac_desc->hmac_len);
  180. }
  181. /* Check what's our max considering the above */
  182. max_data = min_t(size_t, max_data, asoc->frag_point);
  183. /* Set first_len and then account for possible bundles on first frag */
  184. first_len = max_data;
  185. /* Check to see if we have a pending SACK and try to let it be bundled
  186. * with this message. Do this if we don't have any data queued already.
  187. * To check that, look at out_qlen and retransmit list.
  188. * NOTE: we will not reduce to account for SACK, if the message would
  189. * not have been fragmented.
  190. */
  191. if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
  192. asoc->outqueue.out_qlen == 0 &&
  193. list_empty(&asoc->outqueue.retransmit) &&
  194. msg_len > max_data)
  195. first_len -= SCTP_PAD4(sizeof(struct sctp_sack_chunk));
  196. /* Encourage Cookie-ECHO bundling. */
  197. if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
  198. first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  199. /* Account for a different sized first fragment */
  200. if (msg_len >= first_len) {
  201. msg->can_delay = 0;
  202. if (msg_len > first_len)
  203. SCTP_INC_STATS(sock_net(asoc->base.sk),
  204. SCTP_MIB_FRAGUSRMSGS);
  205. } else {
  206. /* Which may be the only one... */
  207. first_len = msg_len;
  208. }
  209. /* Create chunks for all DATA chunks. */
  210. for (remaining = msg_len; remaining; remaining -= len) {
  211. u8 frag = SCTP_DATA_MIDDLE_FRAG;
  212. if (remaining == msg_len) {
  213. /* First frag, which may also be the last */
  214. frag |= SCTP_DATA_FIRST_FRAG;
  215. len = first_len;
  216. } else {
  217. /* Middle frags */
  218. len = max_data;
  219. }
  220. if (len >= remaining) {
  221. /* Last frag, which may also be the first */
  222. len = remaining;
  223. frag |= SCTP_DATA_LAST_FRAG;
  224. /* The application requests to set the I-bit of the
  225. * last DATA chunk of a user message when providing
  226. * the user message to the SCTP implementation.
  227. */
  228. if ((sinfo->sinfo_flags & SCTP_EOF) ||
  229. (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
  230. frag |= SCTP_DATA_SACK_IMM;
  231. }
  232. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag,
  233. 0, GFP_KERNEL);
  234. if (!chunk) {
  235. err = -ENOMEM;
  236. goto errout;
  237. }
  238. err = sctp_user_addto_chunk(chunk, len, from);
  239. if (err < 0)
  240. goto errout_chunk_free;
  241. /* Put the chunk->skb back into the form expected by send. */
  242. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
  243. chunk->skb->data);
  244. sctp_datamsg_assign(msg, chunk);
  245. list_add_tail(&chunk->frag_list, &msg->chunks);
  246. }
  247. return msg;
  248. errout_chunk_free:
  249. sctp_chunk_free(chunk);
  250. errout:
  251. list_for_each_safe(pos, temp, &msg->chunks) {
  252. list_del_init(pos);
  253. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  254. sctp_chunk_free(chunk);
  255. }
  256. sctp_datamsg_put(msg);
  257. return ERR_PTR(err);
  258. }
  259. /* Check whether this message has expired. */
  260. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  261. {
  262. if (!chunk->asoc->peer.prsctp_capable)
  263. return 0;
  264. if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
  265. time_after(jiffies, chunk->msg->expires_at)) {
  266. struct sctp_stream_out *streamout =
  267. &chunk->asoc->stream.out[chunk->sinfo.sinfo_stream];
  268. if (chunk->sent_count) {
  269. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  270. streamout->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
  271. } else {
  272. chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  273. streamout->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
  274. }
  275. return 1;
  276. } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
  277. chunk->sent_count > chunk->sinfo.sinfo_timetolive) {
  278. struct sctp_stream_out *streamout =
  279. &chunk->asoc->stream.out[chunk->sinfo.sinfo_stream];
  280. chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  281. streamout->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
  282. return 1;
  283. } else if (!SCTP_PR_POLICY(chunk->sinfo.sinfo_flags) &&
  284. chunk->msg->expires_at &&
  285. time_after(jiffies, chunk->msg->expires_at)) {
  286. return 1;
  287. }
  288. /* PRIO policy is processed by sendmsg, not here */
  289. return 0;
  290. }
  291. /* This chunk (and consequently entire message) has failed in its sending. */
  292. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  293. {
  294. chunk->msg->send_failed = 1;
  295. chunk->msg->send_error = error;
  296. }