inqueue.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* Release the memory associated with an SCTP inqueue. */
  37. void sctp_inq_free(struct sctp_inq *queue)
  38. {
  39. struct sctp_chunk *chunk, *tmp;
  40. /* Empty the queue. */
  41. list_for_each_entry_safe(chunk, tmp, &queue->in_chunk_list, list) {
  42. list_del_init(&chunk->list);
  43. sctp_chunk_free(chunk);
  44. }
  45. /* If there is a packet which is currently being worked on,
  46. * free it as well.
  47. */
  48. if (queue->in_progress) {
  49. sctp_chunk_free(queue->in_progress);
  50. queue->in_progress = NULL;
  51. }
  52. }
  53. /* Put a new packet in an SCTP inqueue.
  54. * We assume that packet->sctp_hdr is set and in host byte order.
  55. */
  56. void sctp_inq_push(struct sctp_inq *q, struct sctp_chunk *chunk)
  57. {
  58. /* Directly call the packet handling routine. */
  59. if (chunk->rcvr->dead) {
  60. sctp_chunk_free(chunk);
  61. return;
  62. }
  63. /* We are now calling this either from the soft interrupt
  64. * or from the backlog processing.
  65. * Eventually, we should clean up inqueue to not rely
  66. * on the BH related data structures.
  67. */
  68. list_add_tail(&chunk->list, &q->in_chunk_list);
  69. if (chunk->asoc)
  70. chunk->asoc->stats.ipackets++;
  71. q->immediate.func(&q->immediate);
  72. }
  73. /* Peek at the next chunk on the inqeue. */
  74. struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *queue)
  75. {
  76. struct sctp_chunk *chunk;
  77. struct sctp_chunkhdr *ch = NULL;
  78. chunk = queue->in_progress;
  79. /* If there is no more chunks in this packet, say so */
  80. if (chunk->singleton ||
  81. chunk->end_of_packet ||
  82. chunk->pdiscard)
  83. return NULL;
  84. ch = (struct sctp_chunkhdr *)chunk->chunk_end;
  85. return ch;
  86. }
  87. /* Extract a chunk from an SCTP inqueue.
  88. *
  89. * WARNING: If you need to put the chunk on another queue, you need to
  90. * make a shallow copy (clone) of it.
  91. */
  92. struct sctp_chunk *sctp_inq_pop(struct sctp_inq *queue)
  93. {
  94. struct sctp_chunk *chunk;
  95. struct sctp_chunkhdr *ch = NULL;
  96. /* The assumption is that we are safe to process the chunks
  97. * at this time.
  98. */
  99. chunk = queue->in_progress;
  100. if (chunk) {
  101. /* There is a packet that we have been working on.
  102. * Any post processing work to do before we move on?
  103. */
  104. if (chunk->singleton ||
  105. chunk->end_of_packet ||
  106. chunk->pdiscard) {
  107. if (chunk->head_skb == chunk->skb) {
  108. chunk->skb = skb_shinfo(chunk->skb)->frag_list;
  109. goto new_skb;
  110. }
  111. if (chunk->skb->next) {
  112. chunk->skb = chunk->skb->next;
  113. goto new_skb;
  114. }
  115. if (chunk->head_skb)
  116. chunk->skb = chunk->head_skb;
  117. sctp_chunk_free(chunk);
  118. chunk = queue->in_progress = NULL;
  119. } else {
  120. /* Nothing to do. Next chunk in the packet, please. */
  121. ch = (struct sctp_chunkhdr *)chunk->chunk_end;
  122. /* Force chunk->skb->data to chunk->chunk_end. */
  123. skb_pull(chunk->skb, chunk->chunk_end - chunk->skb->data);
  124. /* We are guaranteed to pull a SCTP header. */
  125. }
  126. }
  127. /* Do we need to take the next packet out of the queue to process? */
  128. if (!chunk) {
  129. struct list_head *entry;
  130. next_chunk:
  131. /* Is the queue empty? */
  132. entry = sctp_list_dequeue(&queue->in_chunk_list);
  133. if (!entry)
  134. return NULL;
  135. chunk = list_entry(entry, struct sctp_chunk, list);
  136. if (skb_is_gso(chunk->skb) && skb_is_gso_sctp(chunk->skb)) {
  137. /* GSO-marked skbs but without frags, handle
  138. * them normally
  139. */
  140. if (skb_shinfo(chunk->skb)->frag_list)
  141. chunk->head_skb = chunk->skb;
  142. /* skbs with "cover letter" */
  143. if (chunk->head_skb && chunk->skb->data_len == chunk->skb->len)
  144. chunk->skb = skb_shinfo(chunk->skb)->frag_list;
  145. if (WARN_ON(!chunk->skb)) {
  146. __SCTP_INC_STATS(dev_net(chunk->skb->dev), SCTP_MIB_IN_PKT_DISCARDS);
  147. sctp_chunk_free(chunk);
  148. goto next_chunk;
  149. }
  150. }
  151. if (chunk->asoc)
  152. sock_rps_save_rxhash(chunk->asoc->base.sk, chunk->skb);
  153. queue->in_progress = chunk;
  154. new_skb:
  155. /* This is the first chunk in the packet. */
  156. ch = (struct sctp_chunkhdr *)chunk->skb->data;
  157. chunk->singleton = 1;
  158. chunk->data_accepted = 0;
  159. chunk->pdiscard = 0;
  160. chunk->auth = 0;
  161. chunk->has_asconf = 0;
  162. chunk->end_of_packet = 0;
  163. if (chunk->head_skb) {
  164. struct sctp_input_cb
  165. *cb = SCTP_INPUT_CB(chunk->skb),
  166. *head_cb = SCTP_INPUT_CB(chunk->head_skb);
  167. cb->chunk = head_cb->chunk;
  168. cb->af = head_cb->af;
  169. }
  170. }
  171. chunk->chunk_hdr = ch;
  172. chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
  173. skb_pull(chunk->skb, sizeof(*ch));
  174. chunk->subh.v = NULL; /* Subheader is no longer valid. */
  175. if (chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb)) {
  176. /* This is not a singleton */
  177. chunk->singleton = 0;
  178. } else if (chunk->chunk_end > skb_tail_pointer(chunk->skb)) {
  179. /* Discard inside state machine. */
  180. chunk->pdiscard = 1;
  181. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  182. } else {
  183. /* We are at the end of the packet, so mark the chunk
  184. * in case we need to send a SACK.
  185. */
  186. chunk->end_of_packet = 1;
  187. }
  188. pr_debug("+++sctp_inq_pop+++ chunk:%p[%s], length:%d, skb->len:%d\n",
  189. chunk, sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
  190. ntohs(chunk->chunk_hdr->length), chunk->skb->len);
  191. return chunk;
  192. }
  193. /* Set a top-half handler.
  194. *
  195. * Originally, we the top-half handler was scheduled as a BH. We now
  196. * call the handler directly in sctp_inq_push() at a time that
  197. * we know we are lock safe.
  198. * The intent is that this routine will pull stuff out of the
  199. * inqueue and process it.
  200. */
  201. void sctp_inq_set_th_handler(struct sctp_inq *q, work_func_t callback)
  202. {
  203. INIT_WORK(&q->immediate, callback);
  204. }