2
0

quic_sf_list.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/uint_set.h"
  10. #include "internal/common.h"
  11. #include "internal/quic_sf_list.h"
  12. struct stream_frame_st {
  13. struct stream_frame_st *prev, *next;
  14. UINT_RANGE range;
  15. OSSL_QRX_PKT *pkt;
  16. const unsigned char *data;
  17. };
  18. static void stream_frame_free(SFRAME_LIST *fl, STREAM_FRAME *sf)
  19. {
  20. if (fl->cleanse && sf->data != NULL)
  21. OPENSSL_cleanse((unsigned char *)sf->data,
  22. (size_t)(sf->range.end - sf->range.start));
  23. ossl_qrx_pkt_release(sf->pkt);
  24. OPENSSL_free(sf);
  25. }
  26. static STREAM_FRAME *stream_frame_new(UINT_RANGE *range, OSSL_QRX_PKT *pkt,
  27. const unsigned char *data)
  28. {
  29. STREAM_FRAME *sf = OPENSSL_zalloc(sizeof(*sf));
  30. if (sf == NULL)
  31. return NULL;
  32. if (pkt != NULL)
  33. ossl_qrx_pkt_up_ref(pkt);
  34. sf->range = *range;
  35. sf->pkt = pkt;
  36. sf->data = data;
  37. return sf;
  38. }
  39. void ossl_sframe_list_init(SFRAME_LIST *fl)
  40. {
  41. memset(fl, 0, sizeof(*fl));
  42. }
  43. void ossl_sframe_list_destroy(SFRAME_LIST *fl)
  44. {
  45. STREAM_FRAME *sf, *next_frame;
  46. for (sf = fl->head; sf != NULL; sf = next_frame) {
  47. next_frame = sf->next;
  48. stream_frame_free(fl, sf);
  49. }
  50. }
  51. static int append_frame(SFRAME_LIST *fl, UINT_RANGE *range,
  52. OSSL_QRX_PKT *pkt,
  53. const unsigned char *data)
  54. {
  55. STREAM_FRAME *new_frame;
  56. if ((new_frame = stream_frame_new(range, pkt, data)) == NULL)
  57. return 0;
  58. new_frame->prev = fl->tail;
  59. if (fl->tail != NULL)
  60. fl->tail->next = new_frame;
  61. fl->tail = new_frame;
  62. ++fl->num_frames;
  63. return 1;
  64. }
  65. int ossl_sframe_list_insert(SFRAME_LIST *fl, UINT_RANGE *range,
  66. OSSL_QRX_PKT *pkt,
  67. const unsigned char *data, int fin)
  68. {
  69. STREAM_FRAME *sf, *new_frame, *prev_frame, *next_frame;
  70. #ifndef NDEBUG
  71. uint64_t curr_end = fl->tail != NULL ? fl->tail->range.end
  72. : fl->offset;
  73. /* This check for FINAL_SIZE_ERROR is handled by QUIC FC already */
  74. assert((!fin || curr_end <= range->end)
  75. && (!fl->fin || curr_end >= range->end));
  76. #endif
  77. if (fl->offset >= range->end)
  78. goto end;
  79. /* nothing there yet */
  80. if (fl->tail == NULL) {
  81. fl->tail = fl->head = stream_frame_new(range, pkt, data);
  82. if (fl->tail == NULL)
  83. return 0;
  84. ++fl->num_frames;
  85. goto end;
  86. }
  87. /* optimize insertion at the end */
  88. if (fl->tail->range.start < range->start) {
  89. if (fl->tail->range.end >= range->end)
  90. goto end;
  91. if (!append_frame(fl, range, pkt, data))
  92. return 0;
  93. goto end;
  94. }
  95. prev_frame = NULL;
  96. for (sf = fl->head; sf != NULL && sf->range.start < range->start;
  97. sf = sf->next)
  98. prev_frame = sf;
  99. if (!ossl_assert(sf != NULL))
  100. /* frame list invariant broken */
  101. return 0;
  102. if (prev_frame != NULL && prev_frame->range.end >= range->end)
  103. goto end;
  104. /*
  105. * Now we must create a new frame although in the end we might drop it,
  106. * because we will be potentially dropping existing overlapping frames.
  107. */
  108. new_frame = stream_frame_new(range, pkt, data);
  109. if (new_frame == NULL)
  110. return 0;
  111. for (next_frame = sf;
  112. next_frame != NULL && next_frame->range.end <= range->end;) {
  113. STREAM_FRAME *drop_frame = next_frame;
  114. next_frame = next_frame->next;
  115. if (next_frame != NULL)
  116. next_frame->prev = drop_frame->prev;
  117. if (prev_frame != NULL)
  118. prev_frame->next = drop_frame->next;
  119. if (fl->head == drop_frame)
  120. fl->head = next_frame;
  121. if (fl->tail == drop_frame)
  122. fl->tail = prev_frame;
  123. --fl->num_frames;
  124. stream_frame_free(fl, drop_frame);
  125. }
  126. if (next_frame != NULL) {
  127. /* check whether the new_frame is redundant because there is no gap */
  128. if (prev_frame != NULL
  129. && next_frame->range.start <= prev_frame->range.end) {
  130. stream_frame_free(fl, new_frame);
  131. goto end;
  132. }
  133. next_frame->prev = new_frame;
  134. } else {
  135. fl->tail = new_frame;
  136. }
  137. new_frame->next = next_frame;
  138. new_frame->prev = prev_frame;
  139. if (prev_frame != NULL)
  140. prev_frame->next = new_frame;
  141. else
  142. fl->head = new_frame;
  143. ++fl->num_frames;
  144. end:
  145. fl->fin = fin || fl->fin;
  146. return 1;
  147. }
  148. int ossl_sframe_list_peek(const SFRAME_LIST *fl, void **iter,
  149. UINT_RANGE *range, const unsigned char **data,
  150. int *fin)
  151. {
  152. STREAM_FRAME *sf = *iter;
  153. uint64_t start;
  154. if (sf == NULL) {
  155. start = fl->offset;
  156. sf = fl->head;
  157. } else {
  158. start = sf->range.end;
  159. sf = sf->next;
  160. }
  161. range->start = start;
  162. if (sf == NULL || sf->range.start > start
  163. || !ossl_assert(start < sf->range.end)) {
  164. range->end = start;
  165. *data = NULL;
  166. *iter = NULL;
  167. /* set fin only if we are at the end */
  168. *fin = sf == NULL ? fl->fin : 0;
  169. return 0;
  170. }
  171. range->end = sf->range.end;
  172. if (sf->data != NULL)
  173. *data = sf->data + (start - sf->range.start);
  174. else
  175. *data = NULL;
  176. *fin = sf->next == NULL ? fl->fin : 0;
  177. *iter = sf;
  178. return 1;
  179. }
  180. int ossl_sframe_list_drop_frames(SFRAME_LIST *fl, uint64_t limit)
  181. {
  182. STREAM_FRAME *sf;
  183. /* offset cannot move back or past the data received */
  184. if (!ossl_assert(limit >= fl->offset)
  185. || !ossl_assert(fl->tail == NULL
  186. || limit <= fl->tail->range.end)
  187. || !ossl_assert(fl->tail != NULL
  188. || limit == fl->offset))
  189. return 0;
  190. fl->offset = limit;
  191. for (sf = fl->head; sf != NULL && sf->range.end <= limit;) {
  192. STREAM_FRAME *drop_frame = sf;
  193. sf = sf->next;
  194. --fl->num_frames;
  195. stream_frame_free(fl, drop_frame);
  196. }
  197. fl->head = sf;
  198. if (sf != NULL)
  199. sf->prev = NULL;
  200. else
  201. fl->tail = NULL;
  202. fl->head_locked = 0;
  203. return 1;
  204. }
  205. int ossl_sframe_list_lock_head(SFRAME_LIST *fl, UINT_RANGE *range,
  206. const unsigned char **data,
  207. int *fin)
  208. {
  209. int ret;
  210. void *iter = NULL;
  211. if (fl->head_locked)
  212. return 0;
  213. ret = ossl_sframe_list_peek(fl, &iter, range, data, fin);
  214. if (ret)
  215. fl->head_locked = 1;
  216. return ret;
  217. }
  218. int ossl_sframe_list_is_head_locked(SFRAME_LIST *fl)
  219. {
  220. return fl->head_locked;
  221. }
  222. int ossl_sframe_list_move_data(SFRAME_LIST *fl,
  223. sframe_list_write_at_cb *write_at_cb,
  224. void *cb_arg)
  225. {
  226. STREAM_FRAME *sf = fl->head, *prev_frame = NULL;
  227. uint64_t limit = fl->offset;
  228. if (sf == NULL)
  229. return 1;
  230. if (fl->head_locked)
  231. sf = sf->next;
  232. for (; sf != NULL; sf = sf->next) {
  233. size_t len;
  234. const unsigned char *data = sf->data;
  235. if (limit < sf->range.start)
  236. limit = sf->range.start;
  237. if (data != NULL) {
  238. if (limit > sf->range.start)
  239. data += (size_t)(limit - sf->range.start);
  240. len = (size_t)(sf->range.end - limit);
  241. if (!write_at_cb(limit, data, len, cb_arg))
  242. /* data did not fit */
  243. return 0;
  244. if (fl->cleanse)
  245. OPENSSL_cleanse((unsigned char *)sf->data,
  246. (size_t)(sf->range.end - sf->range.start));
  247. /* release the packet */
  248. sf->data = NULL;
  249. ossl_qrx_pkt_release(sf->pkt);
  250. sf->pkt = NULL;
  251. }
  252. limit = sf->range.end;
  253. /* merge contiguous frames */
  254. if (prev_frame != NULL
  255. && prev_frame->range.end >= sf->range.start) {
  256. prev_frame->range.end = sf->range.end;
  257. prev_frame->next = sf->next;
  258. if (sf->next != NULL)
  259. sf->next->prev = prev_frame;
  260. else
  261. fl->tail = prev_frame;
  262. --fl->num_frames;
  263. stream_frame_free(fl, sf);
  264. sf = prev_frame;
  265. continue;
  266. }
  267. prev_frame = sf;
  268. }
  269. return 1;
  270. }