inqueue.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2002 International Business Machines, Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * These functions are the methods for accessing the SCTP inqueue.
  10. *
  11. * An SCTP inqueue is a queue into which you push SCTP packets
  12. * (which might be bundles or fragments of chunks) and out of which you
  13. * pop SCTP whole chunks.
  14. *
  15. * Please send any bug reports or fixes you make to the
  16. * email address(es):
  17. * lksctp developers <linux-sctp@vger.kernel.org>
  18. *
  19. * Written or modified by:
  20. * La Monte H.P. Yarroll <piggy@acm.org>
  21. * Karl Knutson <karl@athena.chicago.il.us>
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <net/sctp/sctp.h>
  25. #include <net/sctp/sm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/slab.h>
  28. /* Initialize an SCTP inqueue. */
  29. void sctp_inq_init(struct sctp_inq *queue)
  30. {
  31. INIT_LIST_HEAD(&queue->in_chunk_list);
  32. queue->in_progress = NULL;
  33. /* Create a task for delivering data. */
  34. INIT_WORK(&queue->immediate, NULL);
  35. }
  36. /* Properly release the chunk which is being worked on. */
  37. static inline void sctp_inq_chunk_free(struct sctp_chunk *chunk)
  38. {
  39. if (chunk->head_skb)
  40. chunk->skb = chunk->head_skb;
  41. sctp_chunk_free(chunk);
  42. }
  43. /* Release the memory associated with an SCTP inqueue. */
  44. void sctp_inq_free(struct sctp_inq *queue)
  45. {
  46. struct sctp_chunk *chunk, *tmp;
  47. /* Empty the queue. */
  48. list_for_each_entry_safe(chunk, tmp, &queue->in_chunk_list, list) {
  49. list_del_init(&chunk->list);
  50. sctp_chunk_free(chunk);
  51. }
  52. /* If there is a packet which is currently being worked on,
  53. * free it as well.
  54. */
  55. if (queue->in_progress) {
  56. sctp_inq_chunk_free(queue->in_progress);
  57. queue->in_progress = NULL;
  58. }
  59. }
  60. /* Put a new packet in an SCTP inqueue.
  61. * We assume that packet->sctp_hdr is set and in host byte order.
  62. */
  63. void sctp_inq_push(struct sctp_inq *q, struct sctp_chunk *chunk)
  64. {
  65. /* Directly call the packet handling routine. */
  66. if (chunk->rcvr->dead) {
  67. sctp_chunk_free(chunk);
  68. return;
  69. }
  70. /* We are now calling this either from the soft interrupt
  71. * or from the backlog processing.
  72. * Eventually, we should clean up inqueue to not rely
  73. * on the BH related data structures.
  74. */
  75. list_add_tail(&chunk->list, &q->in_chunk_list);
  76. if (chunk->asoc)
  77. chunk->asoc->stats.ipackets++;
  78. q->immediate.func(&q->immediate);
  79. }
  80. /* Peek at the next chunk on the inqeue. */
  81. struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *queue)
  82. {
  83. struct sctp_chunk *chunk;
  84. struct sctp_chunkhdr *ch = NULL;
  85. chunk = queue->in_progress;
  86. /* If there is no more chunks in this packet, say so */
  87. if (chunk->singleton ||
  88. chunk->end_of_packet ||
  89. chunk->pdiscard)
  90. return NULL;
  91. ch = (struct sctp_chunkhdr *)chunk->chunk_end;
  92. return ch;
  93. }
  94. /* Extract a chunk from an SCTP inqueue.
  95. *
  96. * WARNING: If you need to put the chunk on another queue, you need to
  97. * make a shallow copy (clone) of it.
  98. */
  99. struct sctp_chunk *sctp_inq_pop(struct sctp_inq *queue)
  100. {
  101. struct sctp_chunk *chunk;
  102. struct sctp_chunkhdr *ch = NULL;
  103. /* The assumption is that we are safe to process the chunks
  104. * at this time.
  105. */
  106. chunk = queue->in_progress;
  107. if (chunk) {
  108. /* There is a packet that we have been working on.
  109. * Any post processing work to do before we move on?
  110. */
  111. if (chunk->singleton ||
  112. chunk->end_of_packet ||
  113. chunk->pdiscard) {
  114. if (chunk->head_skb == chunk->skb) {
  115. chunk->skb = skb_shinfo(chunk->skb)->frag_list;
  116. goto new_skb;
  117. }
  118. if (chunk->skb->next) {
  119. chunk->skb = chunk->skb->next;
  120. goto new_skb;
  121. }
  122. sctp_inq_chunk_free(chunk);
  123. chunk = queue->in_progress = NULL;
  124. } else {
  125. /* Nothing to do. Next chunk in the packet, please. */
  126. ch = (struct sctp_chunkhdr *)chunk->chunk_end;
  127. /* Force chunk->skb->data to chunk->chunk_end. */
  128. skb_pull(chunk->skb, chunk->chunk_end - chunk->skb->data);
  129. /* We are guaranteed to pull a SCTP header. */
  130. }
  131. }
  132. /* Do we need to take the next packet out of the queue to process? */
  133. if (!chunk) {
  134. struct list_head *entry;
  135. next_chunk:
  136. /* Is the queue empty? */
  137. entry = sctp_list_dequeue(&queue->in_chunk_list);
  138. if (!entry)
  139. return NULL;
  140. chunk = list_entry(entry, struct sctp_chunk, list);
  141. if (skb_is_gso(chunk->skb) && skb_is_gso_sctp(chunk->skb)) {
  142. /* GSO-marked skbs but without frags, handle
  143. * them normally
  144. */
  145. if (skb_shinfo(chunk->skb)->frag_list)
  146. chunk->head_skb = chunk->skb;
  147. /* skbs with "cover letter" */
  148. if (chunk->head_skb && chunk->skb->data_len == chunk->skb->len) {
  149. if (WARN_ON(!skb_shinfo(chunk->skb)->frag_list)) {
  150. __SCTP_INC_STATS(dev_net(chunk->skb->dev),
  151. SCTP_MIB_IN_PKT_DISCARDS);
  152. sctp_chunk_free(chunk);
  153. goto next_chunk;
  154. }
  155. chunk->skb = skb_shinfo(chunk->skb)->frag_list;
  156. }
  157. }
  158. if (chunk->asoc)
  159. sock_rps_save_rxhash(chunk->asoc->base.sk, chunk->skb);
  160. queue->in_progress = chunk;
  161. new_skb:
  162. /* This is the first chunk in the packet. */
  163. ch = (struct sctp_chunkhdr *)chunk->skb->data;
  164. chunk->singleton = 1;
  165. chunk->data_accepted = 0;
  166. chunk->pdiscard = 0;
  167. chunk->auth = 0;
  168. chunk->has_asconf = 0;
  169. chunk->end_of_packet = 0;
  170. if (chunk->head_skb) {
  171. struct sctp_input_cb
  172. *cb = SCTP_INPUT_CB(chunk->skb),
  173. *head_cb = SCTP_INPUT_CB(chunk->head_skb);
  174. cb->chunk = head_cb->chunk;
  175. cb->af = head_cb->af;
  176. }
  177. }
  178. chunk->chunk_hdr = ch;
  179. chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
  180. skb_pull(chunk->skb, sizeof(*ch));
  181. chunk->subh.v = NULL; /* Subheader is no longer valid. */
  182. if (chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb)) {
  183. /* This is not a singleton */
  184. chunk->singleton = 0;
  185. } else if (chunk->chunk_end > skb_tail_pointer(chunk->skb)) {
  186. /* Discard inside state machine. */
  187. chunk->pdiscard = 1;
  188. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  189. } else {
  190. /* We are at the end of the packet, so mark the chunk
  191. * in case we need to send a SACK.
  192. */
  193. chunk->end_of_packet = 1;
  194. }
  195. pr_debug("+++sctp_inq_pop+++ chunk:%p[%s], length:%d, skb->len:%d\n",
  196. chunk, sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
  197. ntohs(chunk->chunk_hdr->length), chunk->skb->len);
  198. return chunk;
  199. }
  200. /* Set a top-half handler.
  201. *
  202. * Originally, we the top-half handler was scheduled as a BH. We now
  203. * call the handler directly in sctp_inq_push() at a time that
  204. * we know we are lock safe.
  205. * The intent is that this routine will pull stuff out of the
  206. * inqueue and process it.
  207. */
  208. void sctp_inq_set_th_handler(struct sctp_inq *q, work_func_t callback)
  209. {
  210. INIT_WORK(&q->immediate, callback);
  211. }